blob: c558a848a4b6a8e23cba3a00fa8101e36b00713f [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
Hanno Beckerc5f24222019-05-03 12:54:52 +0100168 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
169 * were used, but client and server requested the empty CID.
170 * This is indistinguishable from not using the CID extension
171 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100172 if( ssl->transform_in->in_cid_len == 0 &&
173 ssl->transform_in->out_cid_len == 0 )
174 {
175 return( 0 );
176 }
177
178 *peer_cid_len = ssl->transform_in->out_cid_len;
179 memcpy( peer_cid, ssl->transform_in->out_cid,
180 ssl->transform_in->out_cid_len );
181
182 *enabled = MBEDTLS_SSL_CID_ENABLED;
183
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100184 return( 0 );
185}
Hanno Becker35c36a62019-04-23 12:31:42 +0100186#endif /* MBEDTLS_SSL_CID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100187
Hanno Beckerd5847772018-08-28 10:09:23 +0100188/* Forward declarations for functions related to message buffering. */
189static void ssl_buffering_free( mbedtls_ssl_context *ssl );
190static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
191 uint8_t slot );
192static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
193static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
194static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
195static int ssl_buffer_message( mbedtls_ssl_context *ssl );
196static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100197static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100198
Hanno Beckera67dee22018-08-22 10:05:20 +0100199static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100200static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100201{
Hanno Becker11682cc2018-08-22 14:41:02 +0100202 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100203
204 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100205 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100206
207 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
208}
209
Hanno Becker67bc7c32018-08-06 11:33:50 +0100210static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
211{
Hanno Becker11682cc2018-08-22 14:41:02 +0100212 size_t const bytes_written = ssl->out_left;
213 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100214
215 /* Double-check that the write-index hasn't gone
216 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100217 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100218 {
219 /* Should never happen... */
220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
221 }
222
223 return( (int) ( mtu - bytes_written ) );
224}
225
226static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
227{
228 int ret;
229 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400230 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100231
232#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
233 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
234
235 if( max_len > mfl )
236 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100237
238 /* By the standard (RFC 6066 Sect. 4), the MFL extension
239 * only limits the maximum record payload size, so in theory
240 * we would be allowed to pack multiple records of payload size
241 * MFL into a single datagram. However, this would mean that there's
242 * no way to explicitly communicate MTU restrictions to the peer.
243 *
244 * The following reduction of max_len makes sure that we never
245 * write datagrams larger than MFL + Record Expansion Overhead.
246 */
247 if( max_len <= ssl->out_left )
248 return( 0 );
249
250 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100251#endif
252
253 ret = ssl_get_remaining_space_in_datagram( ssl );
254 if( ret < 0 )
255 return( ret );
256 remaining = (size_t) ret;
257
258 ret = mbedtls_ssl_get_record_expansion( ssl );
259 if( ret < 0 )
260 return( ret );
261 expansion = (size_t) ret;
262
263 if( remaining <= expansion )
264 return( 0 );
265
266 remaining -= expansion;
267 if( remaining >= max_len )
268 remaining = max_len;
269
270 return( (int) remaining );
271}
272
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200273/*
274 * Double the retransmit timeout value, within the allowed range,
275 * returning -1 if the maximum value has already been reached.
276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200278{
279 uint32_t new_timeout;
280
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200281 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200282 return( -1 );
283
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200284 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
285 * in the following way: after the initial transmission and a first
286 * retransmission, back off to a temporary estimated MTU of 508 bytes.
287 * This value is guaranteed to be deliverable (if not guaranteed to be
288 * delivered) of any compliant IPv4 (and IPv6) network, and should work
289 * on most non-IP stacks too. */
290 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400291 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200292 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
294 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200295
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200296 new_timeout = 2 * ssl->handshake->retransmit_timeout;
297
298 /* Avoid arithmetic overflow and range overflow */
299 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200300 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200301 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200302 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200303 }
304
305 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200307 ssl->handshake->retransmit_timeout ) );
308
309 return( 0 );
310}
311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200313{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200314 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200316 ssl->handshake->retransmit_timeout ) );
317}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200321/*
322 * Convert max_fragment_length codes to length.
323 * RFC 6066 says:
324 * enum{
325 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
326 * } MaxFragmentLength;
327 * and we add 0 -> extension unused
328 */
Angus Grattond8213d02016-05-25 20:56:48 +1000329static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200330{
Angus Grattond8213d02016-05-25 20:56:48 +1000331 switch( mfl )
332 {
333 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
334 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
335 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
336 return 512;
337 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
338 return 1024;
339 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
340 return 2048;
341 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
342 return 4096;
343 default:
344 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
345 }
346}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200348
Hanno Becker52055ae2019-02-06 14:30:46 +0000349int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
350 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200351{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_ssl_session_free( dst );
353 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000356
357#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200358 if( src->peer_cert != NULL )
359 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200360 int ret;
361
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200362 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200363 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200364 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200369 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200372 dst->peer_cert = NULL;
373 return( ret );
374 }
375 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000376#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000377 if( src->peer_cert_digest != NULL )
378 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000379 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000380 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000381 if( dst->peer_cert_digest == NULL )
382 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
383
384 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
385 src->peer_cert_digest_len );
386 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000387 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000388 }
389#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200393#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200394 if( src->ticket != NULL )
395 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200396 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200397 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200398 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200399
400 memcpy( dst->ticket, src->ticket, src->ticket_len );
401 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200402#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200403
404 return( 0 );
405}
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
408int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200409 const unsigned char *key_enc, const unsigned char *key_dec,
410 size_t keylen,
411 const unsigned char *iv_enc, const unsigned char *iv_dec,
412 size_t ivlen,
413 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200414 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
416int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
417int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
418int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
419int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
420#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000421
Paul Bakker5121ce52009-01-03 21:22:43 +0000422/*
423 * Key material generation
424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200426static int ssl3_prf( const unsigned char *secret, size_t slen,
427 const char *label,
428 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000429 unsigned char *dstbuf, size_t dlen )
430{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100431 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000432 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200433 mbedtls_md5_context md5;
434 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000435 unsigned char padding[16];
436 unsigned char sha1sum[20];
437 ((void)label);
438
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200439 mbedtls_md5_init( &md5 );
440 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200441
Paul Bakker5f70b252012-09-13 14:23:06 +0000442 /*
443 * SSLv3:
444 * block =
445 * MD5( secret + SHA1( 'A' + secret + random ) ) +
446 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
447 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
448 * ...
449 */
450 for( i = 0; i < dlen / 16; i++ )
451 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200452 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000453
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100454 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100455 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100456 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100457 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100458 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100459 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100460 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100461 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100462 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100463 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000464
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100465 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100466 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100467 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100468 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100469 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100470 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100471 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100472 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000473 }
474
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100475exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200476 mbedtls_md5_free( &md5 );
477 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000478
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500479 mbedtls_platform_zeroize( padding, sizeof( padding ) );
480 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000481
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100482 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200487static int tls1_prf( const unsigned char *secret, size_t slen,
488 const char *label,
489 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000490 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000491{
Paul Bakker23986e52011-04-24 08:57:21 +0000492 size_t nb, hs;
493 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200494 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300495 unsigned char *tmp;
496 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 const mbedtls_md_info_t *md_info;
499 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100500 int ret;
501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
Ron Eldor3b350852019-05-07 18:31:49 +0300504 tmp_len = 20 + strlen( label ) + rlen;
505 tmp = mbedtls_calloc( 1, tmp_len );
506 if( tmp == NULL )
507 {
508 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
509 goto exit;
510 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
512 hs = ( slen + 1 ) / 2;
513 S1 = secret;
514 S2 = secret + slen - hs;
515
516 nb = strlen( label );
517 memcpy( tmp + 20, label, nb );
518 memcpy( tmp + 20 + nb, random, rlen );
519 nb += rlen;
520
521 /*
522 * First compute P_md5(secret,label+random)[0..dlen]
523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300525 {
526 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
527 goto exit;
528 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300531 {
532 goto exit;
533 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
536 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
537 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
539 for( i = 0; i < dlen; i += 16 )
540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_md_hmac_reset ( &md_ctx );
542 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
543 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_md_hmac_reset ( &md_ctx );
546 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
547 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
550
551 for( j = 0; j < k; j++ )
552 dstbuf[i + j] = h_i[j];
553 }
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100556
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 /*
558 * XOR out with P_sha1(secret,label+random)[0..dlen]
559 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300561 {
562 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
563 goto exit;
564 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300567 {
568 goto exit;
569 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
572 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
573 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
575 for( i = 0; i < dlen; i += 20 )
576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_md_hmac_reset ( &md_ctx );
578 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
579 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_md_hmac_reset ( &md_ctx );
582 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
583 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
585 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
586
587 for( j = 0; j < k; j++ )
588 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
589 }
590
Ron Eldor3b350852019-05-07 18:31:49 +0300591exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100593
Ron Eldor3b350852019-05-07 18:31:49 +0300594 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500595 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Ron Eldor3b350852019-05-07 18:31:49 +0300597 mbedtls_free( tmp );
598 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000599}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500603#if defined(MBEDTLS_USE_PSA_CRYPTO)
604static int tls_prf_generic( mbedtls_md_type_t md_type,
605 const unsigned char *secret, size_t slen,
606 const char *label,
607 const unsigned char *random, size_t rlen,
608 unsigned char *dstbuf, size_t dlen )
609{
610 psa_status_t status;
611 psa_algorithm_t alg;
612 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500613 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500614 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
615
Andrzej Kurek2f760752019-01-28 08:08:15 -0500616 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500617 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500618
Andrzej Kurekc929a822019-01-14 03:51:11 -0500619 if( md_type == MBEDTLS_MD_SHA384 )
620 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
621 else
622 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
623
Andrzej Kurek2f760752019-01-28 08:08:15 -0500624 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500625 psa_key_policy_set_usage( &policy,
626 PSA_KEY_USAGE_DERIVE,
627 alg );
628 status = psa_set_key_policy( master_slot, &policy );
629 if( status != PSA_SUCCESS )
630 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
631
632 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
633 if( status != PSA_SUCCESS )
634 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
635
636 status = psa_key_derivation( &generator,
637 master_slot, alg,
638 random, rlen,
639 (unsigned char const *) label,
640 (size_t) strlen( label ),
641 dlen );
642 if( status != PSA_SUCCESS )
643 {
644 psa_generator_abort( &generator );
645 psa_destroy_key( master_slot );
646 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
647 }
648
649 status = psa_generator_read( &generator, dstbuf, dlen );
650 if( status != PSA_SUCCESS )
651 {
652 psa_generator_abort( &generator );
653 psa_destroy_key( master_slot );
654 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
655 }
656
657 status = psa_generator_abort( &generator );
658 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500659 {
660 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500661 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500662 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500663
664 status = psa_destroy_key( master_slot );
665 if( status != PSA_SUCCESS )
666 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
667
Andrzej Kurek33171262019-01-15 03:25:18 -0500668 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500669}
670
671#else /* MBEDTLS_USE_PSA_CRYPTO */
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100674 const unsigned char *secret, size_t slen,
675 const char *label,
676 const unsigned char *random, size_t rlen,
677 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000678{
679 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100680 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300681 unsigned char *tmp;
682 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
684 const mbedtls_md_info_t *md_info;
685 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100686 int ret;
687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
691 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100694
Ron Eldor3b350852019-05-07 18:31:49 +0300695 tmp_len = md_len + strlen( label ) + rlen;
696 tmp = mbedtls_calloc( 1, tmp_len );
697 if( tmp == NULL )
698 {
699 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
700 goto exit;
701 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000702
703 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100704 memcpy( tmp + md_len, label, nb );
705 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000706 nb += rlen;
707
708 /*
709 * Compute P_<hash>(secret, label + random)[0..dlen]
710 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300712 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
715 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
716 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100717
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100718 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 mbedtls_md_hmac_reset ( &md_ctx );
721 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
722 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_md_hmac_reset ( &md_ctx );
725 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
726 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000727
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100728 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000729
730 for( j = 0; j < k; j++ )
731 dstbuf[i + j] = h_i[j];
732 }
733
Ron Eldor3b350852019-05-07 18:31:49 +0300734exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100736
Ron Eldor3b350852019-05-07 18:31:49 +0300737 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500738 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000739
Ron Eldor3b350852019-05-07 18:31:49 +0300740 mbedtls_free( tmp );
741
742 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000743}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500744#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100746static int tls_prf_sha256( const unsigned char *secret, size_t slen,
747 const char *label,
748 const unsigned char *random, size_t rlen,
749 unsigned char *dstbuf, size_t dlen )
750{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100752 label, random, rlen, dstbuf, dlen ) );
753}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200757static int tls_prf_sha384( const unsigned char *secret, size_t slen,
758 const char *label,
759 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000760 unsigned char *dstbuf, size_t dlen )
761{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100763 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000764}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#endif /* MBEDTLS_SHA512_C */
766#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
771 defined(MBEDTLS_SSL_PROTO_TLS1_1)
772static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200773#endif
Paul Bakker380da532012-04-18 16:10:25 +0000774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775#if defined(MBEDTLS_SSL_PROTO_SSL3)
776static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
777static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200778#endif
779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
781static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
782static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200783#endif
784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
786#if defined(MBEDTLS_SHA256_C)
787static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
788static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
789static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200790#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#if defined(MBEDTLS_SHA512_C)
793static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
794static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
795static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100796#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000798
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100799#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100800 defined(MBEDTLS_USE_PSA_CRYPTO)
801static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
802{
803 if( ssl->conf->f_psk != NULL )
804 {
805 /* If we've used a callback to select the PSK,
806 * the static configuration is irrelevant. */
807 if( ssl->handshake->psk_opaque != 0 )
808 return( 1 );
809
810 return( 0 );
811 }
812
813 if( ssl->conf->psk_opaque != 0 )
814 return( 1 );
815
816 return( 0 );
817}
818#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100819 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100820
Ron Eldorcf280092019-05-14 20:19:13 +0300821#if defined(MBEDTLS_SSL_EXPORT_KEYS)
822static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
823{
824#if defined(MBEDTLS_SSL_PROTO_SSL3)
825 if( tls_prf == ssl3_prf )
826 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300827 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300828 }
829 else
830#endif
831#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
832 if( tls_prf == tls1_prf )
833 {
834 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
835 }
836 else
837#endif
838#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
839#if defined(MBEDTLS_SHA512_C)
840 if( tls_prf == tls_prf_sha384 )
841 {
842 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
843 }
844 else
845#endif
846#if defined(MBEDTLS_SHA256_C)
847 if( tls_prf == tls_prf_sha256 )
848 {
849 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
850 }
851 else
852#endif
853#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
854 return( MBEDTLS_SSL_TLS_PRF_NONE );
855}
856#endif /* MBEDTLS_SSL_EXPORT_KEYS */
857
Ron Eldor51d3ab52019-05-12 14:54:30 +0300858int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
859 const unsigned char *secret, size_t slen,
860 const char *label,
861 const unsigned char *random, size_t rlen,
862 unsigned char *dstbuf, size_t dlen )
863{
864 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
865
866 switch( prf )
867 {
868#if defined(MBEDTLS_SSL_PROTO_SSL3)
869 case MBEDTLS_SSL_TLS_PRF_SSL3:
870 tls_prf = ssl3_prf;
871 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300872#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300873#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
874 case MBEDTLS_SSL_TLS_PRF_TLS1:
875 tls_prf = tls1_prf;
876 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300877#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
878
879#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300880#if defined(MBEDTLS_SHA512_C)
881 case MBEDTLS_SSL_TLS_PRF_SHA384:
882 tls_prf = tls_prf_sha384;
883 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300884#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300885#if defined(MBEDTLS_SHA256_C)
886 case MBEDTLS_SSL_TLS_PRF_SHA256:
887 tls_prf = tls_prf_sha256;
888 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300889#endif /* MBEDTLS_SHA256_C */
890#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300891 default:
892 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
893 }
894
895 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
896}
897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000899{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200900 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000901#if defined(MBEDTLS_USE_PSA_CRYPTO)
902 int psa_fallthrough;
903#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000904 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000905 unsigned char keyblk[256];
906 unsigned char *key1;
907 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100908 unsigned char *mac_enc;
909 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000910 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200911 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000912 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000913 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 const mbedtls_cipher_info_t *cipher_info;
915 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100916
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000917 /* cf. RFC 5246, Section 8.1:
918 * "The master secret is always exactly 48 bytes in length." */
919 size_t const master_secret_len = 48;
920
Hanno Becker35b23c72018-10-23 12:10:41 +0100921#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
922 unsigned char session_hash[48];
923#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 mbedtls_ssl_session *session = ssl->session_negotiate;
926 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
927 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000930
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000931#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
932 transform->encrypt_then_mac = session->encrypt_then_mac;
933#endif
934 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000935
936 ciphersuite_info = handshake->ciphersuite_info;
937 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100938 if( cipher_info == NULL )
939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000941 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100943 }
944
Hanno Beckere694c3e2017-12-27 21:34:08 +0000945 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100946 if( md_info == NULL )
947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000949 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100951 }
952
Hanno Becker4bf74652019-04-26 16:22:27 +0100953#if defined(MBEDTLS_SSL_CID)
954 /* Copy own and peer's CID if the use of the CID
955 * extension has been negotiated. */
956 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
957 {
958 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +0100959
960 /* Uncomment this once CID-parsing and support for a change
961 * record content type during record decryption are added. */
962 /* transform->in_cid_len = ssl->own_cid_len; */
963 /* transform->out_cid_len = ssl->handshake->peer_cid_len; */
964 /* memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); */
965 /* memcpy( transform->out_cid, ssl->handshake->peer_cid, */
966 /* ssl->handshake->peer_cid_len ); */
Hanno Becker4bf74652019-04-26 16:22:27 +0100967
968 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
969 transform->out_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +0100970 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +0100971 transform->in_cid_len );
972 }
973#endif /* MBEDTLS_SSL_CID */
974
Paul Bakker5121ce52009-01-03 21:22:43 +0000975 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000976 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000977 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978#if defined(MBEDTLS_SSL_PROTO_SSL3)
979 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000980 {
Paul Bakker48916f92012-09-16 19:57:18 +0000981 handshake->tls_prf = ssl3_prf;
982 handshake->calc_verify = ssl_calc_verify_ssl;
983 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000984 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200985 else
986#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
988 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000989 {
Paul Bakker48916f92012-09-16 19:57:18 +0000990 handshake->tls_prf = tls1_prf;
991 handshake->calc_verify = ssl_calc_verify_tls;
992 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000993 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200994 else
995#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
997#if defined(MBEDTLS_SHA512_C)
998 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +0000999 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001000 {
Paul Bakker48916f92012-09-16 19:57:18 +00001001 handshake->tls_prf = tls_prf_sha384;
1002 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1003 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001004 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001005 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001006#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007#if defined(MBEDTLS_SHA256_C)
1008 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001009 {
Paul Bakker48916f92012-09-16 19:57:18 +00001010 handshake->tls_prf = tls_prf_sha256;
1011 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1012 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001013 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001014 else
1015#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1019 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001020 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001021
1022 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001023 * SSLv3:
1024 * master =
1025 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1026 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1027 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001028 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001029 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 * master = PRF( premaster, "master secret", randbytes )[0..47]
1031 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001032 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001034 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1035 }
1036 else
1037 {
1038 /* The label for the KDF used for key expansion.
1039 * This is either "master secret" or "extended master secret"
1040 * depending on whether the Extended Master Secret extension
1041 * is used. */
1042 char const *lbl = "master secret";
1043
1044 /* The salt for the KDF used for key expansion.
1045 * - If the Extended Master Secret extension is not used,
1046 * this is ClientHello.Random + ServerHello.Random
1047 * (see Sect. 8.1 in RFC 5246).
1048 * - If the Extended Master Secret extension is used,
1049 * this is the transcript of the handshake so far.
1050 * (see Sect. 4 in RFC 7627). */
1051 unsigned char const *salt = handshake->randbytes;
1052 size_t salt_len = 64;
1053
1054#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001058
Hanno Becker35b23c72018-10-23 12:10:41 +01001059 lbl = "extended master secret";
1060 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001061 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1063 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001066 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1067 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001068 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001069 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001070 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001071#endif /* MBEDTLS_SHA512_C */
1072 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001073 }
1074 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001076 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001077
Hanno Becker35b23c72018-10-23 12:10:41 +01001078 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001079 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001080#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1081
Hanno Becker7d0a5692018-10-23 15:26:22 +01001082#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1083 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1084 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1085 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1086 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001087 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001088 /* Perform PSK-to-MS expansion in a single step. */
1089 psa_status_t status;
1090 psa_algorithm_t alg;
1091 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001092 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001093
1094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1095
1096 psk = ssl->conf->psk_opaque;
1097 if( ssl->handshake->psk_opaque != 0 )
1098 psk = ssl->handshake->psk_opaque;
1099
Hanno Becker22bf1452019-04-05 11:21:08 +01001100 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001101 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1102 else
1103 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1104
1105 status = psa_key_derivation( &generator, psk, alg,
1106 salt, salt_len,
1107 (unsigned char const *) lbl,
1108 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001109 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001110 if( status != PSA_SUCCESS )
1111 {
1112 psa_generator_abort( &generator );
1113 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1114 }
1115
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001116 status = psa_generator_read( &generator, session->master,
1117 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001118 if( status != PSA_SUCCESS )
1119 {
1120 psa_generator_abort( &generator );
1121 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1122 }
1123
1124 status = psa_generator_abort( &generator );
1125 if( status != PSA_SUCCESS )
1126 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001127 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001128 else
1129#endif
1130 {
1131 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1132 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001133 session->master,
1134 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001135 if( ret != 0 )
1136 {
1137 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1138 return( ret );
1139 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001140
Hanno Becker7d0a5692018-10-23 15:26:22 +01001141 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1142 handshake->premaster,
1143 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001144
Hanno Becker7d0a5692018-10-23 15:26:22 +01001145 mbedtls_platform_zeroize( handshake->premaster,
1146 sizeof(handshake->premaster) );
1147 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001148 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001149
1150 /*
1151 * Swap the client and server random values.
1152 */
Paul Bakker48916f92012-09-16 19:57:18 +00001153 memcpy( tmp, handshake->randbytes, 64 );
1154 memcpy( handshake->randbytes, tmp + 32, 32 );
1155 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001156 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001157
1158 /*
1159 * SSLv3:
1160 * key block =
1161 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1162 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1163 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1164 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1165 * ...
1166 *
1167 * TLSv1:
1168 * key block = PRF( master, "key expansion", randbytes )
1169 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001170 ret = handshake->tls_prf( session->master, 48, "key expansion",
1171 handshake->randbytes, 64, keyblk, 256 );
1172 if( ret != 0 )
1173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001175 return( ret );
1176 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1179 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1180 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1181 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1182 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001183
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 /*
1185 * Determine the appropriate key, IV and MAC length.
1186 */
Paul Bakker68884e32013-01-07 18:20:04 +01001187
Hanno Becker88aaf652017-12-27 08:17:40 +00001188 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001189
Hanno Becker8031d062018-01-03 15:32:31 +00001190#if defined(MBEDTLS_GCM_C) || \
1191 defined(MBEDTLS_CCM_C) || \
1192 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001194 cipher_info->mode == MBEDTLS_MODE_CCM ||
1195 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001197 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001198
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001199 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001200 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001201 transform->taglen =
1202 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001203
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001204 /* All modes haves 96-bit IVs;
1205 * GCM and CCM has 4 implicit and 8 explicit bytes
1206 * ChachaPoly has all 12 bytes implicit
1207 */
Paul Bakker68884e32013-01-07 18:20:04 +01001208 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001209 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1210 transform->fixed_ivlen = 12;
1211 else
1212 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001213
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001214 /* Minimum length of encrypted record */
1215 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001216 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001217 }
1218 else
Hanno Becker8031d062018-01-03 15:32:31 +00001219#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1220#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1221 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1222 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001223 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001224 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1226 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001229 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001230 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001231
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001232 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001233 mac_key_len = mbedtls_md_get_size( md_info );
1234 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001237 /*
1238 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1239 * (rfc 6066 page 13 or rfc 2104 section 4),
1240 * so we only need to adjust the length here.
1241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001245
1246#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1247 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001248 * HMAC implementation which also truncates the key
1249 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001250 mac_key_len = transform->maclen;
1251#endif
1252 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001254
1255 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001256 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001257
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001258 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001260 transform->minlen = transform->maclen;
1261 else
Paul Bakker68884e32013-01-07 18:20:04 +01001262 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001263 /*
1264 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001265 * 1. if EtM is in use: one block plus MAC
1266 * otherwise: * first multiple of blocklen greater than maclen
1267 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001268 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1270 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001271 {
1272 transform->minlen = transform->maclen
1273 + cipher_info->block_size;
1274 }
1275 else
1276#endif
1277 {
1278 transform->minlen = transform->maclen
1279 + cipher_info->block_size
1280 - transform->maclen % cipher_info->block_size;
1281 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1284 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1285 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001286 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001287 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001288#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1290 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1291 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001292 {
1293 transform->minlen += transform->ivlen;
1294 }
1295 else
1296#endif
1297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001299 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1300 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001301 }
Paul Bakker68884e32013-01-07 18:20:04 +01001302 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001303 }
Hanno Becker8031d062018-01-03 15:32:31 +00001304 else
1305#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1306 {
1307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1308 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1309 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001310
Hanno Becker88aaf652017-12-27 08:17:40 +00001311 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1312 (unsigned) keylen,
1313 (unsigned) transform->minlen,
1314 (unsigned) transform->ivlen,
1315 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001316
1317 /*
1318 * Finally setup the cipher contexts, IVs and MAC secrets.
1319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001321 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001323 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001324 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001325
Paul Bakker68884e32013-01-07 18:20:04 +01001326 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001327 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001329 /*
1330 * This is not used in TLS v1.1.
1331 */
Paul Bakker48916f92012-09-16 19:57:18 +00001332 iv_copy_len = ( transform->fixed_ivlen ) ?
1333 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001334 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1335 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001336 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 }
1338 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#endif /* MBEDTLS_SSL_CLI_C */
1340#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001341 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001343 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001344 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001345
Hanno Becker81c7b182017-11-09 18:39:33 +00001346 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001347 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001349 /*
1350 * This is not used in TLS v1.1.
1351 */
Paul Bakker48916f92012-09-16 19:57:18 +00001352 iv_copy_len = ( transform->fixed_ivlen ) ?
1353 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001354 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1355 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001356 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001358 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001362 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1363 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001364 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001365
Hanno Beckerd56ed242018-01-03 15:32:51 +00001366#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_SSL_PROTO_SSL3)
1368 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001369 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001370 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001373 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1374 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001375 }
1376
Hanno Becker81c7b182017-11-09 18:39:33 +00001377 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1378 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001379 }
1380 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1382#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1383 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1384 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001385 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001386 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1387 For AEAD-based ciphersuites, there is nothing to do here. */
1388 if( mac_key_len != 0 )
1389 {
1390 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1391 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1392 }
Paul Bakker68884e32013-01-07 18:20:04 +01001393 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001394 else
1395#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001398 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1399 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001400 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001401#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1404 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001405 {
1406 int ret = 0;
1407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001409
Hanno Becker88aaf652017-12-27 08:17:40 +00001410 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001411 transform->iv_enc, transform->iv_dec,
1412 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001413 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001414 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001417 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1418 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001419 }
1420 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001421#else
1422 ((void) mac_dec);
1423 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001425
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001426#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1427 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001428 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001429 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1430 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001431 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001432 iv_copy_len );
1433 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001434
1435 if( ssl->conf->f_export_keys_ext != NULL )
1436 {
1437 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1438 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001439 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001440 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001441 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001442 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001443 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001444 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001445#endif
1446
Hanno Beckerf704bef2018-11-16 15:21:18 +00001447#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001448
1449 /* Only use PSA-based ciphers for TLS-1.2.
1450 * That's relevant at least for TLS-1.0, where
1451 * we assume that mbedtls_cipher_crypt() updates
1452 * the structure field for the IV, which the PSA-based
1453 * implementation currently doesn't. */
1454#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1455 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001456 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001457 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001458 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001459 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1460 {
1461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001462 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001463 }
1464
1465 if( ret == 0 )
1466 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001467 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001468 psa_fallthrough = 0;
1469 }
1470 else
1471 {
1472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1473 psa_fallthrough = 1;
1474 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001475 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001476 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001477 psa_fallthrough = 1;
1478#else
1479 psa_fallthrough = 1;
1480#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001481
Hanno Beckercb1cc802018-11-17 22:27:38 +00001482 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001483#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001484 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001485 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001486 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001487 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001488 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001489 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001490
Hanno Beckerf704bef2018-11-16 15:21:18 +00001491#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001492 /* Only use PSA-based ciphers for TLS-1.2.
1493 * That's relevant at least for TLS-1.0, where
1494 * we assume that mbedtls_cipher_crypt() updates
1495 * the structure field for the IV, which the PSA-based
1496 * implementation currently doesn't. */
1497#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1498 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001499 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001500 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001501 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001502 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1503 {
1504 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001505 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001506 }
1507
1508 if( ret == 0 )
1509 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001510 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001511 psa_fallthrough = 0;
1512 }
1513 else
1514 {
1515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1516 psa_fallthrough = 1;
1517 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001518 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001519 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001520 psa_fallthrough = 1;
1521#else
1522 psa_fallthrough = 1;
1523#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001524
Hanno Beckercb1cc802018-11-17 22:27:38 +00001525 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001526#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001527 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001528 cipher_info ) ) != 0 )
1529 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001530 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001531 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001532 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001535 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001539 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001540 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001543 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001547 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001548 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550#if defined(MBEDTLS_CIPHER_MODE_CBC)
1551 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1554 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001557 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001558 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1561 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001564 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001565 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001568
Paul Bakker5121ce52009-01-03 21:22:43 +00001569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001571 // Initialize compression
1572 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001574 {
Paul Bakker16770332013-10-11 09:59:44 +02001575 if( ssl->compress_buf == NULL )
1576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001578 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001579 if( ssl->compress_buf == NULL )
1580 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001582 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001583 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1584 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001585 }
1586 }
1587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001589
Paul Bakker48916f92012-09-16 19:57:18 +00001590 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1591 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001592
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001593 if( deflateInit( &transform->ctx_deflate,
1594 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001595 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001598 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1599 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001600 }
1601 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001605end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001606 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1607 mbedtls_platform_zeroize( handshake->randbytes,
1608 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001609 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001610}
1611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612#if defined(MBEDTLS_SSL_PROTO_SSL3)
1613void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001614{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001615 mbedtls_md5_context md5;
1616 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001617 unsigned char pad_1[48];
1618 unsigned char pad_2[48];
1619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001621
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001622 mbedtls_md5_init( &md5 );
1623 mbedtls_sha1_init( &sha1 );
1624
1625 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1626 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001627
Paul Bakker380da532012-04-18 16:10:25 +00001628 memset( pad_1, 0x36, 48 );
1629 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001630
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001631 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1632 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1633 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001634
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001635 mbedtls_md5_starts_ret( &md5 );
1636 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1637 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1638 mbedtls_md5_update_ret( &md5, hash, 16 );
1639 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001640
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001641 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1642 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1643 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001644
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001645 mbedtls_sha1_starts_ret( &sha1 );
1646 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1647 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1648 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1649 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001653
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001654 mbedtls_md5_free( &md5 );
1655 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001656
Paul Bakker380da532012-04-18 16:10:25 +00001657 return;
1658}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1662void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001663{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001664 mbedtls_md5_context md5;
1665 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001668
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001669 mbedtls_md5_init( &md5 );
1670 mbedtls_sha1_init( &sha1 );
1671
1672 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1673 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001674
Andrzej Kurekeb342242019-01-29 09:14:33 -05001675 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001676 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001680
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001681 mbedtls_md5_free( &md5 );
1682 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001683
Paul Bakker380da532012-04-18 16:10:25 +00001684 return;
1685}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1689#if defined(MBEDTLS_SHA256_C)
1690void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001691{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001692#if defined(MBEDTLS_USE_PSA_CRYPTO)
1693 size_t hash_size;
1694 psa_status_t status;
1695 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1696
1697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1698 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1699 if( status != PSA_SUCCESS )
1700 {
1701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1702 return;
1703 }
1704
1705 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1706 if( status != PSA_SUCCESS )
1707 {
1708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1709 return;
1710 }
1711 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1713#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001714 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001715
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001716 mbedtls_sha256_init( &sha256 );
1717
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001719
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001720 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001721 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001725
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001726 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001727#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001728 return;
1729}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732#if defined(MBEDTLS_SHA512_C)
1733void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001734{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001735#if defined(MBEDTLS_USE_PSA_CRYPTO)
1736 size_t hash_size;
1737 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001738 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001739
1740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001741 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001742 if( status != PSA_SUCCESS )
1743 {
1744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1745 return;
1746 }
1747
Andrzej Kurek972fba52019-01-30 03:29:12 -05001748 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001749 if( status != PSA_SUCCESS )
1750 {
1751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1752 return;
1753 }
1754 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1756#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001757 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001758
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001759 mbedtls_sha512_init( &sha512 );
1760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001762
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001763 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001764 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001768
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001769 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001770#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001771 return;
1772}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#endif /* MBEDTLS_SHA512_C */
1774#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1777int 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 +02001778{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001779 unsigned char *p = ssl->handshake->premaster;
1780 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001781 const unsigned char *psk = ssl->conf->psk;
1782 size_t psk_len = ssl->conf->psk_len;
1783
1784 /* If the psk callback was called, use its result */
1785 if( ssl->handshake->psk != NULL )
1786 {
1787 psk = ssl->handshake->psk;
1788 psk_len = ssl->handshake->psk_len;
1789 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001790
1791 /*
1792 * PMS = struct {
1793 * opaque other_secret<0..2^16-1>;
1794 * opaque psk<0..2^16-1>;
1795 * };
1796 * with "other_secret" depending on the particular key exchange
1797 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1799 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001800 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001801 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001803
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001804 *(p++) = (unsigned char)( psk_len >> 8 );
1805 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001806
1807 if( end < p || (size_t)( end - p ) < psk_len )
1808 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1809
1810 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001811 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001812 }
1813 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1815#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1816 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001817 {
1818 /*
1819 * other_secret already set by the ClientKeyExchange message,
1820 * and is 48 bytes long
1821 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001822 if( end - p < 2 )
1823 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1824
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001825 *p++ = 0;
1826 *p++ = 48;
1827 p += 48;
1828 }
1829 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1831#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1832 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001833 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001834 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001835 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001836
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001837 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001839 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001840 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001843 return( ret );
1844 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001845 *(p++) = (unsigned char)( len >> 8 );
1846 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001847 p += len;
1848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001850 }
1851 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1853#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1854 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001855 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001856 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001857 size_t zlen;
1858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001860 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001861 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001864 return( ret );
1865 }
1866
1867 *(p++) = (unsigned char)( zlen >> 8 );
1868 *(p++) = (unsigned char)( zlen );
1869 p += zlen;
1870
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001871 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1872 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001873 }
1874 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1878 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001879 }
1880
1881 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001882 if( end - p < 2 )
1883 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001884
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001885 *(p++) = (unsigned char)( psk_len >> 8 );
1886 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001887
1888 if( end < p || (size_t)( end - p ) < psk_len )
1889 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1890
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001891 memcpy( p, psk, psk_len );
1892 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001893
1894 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1895
1896 return( 0 );
1897}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001901/*
1902 * SSLv3.0 MAC functions
1903 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001904#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001905static void ssl_mac( mbedtls_md_context_t *md_ctx,
1906 const unsigned char *secret,
1907 const unsigned char *buf, size_t len,
1908 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001909 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001910{
1911 unsigned char header[11];
1912 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001913 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1915 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001916
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001917 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001919 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001920 else
Paul Bakker68884e32013-01-07 18:20:04 +01001921 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001922
1923 memcpy( header, ctr, 8 );
1924 header[ 8] = (unsigned char) type;
1925 header[ 9] = (unsigned char)( len >> 8 );
1926 header[10] = (unsigned char)( len );
1927
Paul Bakker68884e32013-01-07 18:20:04 +01001928 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929 mbedtls_md_starts( md_ctx );
1930 mbedtls_md_update( md_ctx, secret, md_size );
1931 mbedtls_md_update( md_ctx, padding, padlen );
1932 mbedtls_md_update( md_ctx, header, 11 );
1933 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001934 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001935
Paul Bakker68884e32013-01-07 18:20:04 +01001936 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 mbedtls_md_starts( md_ctx );
1938 mbedtls_md_update( md_ctx, secret, md_size );
1939 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001940 mbedtls_md_update( md_ctx, out, md_size );
1941 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001942}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001944
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001945/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001946 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001947#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001948 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1949 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1950 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1951/* This function makes sure every byte in the memory region is accessed
1952 * (in ascending addresses order) */
1953static void ssl_read_memory( unsigned char *p, size_t len )
1954{
1955 unsigned char acc = 0;
1956 volatile unsigned char force;
1957
1958 for( ; len != 0; p++, len-- )
1959 acc ^= *p;
1960
1961 force = acc;
1962 (void) force;
1963}
1964#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1965
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001966/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001967 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001968 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001969
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001970#if defined(MBEDTLS_SSL_CID)
1971/* This functions transforms a pair of a DTLS plaintext fragment
1972 * and a record content type into an instance of the DTLSInnerPlaintext
1973 * structure:
1974 *
1975 * struct {
1976 * opaque content[DTLSPlaintext.length];
1977 * ContentType real_type;
1978 * uint8 zeros[length_of_padding];
1979 * } DTLSInnerPlaintext;
1980 *
1981 * Input:
1982 * - `content`: The beginning of the buffer holding the
1983 * plaintext to be wrapped.
1984 * - `*content_size`: The length of the plaintext in Bytes.
1985 * - `max_len`: The number of Bytes available starting from
1986 * `content`. This must be `>= *content_size`.
1987 * - `rec_type`: The desired record content type.
1988 *
1989 * Output:
1990 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1991 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1992 *
1993 * Returns:
1994 * - `0` on success.
1995 * - A negative error code if `max_len` didn't offer enough space
1996 * for the expansion.
1997 */
1998static int ssl_cid_build_inner_plaintext( unsigned char *content,
1999 size_t *content_size,
2000 size_t remaining,
2001 uint8_t rec_type )
2002{
2003 size_t len = *content_size;
2004 size_t pad = ~len & 0xF; /* Pad to a multiple of 16 */
2005
2006 /* Write real content type */
2007 if( remaining == 0 )
2008 return( -1 );
2009 content[ len ] = rec_type;
2010 len++;
2011 remaining--;
2012
2013 if( remaining < pad )
2014 return( -1 );
2015 memset( content + len, 0, pad );
2016 len += pad;
2017 remaining -= pad;
2018
2019 *content_size = len;
2020 return( 0 );
2021}
2022
2023/* This function parses a DTLSInnerPlaintext structure
2024 *
2025 * struct {
2026 * opaque content[DTLSPlaintext.length];
2027 * ContentType real_type;
2028 * uint8 zeros[length_of_padding];
2029 * } DTLSInnerPlaintext;
2030 */
2031static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2032 size_t *content_size,
2033 uint8_t *rec_type )
2034{
2035 size_t remaining = *content_size;
2036
2037 /* Determine length of padding by skipping zeroes from the back. */
2038 do
2039 {
2040 if( remaining == 0 )
2041 return( -1 );
2042 remaining--;
2043 } while( content[ remaining ] == 0 );
2044
2045 *content_size = remaining;
2046 *rec_type = content[ remaining ];
2047
2048 return( 0 );
2049}
2050#endif /* MBEDTLS_SSL_CID */
2051
Hanno Beckercab87e62019-04-29 13:52:53 +01002052/* add_data must have size ( 13 + SSL_CID_LEN_MAX ) Bytes */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002053static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002054 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002055 mbedtls_record *rec )
2056{
Hanno Beckercab87e62019-04-29 13:52:53 +01002057 /* Quoting RFC 5246:
2058 *
2059 * additional_data = seq_num + TLSCompressed.type +
2060 * TLSCompressed.version + TLSCompressed.length;
2061 *
2062 * For the CID extension, this is extended as follows:
2063 *
2064 * additional_data = seq_num + DTLSPlaintext.type +
2065 * DTLSPlaintext.version +
2066 * cid + // New input
2067 * cid_length + // New input
2068 * length_of_DTLSInnerPlaintext;
2069 */
2070
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002071 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2072 add_data[8] = rec->type;
Hanno Beckercab87e62019-04-29 13:52:53 +01002073 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
2074
2075#if defined(MBEDTLS_SSL_CID)
2076 memcpy( add_data + 11, rec->cid, rec->cid_len );
2077#endif /* MBEDTLS_SSL_CID */
2078
2079 add_data[11 + rec->cid_len + 0] = ( rec->data_len >> 8 ) & 0xFF;
2080 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 0 ) & 0xFF;
2081
2082#if defined(MBEDTLS_SSL_CID)
2083 *add_data_len = 13 + rec->cid_len;
2084#else
2085 *add_data_len = 13;
2086#endif /* MBEDTLS_SSL_CID */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002087}
2088
Hanno Beckera18d1322018-01-03 14:27:32 +00002089int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2090 mbedtls_ssl_transform *transform,
2091 mbedtls_record *rec,
2092 int (*f_rng)(void *, unsigned char *, size_t),
2093 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002094{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002096 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002097 unsigned char * data;
Hanno Beckercab87e62019-04-29 13:52:53 +01002098 unsigned char add_data[13 + SSL_CID_LEN_MAX ];
2099 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002100 size_t post_avail;
2101
2102 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002103#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002104 ((void) ssl);
2105#endif
2106
2107 /* The PRNG is used for dynamic IV generation that's used
2108 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2109#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2110 ( defined(MBEDTLS_AES_C) || \
2111 defined(MBEDTLS_ARIA_C) || \
2112 defined(MBEDTLS_CAMELLIA_C) ) && \
2113 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2114 ((void) f_rng);
2115 ((void) p_rng);
2116#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002119
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002120 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002121 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2123 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2124 }
2125 if( rec == NULL ||
2126 rec->buf == NULL ||
2127 rec->buf_len < rec->data_offset ||
Hanno Beckercab87e62019-04-29 13:52:53 +01002128 rec->buf_len - rec->data_offset < rec->data_len ||
2129 rec->cid_len != 0 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002130 {
2131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002133 }
2134
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002135 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002136 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002138 data, rec->data_len );
2139
2140 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2141
2142 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2143 {
2144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2145 (unsigned) rec->data_len,
2146 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2148 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002149
Hanno Beckercab87e62019-04-29 13:52:53 +01002150#if defined(MBEDTLS_SSL_CID)
2151 /*
2152 * Add CID information
2153 */
2154 rec->cid_len = transform->out_cid_len;
2155 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2156 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002157
2158 if( rec->cid_len != 0 )
2159 {
2160 /*
2161 * Wrap plaintext into DTLSInnerPlaintext structure
2162 *
2163 * struct {
2164 * opaque content[DTLSPlaintext.length];
2165 * ContentType real_type;
2166 * uint8 zeros[length_of_padding];
2167 * } DTLSInnerPlaintext;
2168 *
2169 * and change the record content type.
2170 *
2171 * The rest of the record encryption stays
2172 * unmodified (apart from the inclusion of
2173 * the CID into the additional data for the
2174 * record MAC).
2175 */
2176 if( ssl_cid_build_inner_plaintext( data,
2177 &rec->data_len,
2178 post_avail,
2179 rec->type ) != 0 )
2180 {
2181 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2182 }
2183
2184 rec->type = MBEDTLS_SSL_MSG_CID;
2185 }
Hanno Beckercab87e62019-04-29 13:52:53 +01002186#endif /* MBEDTLS_SSL_CID */
2187
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002188 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2189
Paul Bakker5121ce52009-01-03 21:22:43 +00002190 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002191 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 */
Hanno Becker52344c22018-01-03 15:24:20 +00002193#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 if( mode == MBEDTLS_MODE_STREAM ||
2195 ( mode == MBEDTLS_MODE_CBC
2196#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002197 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002198#endif
2199 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002200 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002201 if( post_avail < transform->maclen )
2202 {
2203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2204 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2205 }
2206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002208 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002209 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002210 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002211 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2212 data, rec->data_len, rec->ctr, rec->type, mac );
2213 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002214 }
2215 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002216#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2218 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002219 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002220 {
Hanno Becker992b6872017-11-09 18:57:39 +00002221 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2222
Hanno Beckercab87e62019-04-29 13:52:53 +01002223 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002224
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002225 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002226 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002227 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2228 data, rec->data_len );
2229 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2230 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2231
2232 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002233 }
2234 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002235#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2238 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002239 }
2240
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002241 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2242 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002243
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002244 rec->data_len += transform->maclen;
2245 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002246 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002247 }
Hanno Becker52344c22018-01-03 15:24:20 +00002248#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002249
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002250 /*
2251 * Encrypt
2252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2254 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002256 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002257 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002259 "including %d bytes of padding",
2260 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002262 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2263 transform->iv_enc, transform->ivlen,
2264 data, rec->data_len,
2265 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002268 return( ret );
2269 }
2270
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002271 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2274 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002275 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 }
Paul Bakker68884e32013-01-07 18:20:04 +01002277 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002279
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002280#if defined(MBEDTLS_GCM_C) || \
2281 defined(MBEDTLS_CCM_C) || \
2282 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002284 mode == MBEDTLS_MODE_CCM ||
2285 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002286 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002287 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002288 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002289 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002290
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002291 /* Check that there's space for both the authentication tag
2292 * and the explicit IV before and after the record content. */
2293 if( post_avail < transform->taglen ||
2294 rec->data_offset < explicit_iv_len )
2295 {
2296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2297 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2298 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002299
Paul Bakker68884e32013-01-07 18:20:04 +01002300 /*
2301 * Generate IV
2302 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002303 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2304 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002305 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002306 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002307 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2308 explicit_iv_len );
2309 /* Prefix record content with explicit IV. */
2310 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002311 }
2312 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2313 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002314 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002315 unsigned char i;
2316
2317 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2318
2319 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002320 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002321 }
2322 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002323 {
2324 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2326 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002327 }
2328
Hanno Beckercab87e62019-04-29 13:52:53 +01002329 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002330
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002331 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2332 iv, transform->ivlen );
2333 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002334 data - explicit_iv_len, explicit_iv_len );
2335 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002336 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002338 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002339 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002340
Paul Bakker68884e32013-01-07 18:20:04 +01002341 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002342 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002343 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002344
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002345 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002346 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002347 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002348 data, rec->data_len, /* source */
2349 data, &rec->data_len, /* destination */
2350 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002353 return( ret );
2354 }
2355
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002356 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2357 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002358
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002359 rec->data_len += transform->taglen + explicit_iv_len;
2360 rec->data_offset -= explicit_iv_len;
2361 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002362 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002363 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002364 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2366#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002367 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002369 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002370 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002371 size_t padlen, i;
2372 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002373
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002374 /* Currently we're always using minimal padding
2375 * (up to 255 bytes would be allowed). */
2376 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2377 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 padlen = 0;
2379
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002380 /* Check there's enough space in the buffer for the padding. */
2381 if( post_avail < padlen + 1 )
2382 {
2383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2384 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2385 }
2386
Paul Bakker5121ce52009-01-03 21:22:43 +00002387 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002388 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002389
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002390 rec->data_len += padlen + 1;
2391 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002394 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002395 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2396 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002397 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002398 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002399 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002400 if( f_rng == NULL )
2401 {
2402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2403 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2404 }
2405
2406 if( rec->data_offset < transform->ivlen )
2407 {
2408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2409 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2410 }
2411
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002412 /*
2413 * Generate IV
2414 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002415 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002416 if( ret != 0 )
2417 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002418
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002419 memcpy( data - transform->ivlen, transform->iv_enc,
2420 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002421
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002422 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002426 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002427 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002428 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002429
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002430 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2431 transform->iv_enc,
2432 transform->ivlen,
2433 data, rec->data_len,
2434 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002437 return( ret );
2438 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002439
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002440 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2443 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002444 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002447 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002448 {
2449 /*
2450 * Save IV in SSL3 and TLS1
2451 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002452 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2453 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002455 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002456#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002457 {
2458 data -= transform->ivlen;
2459 rec->data_offset -= transform->ivlen;
2460 rec->data_len += transform->ivlen;
2461 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002464 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002465 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002466 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2467
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002468 /*
2469 * MAC(MAC_write_key, seq_num +
2470 * TLSCipherText.type +
2471 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002472 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002473 * IV + // except for TLS 1.0
2474 * ENC(content + padding + padding_length));
2475 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002476
2477 if( post_avail < transform->maclen)
2478 {
2479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2480 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2481 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002482
Hanno Beckercab87e62019-04-29 13:52:53 +01002483 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002486 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002487 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002488
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002489 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002490 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002491 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2492 data, rec->data_len );
2493 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2494 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002495
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002496 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002497
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002498 rec->data_len += transform->maclen;
2499 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002500 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002501 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002503 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002504 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002506 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2509 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002510 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002511
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002512 /* Make extra sure authentication was performed, exactly once */
2513 if( auth_done != 1 )
2514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2516 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002517 }
2518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002520
2521 return( 0 );
2522}
2523
Hanno Beckera18d1322018-01-03 14:27:32 +00002524int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2525 mbedtls_ssl_transform *transform,
2526 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002527{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002528 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002530 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002531#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002532 size_t padlen = 0, correct = 1;
2533#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002534 unsigned char* data;
Hanno Beckercab87e62019-04-29 13:52:53 +01002535 unsigned char add_data[13 + SSL_CID_LEN_MAX ];
2536 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002537
Hanno Beckera18d1322018-01-03 14:27:32 +00002538#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002539 ((void) ssl);
2540#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002543 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002544 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2546 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2547 }
2548 if( rec == NULL ||
2549 rec->buf == NULL ||
2550 rec->buf_len < rec->data_offset ||
2551 rec->buf_len - rec->data_offset < rec->data_len )
2552 {
2553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002555 }
2556
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002557 data = rec->buf + rec->data_offset;
2558 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
Hanno Beckercab87e62019-04-29 13:52:53 +01002560#if defined(MBEDTLS_SSL_CID)
2561 /*
2562 * Match record's CID with incoming CID.
2563 */
2564
2565 /* TODO - Uncomment this once CID parsing is in place */
2566 /* if( rec->cid_len != transform->in_cid_len || */
2567 /* memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) */
2568 /* { */
2569 /* return( MBEDTLS_ERR_SSL_INVALID_RECORD ); */
2570 /* } */
2571 /* TODO - Remove this once CID parsing is in place */
2572 rec->cid_len = transform->in_cid_len;
2573 memcpy( rec->cid, transform->in_cid, transform->in_cid_len );
2574 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
2575#endif /* MBEDTLS_SSL_CID */
2576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2578 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002579 {
2580 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002581 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2582 transform->iv_dec,
2583 transform->ivlen,
2584 data, rec->data_len,
2585 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002588 return( ret );
2589 }
2590
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002591 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2594 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002595 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002596 }
Paul Bakker68884e32013-01-07 18:20:04 +01002597 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002599#if defined(MBEDTLS_GCM_C) || \
2600 defined(MBEDTLS_CCM_C) || \
2601 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002603 mode == MBEDTLS_MODE_CCM ||
2604 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002605 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002606 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002607 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002608
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002609 /*
2610 * Compute and update sizes
2611 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002612 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002615 "+ taglen (%d)", rec->data_len,
2616 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002618 }
Paul Bakker68884e32013-01-07 18:20:04 +01002619
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002620 /*
2621 * Prepare IV
2622 */
2623 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2624 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002625 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002626 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002627 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002628
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002629 }
2630 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2631 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002632 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002633 unsigned char i;
2634
2635 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2636
2637 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002638 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002639 }
2640 else
2641 {
2642 /* Reminder if we ever add an AEAD mode with a different size */
2643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2644 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2645 }
2646
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002647 data += explicit_iv_len;
2648 rec->data_offset += explicit_iv_len;
2649 rec->data_len -= explicit_iv_len + transform->taglen;
2650
Hanno Beckercab87e62019-04-29 13:52:53 +01002651 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002652 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002653 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002654
2655 memcpy( transform->iv_dec + transform->fixed_ivlen,
2656 data - explicit_iv_len, explicit_iv_len );
2657
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002658 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002659 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002660 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002661
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002662
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002663 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002664 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002665 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002666 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2667 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002668 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002669 data, rec->data_len,
2670 data, &olen,
2671 data + rec->data_len,
2672 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2677 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002678
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002679 return( ret );
2680 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002681 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002682
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002683 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2686 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002687 }
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002688
Paul Bakkerca4ab492012-04-18 14:23:57 +00002689 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2692#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002693 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002695 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002696 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002697
Paul Bakker5121ce52009-01-03 21:22:43 +00002698 /*
Paul Bakker45829992013-01-03 14:52:21 +01002699 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002702 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2703 {
2704 /* The ciphertext is prefixed with the CBC IV. */
2705 minlen += transform->ivlen;
2706 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002707#endif
Paul Bakker45829992013-01-03 14:52:21 +01002708
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002709 /* Size considerations:
2710 *
2711 * - The CBC cipher text must not be empty and hence
2712 * at least of size transform->ivlen.
2713 *
2714 * Together with the potential IV-prefix, this explains
2715 * the first of the two checks below.
2716 *
2717 * - The record must contain a MAC, either in plain or
2718 * encrypted, depending on whether Encrypt-then-MAC
2719 * is used or not.
2720 * - If it is, the message contains the IV-prefix,
2721 * the CBC ciphertext, and the MAC.
2722 * - If it is not, the padded plaintext, and hence
2723 * the CBC ciphertext, has at least length maclen + 1
2724 * because there is at least the padding length byte.
2725 *
2726 * As the CBC ciphertext is not empty, both cases give the
2727 * lower bound minlen + maclen + 1 on the record size, which
2728 * we test for in the second check below.
2729 */
2730 if( rec->data_len < minlen + transform->ivlen ||
2731 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002734 "+ 1 ) ( + expl IV )", rec->data_len,
2735 transform->ivlen,
2736 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002738 }
2739
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002740 /*
2741 * Authenticate before decrypt if enabled
2742 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002744 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002745 {
Hanno Becker992b6872017-11-09 18:57:39 +00002746 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002749
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002750 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2751 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002752
Hanno Beckercab87e62019-04-29 13:52:53 +01002753 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002754
Hanno Beckercab87e62019-04-29 13:52:53 +01002755 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2756 add_data_len );
2757 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2758 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002759 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2760 data, rec->data_len );
2761 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2762 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002763
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002764 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2765 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002766 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002767 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002768
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002769 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2770 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002774 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002775 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002776 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002778
2779 /*
2780 * Check length sanity
2781 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002782 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002785 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002787 }
2788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002790 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002791 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002792 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002793 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002794 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002795 /* This is safe because data_len >= minlen + maclen + 1 initially,
2796 * and at this point we have at most subtracted maclen (note that
2797 * minlen == transform->ivlen here). */
2798 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002799
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002800 data += transform->ivlen;
2801 rec->data_offset += transform->ivlen;
2802 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002803 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002805
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002806 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2807 transform->iv_dec, transform->ivlen,
2808 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002811 return( ret );
2812 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002813
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002814 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2817 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002818 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002821 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002822 {
2823 /*
2824 * Save IV in SSL3 and TLS1
2825 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002826 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2827 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002828 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002829#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002830
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002831 /* Safe since data_len >= minlen + maclen + 1, so after having
2832 * subtracted at most minlen and maclen up to this point,
2833 * data_len > 0. */
2834 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002835
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002836 if( auth_done == 1 )
2837 {
2838 correct *= ( rec->data_len >= padlen + 1 );
2839 padlen *= ( rec->data_len >= padlen + 1 );
2840 }
2841 else
Paul Bakker45829992013-01-03 14:52:21 +01002842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002844 if( rec->data_len < transform->maclen + padlen + 1 )
2845 {
2846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2847 rec->data_len,
2848 transform->maclen,
2849 padlen + 1 ) );
2850 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002851#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002852
2853 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2854 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002855 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002857 padlen++;
2858
2859 /* Regardless of the validity of the padding,
2860 * we have data_len >= padlen here. */
2861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002863 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002864 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002865 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867#if defined(MBEDTLS_SSL_DEBUG_ALL)
2868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002869 "should be no more than %d",
2870 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002871#endif
Paul Bakker45829992013-01-03 14:52:21 +01002872 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002873 }
2874 }
2875 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2877#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2878 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002879 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002880 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002881 /* The padding check involves a series of up to 256
2882 * consecutive memory reads at the end of the record
2883 * plaintext buffer. In order to hide the length and
2884 * validity of the padding, always perform exactly
2885 * `min(256,plaintext_len)` reads (but take into account
2886 * only the last `padlen` bytes for the padding check). */
2887 size_t pad_count = 0;
2888 size_t real_count = 0;
2889 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002890
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002891 /* Index of first padding byte; it has been ensured above
2892 * that the subtraction is safe. */
2893 size_t const padding_idx = rec->data_len - padlen;
2894 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2895 size_t const start_idx = rec->data_len - num_checks;
2896 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002897
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002898 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002899 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002900 real_count |= ( idx >= padding_idx );
2901 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002902 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002903 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002906 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002908#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002909 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002910 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002911 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2913 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2916 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002917 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002918
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002919 /* If the padding was found to be invalid, padlen == 0
2920 * and the subtraction is safe. If the padding was found valid,
2921 * padlen hasn't been changed and the previous assertion
2922 * data_len >= padlen still holds. */
2923 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002924 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002925 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002927 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2930 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002931 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002932
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002933#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002935 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002936#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002937
2938 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002939 * Authenticate if not done yet.
2940 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002941 */
Hanno Becker52344c22018-01-03 15:24:20 +00002942#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002943 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002944 {
Hanno Becker992b6872017-11-09 18:57:39 +00002945 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002946
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002947 /* If the initial value of padlen was such that
2948 * data_len < maclen + padlen + 1, then padlen
2949 * got reset to 1, and the initial check
2950 * data_len >= minlen + maclen + 1
2951 * guarantees that at this point we still
2952 * have at least data_len >= maclen.
2953 *
2954 * If the initial value of padlen was such that
2955 * data_len >= maclen + padlen + 1, then we have
2956 * subtracted either padlen + 1 (if the padding was correct)
2957 * or 0 (if the padding was incorrect) since then,
2958 * hence data_len >= maclen in any case.
2959 */
2960 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002961
Hanno Beckercab87e62019-04-29 13:52:53 +01002962 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002965 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002966 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002967 ssl_mac( &transform->md_ctx_dec,
2968 transform->mac_dec,
2969 data, rec->data_len,
2970 rec->ctr, rec->type,
2971 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002972 }
2973 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2975#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2976 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002977 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002978 {
2979 /*
2980 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002981 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002982 *
2983 * Known timing attacks:
2984 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2985 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002986 * To compensate for different timings for the MAC calculation
2987 * depending on how much padding was removed (which is determined
2988 * by padlen), process extra_run more blocks through the hash
2989 * function.
2990 *
2991 * The formula in the paper is
2992 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2993 * where L1 is the size of the header plus the decrypted message
2994 * plus CBC padding and L2 is the size of the header plus the
2995 * decrypted message. This is for an underlying hash function
2996 * with 64-byte blocks.
2997 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2998 * correctly. We round down instead of up, so -56 is the correct
2999 * value for our calculations instead of -55.
3000 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003001 * Repeat the formula rather than defining a block_size variable.
3002 * This avoids requiring division by a variable at runtime
3003 * (which would be marginally less efficient and would require
3004 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003005 */
3006 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003007 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003008
3009 /*
3010 * The next two sizes are the minimum and maximum values of
3011 * in_msglen over all padlen values.
3012 *
3013 * They're independent of padlen, since we previously did
3014 * in_msglen -= padlen.
3015 *
3016 * Note that max_len + maclen is never more than the buffer
3017 * length, as we previously did in_msglen -= maclen too.
3018 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003019 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003020 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3021
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003022 memset( tmp, 0, sizeof( tmp ) );
3023
3024 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003025 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003026#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3027 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003028 case MBEDTLS_MD_MD5:
3029 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003030 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003031 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003032 extra_run =
3033 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3034 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003035 break;
3036#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003037#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003038 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003039 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003040 extra_run =
3041 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3042 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003043 break;
3044#endif
3045 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003047 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3048 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003049
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003050 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003051
Hanno Beckercab87e62019-04-29 13:52:53 +01003052 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3053 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003054 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3055 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003056 /* Make sure we access everything even when padlen > 0. This
3057 * makes the synchronisation requirements for just-in-time
3058 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003059 ssl_read_memory( data + rec->data_len, padlen );
3060 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003061
3062 /* Call mbedtls_md_process at least once due to cache attacks
3063 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003064 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003065 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003066
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003067 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003068
3069 /* Make sure we access all the memory that could contain the MAC,
3070 * before we check it in the next code block. This makes the
3071 * synchronisation requirements for just-in-time Prime+Probe
3072 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003073 ssl_read_memory( data + min_len,
3074 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003075 }
3076 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3078 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3081 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003082 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003083
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003084#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003085 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3086 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003087#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003088
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003089 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3090 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092#if defined(MBEDTLS_SSL_DEBUG_ALL)
3093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003094#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003095 correct = 0;
3096 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003097 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003098 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003099
3100 /*
3101 * Finally check the correct flag
3102 */
3103 if( correct == 0 )
3104 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003105#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003106
3107 /* Make extra sure authentication was performed, exactly once */
3108 if( auth_done != 1 )
3109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3111 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003112 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003113
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003114#if defined(MBEDTLS_SSL_CID)
3115 if( rec->cid_len != 0 )
3116 {
3117 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3118 &rec->type );
3119 if( ret != 0 )
3120 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3121 }
3122#endif /* MBEDTLS_SSL_CID */
3123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003125
3126 return( 0 );
3127}
3128
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003129#undef MAC_NONE
3130#undef MAC_PLAINTEXT
3131#undef MAC_CIPHERTEXT
3132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003134/*
3135 * Compression/decompression functions
3136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003138{
3139 int ret;
3140 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003141 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003142 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003143 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003146
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003147 if( len_pre == 0 )
3148 return( 0 );
3149
Paul Bakker2770fbd2012-07-03 13:30:23 +00003150 memcpy( msg_pre, ssl->out_msg, len_pre );
3151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003153 ssl->out_msglen ) );
3154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003156 ssl->out_msg, ssl->out_msglen );
3157
Paul Bakker48916f92012-09-16 19:57:18 +00003158 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3159 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3160 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003161 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003162
Paul Bakker48916f92012-09-16 19:57:18 +00003163 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003164 if( ret != Z_OK )
3165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3167 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003168 }
3169
Angus Grattond8213d02016-05-25 20:56:48 +10003170 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003171 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003174 ssl->out_msglen ) );
3175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003177 ssl->out_msg, ssl->out_msglen );
3178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003180
3181 return( 0 );
3182}
3183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003185{
3186 int ret;
3187 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003188 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003189 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003190 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003193
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003194 if( len_pre == 0 )
3195 return( 0 );
3196
Paul Bakker2770fbd2012-07-03 13:30:23 +00003197 memcpy( msg_pre, ssl->in_msg, len_pre );
3198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003200 ssl->in_msglen ) );
3201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003203 ssl->in_msg, ssl->in_msglen );
3204
Paul Bakker48916f92012-09-16 19:57:18 +00003205 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3206 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3207 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003208 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003209 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003210
Paul Bakker48916f92012-09-16 19:57:18 +00003211 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003212 if( ret != Z_OK )
3213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3215 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003216 }
3217
Angus Grattond8213d02016-05-25 20:56:48 +10003218 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003219 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003222 ssl->in_msglen ) );
3223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003225 ssl->in_msg, ssl->in_msglen );
3226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003228
3229 return( 0 );
3230}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3234static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236#if defined(MBEDTLS_SSL_PROTO_DTLS)
3237static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003238{
3239 /* If renegotiation is not enforced, retransmit until we would reach max
3240 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003241 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003242 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003243 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003244 unsigned char doublings = 1;
3245
3246 while( ratio != 0 )
3247 {
3248 ++doublings;
3249 ratio >>= 1;
3250 }
3251
3252 if( ++ssl->renego_records_seen > doublings )
3253 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003255 return( 0 );
3256 }
3257 }
3258
3259 return( ssl_write_hello_request( ssl ) );
3260}
3261#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003263
Paul Bakker5121ce52009-01-03 21:22:43 +00003264/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003265 * Fill the input message buffer by appending data to it.
3266 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003267 *
3268 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3269 * available (from this read and/or a previous one). Otherwise, an error code
3270 * is returned (possibly EOF or WANT_READ).
3271 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003272 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3273 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3274 * since we always read a whole datagram at once.
3275 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003276 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003277 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003279int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003280{
Paul Bakker23986e52011-04-24 08:57:21 +00003281 int ret;
3282 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003285
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003286 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003289 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003291 }
3292
Angus Grattond8213d02016-05-25 20:56:48 +10003293 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3296 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003297 }
3298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003300 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003301 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003302 uint32_t timeout;
3303
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003304 /* Just to be sure */
3305 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3306 {
3307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3308 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3309 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3310 }
3311
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003312 /*
3313 * The point is, we need to always read a full datagram at once, so we
3314 * sometimes read more then requested, and handle the additional data.
3315 * It could be the rest of the current record (while fetching the
3316 * header) and/or some other records in the same datagram.
3317 */
3318
3319 /*
3320 * Move to the next record in the already read datagram if applicable
3321 */
3322 if( ssl->next_record_offset != 0 )
3323 {
3324 if( ssl->in_left < ssl->next_record_offset )
3325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3327 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003328 }
3329
3330 ssl->in_left -= ssl->next_record_offset;
3331
3332 if( ssl->in_left != 0 )
3333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003335 ssl->next_record_offset ) );
3336 memmove( ssl->in_hdr,
3337 ssl->in_hdr + ssl->next_record_offset,
3338 ssl->in_left );
3339 }
3340
3341 ssl->next_record_offset = 0;
3342 }
3343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003345 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003346
3347 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003348 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003349 */
3350 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003353 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003354 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003355
3356 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003357 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003358 * are not at the beginning of a new record, the caller did something
3359 * wrong.
3360 */
3361 if( ssl->in_left != 0 )
3362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3364 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003365 }
3366
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003367 /*
3368 * Don't even try to read if time's out already.
3369 * This avoids by-passing the timer when repeatedly receiving messages
3370 * that will end up being dropped.
3371 */
3372 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003373 {
3374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003375 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003376 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003377 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003378 {
Angus Grattond8213d02016-05-25 20:56:48 +10003379 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003382 timeout = ssl->handshake->retransmit_timeout;
3383 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003384 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003387
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003388 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003389 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3390 timeout );
3391 else
3392 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003395
3396 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003398 }
3399
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003400 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003403 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003406 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003407 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003410 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003411 }
3412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003416 return( ret );
3417 }
3418
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003419 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003420 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003422 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003424 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003425 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003428 return( ret );
3429 }
3430
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003431 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003432 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003434 }
3435
Paul Bakker5121ce52009-01-03 21:22:43 +00003436 if( ret < 0 )
3437 return( ret );
3438
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003439 ssl->in_left = ret;
3440 }
3441 else
3442#endif
3443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003445 ssl->in_left, nb_want ) );
3446
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003447 while( ssl->in_left < nb_want )
3448 {
3449 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003450
3451 if( ssl_check_timer( ssl ) != 0 )
3452 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3453 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003454 {
3455 if( ssl->f_recv_timeout != NULL )
3456 {
3457 ret = ssl->f_recv_timeout( ssl->p_bio,
3458 ssl->in_hdr + ssl->in_left, len,
3459 ssl->conf->read_timeout );
3460 }
3461 else
3462 {
3463 ret = ssl->f_recv( ssl->p_bio,
3464 ssl->in_hdr + ssl->in_left, len );
3465 }
3466 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003469 ssl->in_left, nb_want ) );
3470 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003471
3472 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003474
3475 if( ret < 0 )
3476 return( ret );
3477
mohammad160352aecb92018-03-28 23:41:40 -07003478 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003479 {
Darryl Green11999bb2018-03-13 15:22:58 +00003480 MBEDTLS_SSL_DEBUG_MSG( 1,
3481 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003482 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003483 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3484 }
3485
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003486 ssl->in_left += ret;
3487 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003488 }
3489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003491
3492 return( 0 );
3493}
3494
3495/*
3496 * Flush any data not yet written
3497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003499{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003500 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003501 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003504
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003505 if( ssl->f_send == NULL )
3506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003508 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003510 }
3511
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003512 /* Avoid incrementing counter if data is flushed */
3513 if( ssl->out_left == 0 )
3514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003516 return( 0 );
3517 }
3518
Paul Bakker5121ce52009-01-03 21:22:43 +00003519 while( ssl->out_left > 0 )
3520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3522 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003523
Hanno Becker2b1e3542018-08-06 11:19:13 +01003524 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003525 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003528
3529 if( ret <= 0 )
3530 return( ret );
3531
mohammad160352aecb92018-03-28 23:41:40 -07003532 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003533 {
Darryl Green11999bb2018-03-13 15:22:58 +00003534 MBEDTLS_SSL_DEBUG_MSG( 1,
3535 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003536 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003537 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3538 }
3539
Paul Bakker5121ce52009-01-03 21:22:43 +00003540 ssl->out_left -= ret;
3541 }
3542
Hanno Becker2b1e3542018-08-06 11:19:13 +01003543#if defined(MBEDTLS_SSL_PROTO_DTLS)
3544 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003545 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003546 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003547 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003548 else
3549#endif
3550 {
3551 ssl->out_hdr = ssl->out_buf + 8;
3552 }
3553 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003556
3557 return( 0 );
3558}
3559
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003560/*
3561 * Functions to handle the DTLS retransmission state machine
3562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003564/*
3565 * Append current handshake message to current outgoing flight
3566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003568{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3571 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3572 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003573
3574 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003575 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003576 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003579 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003580 }
3581
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003582 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003583 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003586 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003587 }
3588
3589 /* Copy current handshake message with headers */
3590 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3591 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003592 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003593 msg->next = NULL;
3594
3595 /* Append to the current flight */
3596 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003597 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003598 else
3599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003600 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003601 while( cur->next != NULL )
3602 cur = cur->next;
3603 cur->next = msg;
3604 }
3605
Hanno Becker3b235902018-08-06 09:54:53 +01003606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003607 return( 0 );
3608}
3609
3610/*
3611 * Free the current flight of handshake messages
3612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003614{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615 mbedtls_ssl_flight_item *cur = flight;
3616 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003617
3618 while( cur != NULL )
3619 {
3620 next = cur->next;
3621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622 mbedtls_free( cur->p );
3623 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003624
3625 cur = next;
3626 }
3627}
3628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3630static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003631#endif
3632
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003633/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003634 * Swap transform_out and out_ctr with the alternative ones
3635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003637{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003639 unsigned char tmp_out_ctr[8];
3640
3641 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003644 return;
3645 }
3646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003648
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003649 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003650 tmp_transform = ssl->transform_out;
3651 ssl->transform_out = ssl->handshake->alt_transform_out;
3652 ssl->handshake->alt_transform_out = tmp_transform;
3653
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003654 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003655 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3656 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003657 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003658
3659 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003660 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3663 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3668 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003669 }
3670 }
3671#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003672}
3673
3674/*
3675 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003676 */
3677int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3678{
3679 int ret = 0;
3680
3681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3682
3683 ret = mbedtls_ssl_flight_transmit( ssl );
3684
3685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3686
3687 return( ret );
3688}
3689
3690/*
3691 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003692 *
3693 * Need to remember the current message in case flush_output returns
3694 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003695 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003696 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003697int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003698{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003699 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003702 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003703 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003705
3706 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003707 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003708 ssl_swap_epochs( ssl );
3709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003710 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003711 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003712
3713 while( ssl->handshake->cur_msg != NULL )
3714 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003715 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003716 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003717
Hanno Beckere1dcb032018-08-17 16:47:58 +01003718 int const is_finished =
3719 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3720 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3721
Hanno Becker04da1892018-08-14 13:22:10 +01003722 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3723 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3724
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003725 /* Swap epochs before sending Finished: we can't do it after
3726 * sending ChangeCipherSpec, in case write returns WANT_READ.
3727 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003728 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003729 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003731 ssl_swap_epochs( ssl );
3732 }
3733
Hanno Becker67bc7c32018-08-06 11:33:50 +01003734 ret = ssl_get_remaining_payload_in_datagram( ssl );
3735 if( ret < 0 )
3736 return( ret );
3737 max_frag_len = (size_t) ret;
3738
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003739 /* CCS is copied as is, while HS messages may need fragmentation */
3740 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3741 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003742 if( max_frag_len == 0 )
3743 {
3744 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3745 return( ret );
3746
3747 continue;
3748 }
3749
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003750 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003751 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003752 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003753
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003754 /* Update position inside current message */
3755 ssl->handshake->cur_msg_p += cur->len;
3756 }
3757 else
3758 {
3759 const unsigned char * const p = ssl->handshake->cur_msg_p;
3760 const size_t hs_len = cur->len - 12;
3761 const size_t frag_off = p - ( cur->p + 12 );
3762 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003763 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003764
Hanno Beckere1dcb032018-08-17 16:47:58 +01003765 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003766 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003767 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003768 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003769
Hanno Becker67bc7c32018-08-06 11:33:50 +01003770 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3771 return( ret );
3772
3773 continue;
3774 }
3775 max_hs_frag_len = max_frag_len - 12;
3776
3777 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3778 max_hs_frag_len : rem_len;
3779
3780 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003781 {
3782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003783 (unsigned) cur_hs_frag_len,
3784 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003785 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003786
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003787 /* Messages are stored with handshake headers as if not fragmented,
3788 * copy beginning of headers then fill fragmentation fields.
3789 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3790 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003791
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003792 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3793 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3794 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3795
Hanno Becker67bc7c32018-08-06 11:33:50 +01003796 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3797 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3798 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003799
3800 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3801
Hanno Becker3f7b9732018-08-28 09:53:25 +01003802 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003803 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3804 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003805 ssl->out_msgtype = cur->type;
3806
3807 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003808 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003809 }
3810
3811 /* If done with the current message move to the next one if any */
3812 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3813 {
3814 if( cur->next != NULL )
3815 {
3816 ssl->handshake->cur_msg = cur->next;
3817 ssl->handshake->cur_msg_p = cur->next->p + 12;
3818 }
3819 else
3820 {
3821 ssl->handshake->cur_msg = NULL;
3822 ssl->handshake->cur_msg_p = NULL;
3823 }
3824 }
3825
3826 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003827 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003830 return( ret );
3831 }
3832 }
3833
Hanno Becker67bc7c32018-08-06 11:33:50 +01003834 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3835 return( ret );
3836
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003837 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3839 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003840 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003843 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3844 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003845
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003847
3848 return( 0 );
3849}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003850
3851/*
3852 * To be called when the last message of an incoming flight is received.
3853 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003855{
3856 /* We won't need to resend that one any more */
3857 ssl_flight_free( ssl->handshake->flight );
3858 ssl->handshake->flight = NULL;
3859 ssl->handshake->cur_msg = NULL;
3860
3861 /* The next incoming flight will start with this msg_seq */
3862 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3863
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003864 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003865 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003866
Hanno Becker0271f962018-08-16 13:23:47 +01003867 /* Clear future message buffering structure. */
3868 ssl_buffering_free( ssl );
3869
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003870 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003871 ssl_set_timer( ssl, 0 );
3872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003873 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3874 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003877 }
3878 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003880}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003881
3882/*
3883 * To be called when the last message of an outgoing flight is send.
3884 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003886{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003887 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003888 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3891 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003893 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003894 }
3895 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003897}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003899
Paul Bakker5121ce52009-01-03 21:22:43 +00003900/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003901 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003902 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003903
3904/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003905 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003906 *
3907 * - fill in handshake headers
3908 * - update handshake checksum
3909 * - DTLS: save message for resending
3910 * - then pass to the record layer
3911 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003912 * DTLS: except for HelloRequest, messages are only queued, and will only be
3913 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003914 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003915 * Inputs:
3916 * - ssl->out_msglen: 4 + actual handshake message len
3917 * (4 is the size of handshake headers for TLS)
3918 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3919 * - ssl->out_msg + 4: the handshake message body
3920 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003921 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003922 * - ssl->out_msglen: the length of the record contents
3923 * (including handshake headers but excluding record headers)
3924 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003925 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003926int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003927{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003928 int ret;
3929 const size_t hs_len = ssl->out_msglen - 4;
3930 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003931
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3933
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003934 /*
3935 * Sanity checks
3936 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003937 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003938 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3939 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003940 /* In SSLv3, the client might send a NoCertificate alert. */
3941#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3942 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3943 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3944 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3945#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3946 {
3947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3948 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3949 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003950 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003951
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003952 /* Whenever we send anything different from a
3953 * HelloRequest we should be in a handshake - double check. */
3954 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3955 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003956 ssl->handshake == NULL )
3957 {
3958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3959 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3960 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003962#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003963 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003964 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003965 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003966 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3968 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003969 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003970#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003971
Hanno Beckerb50a2532018-08-06 11:52:54 +01003972 /* Double-check that we did not exceed the bounds
3973 * of the outgoing record buffer.
3974 * This should never fail as the various message
3975 * writing functions must obey the bounds of the
3976 * outgoing record buffer, but better be safe.
3977 *
3978 * Note: We deliberately do not check for the MTU or MFL here.
3979 */
3980 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3981 {
3982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3983 "size %u, maximum %u",
3984 (unsigned) ssl->out_msglen,
3985 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3986 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3987 }
3988
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003989 /*
3990 * Fill handshake headers
3991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003992 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003993 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003994 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3995 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3996 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003997
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003998 /*
3999 * DTLS has additional fields in the Handshake layer,
4000 * between the length field and the actual payload:
4001 * uint16 message_seq;
4002 * uint24 fragment_offset;
4003 * uint24 fragment_length;
4004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004006 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004007 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004008 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004009 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004010 {
4011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4012 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004013 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004014 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004015 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4016 }
4017
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004018 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004019 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004020
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004021 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004022 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004023 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004024 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4025 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4026 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004027 }
4028 else
4029 {
4030 ssl->out_msg[4] = 0;
4031 ssl->out_msg[5] = 0;
4032 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004033
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004034 /* Handshake hashes are computed without fragmentation,
4035 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004036 memset( ssl->out_msg + 6, 0x00, 3 );
4037 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004038 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004040
Hanno Becker0207e532018-08-28 10:28:28 +01004041 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004042 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4043 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004044 }
4045
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004046 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004047#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004048 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004049 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4050 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004051 {
4052 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004054 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004055 return( ret );
4056 }
4057 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004058 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004059#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004060 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004061 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004062 {
4063 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4064 return( ret );
4065 }
4066 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004067
4068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4069
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004070 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004071}
4072
4073/*
4074 * Record layer functions
4075 */
4076
4077/*
4078 * Write current record.
4079 *
4080 * Uses:
4081 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4082 * - ssl->out_msglen: length of the record content (excl headers)
4083 * - ssl->out_msg: record content
4084 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004085int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004086{
4087 int ret, done = 0;
4088 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004089 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004090
4091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004093#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004094 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004096 {
4097 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004100 return( ret );
4101 }
4102
4103 len = ssl->out_msglen;
4104 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004107#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4108 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004112 ret = mbedtls_ssl_hw_record_write( ssl );
4113 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004115 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4116 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004117 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004118
4119 if( ret == 0 )
4120 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004121 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004122#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004123 if( !done )
4124 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004125 unsigned i;
4126 size_t protected_record_size;
4127
Paul Bakker05ef8352012-05-08 09:17:57 +00004128 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004129 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004130 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004131
Hanno Becker19859472018-08-06 09:40:20 +01004132 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004133 ssl->out_len[0] = (unsigned char)( len >> 8 );
4134 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004135
Paul Bakker48916f92012-09-16 19:57:18 +00004136 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004137 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004138 mbedtls_record rec;
4139
4140 rec.buf = ssl->out_iv;
4141 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4142 ( ssl->out_iv - ssl->out_buf );
4143 rec.data_len = ssl->out_msglen;
4144 rec.data_offset = ssl->out_msg - rec.buf;
4145
4146 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4147 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4148 ssl->conf->transport, rec.ver );
4149 rec.type = ssl->out_msgtype;
4150
Hanno Beckercab87e62019-04-29 13:52:53 +01004151 rec.cid_len = 0;
4152
Hanno Beckera18d1322018-01-03 14:27:32 +00004153 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004154 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004157 return( ret );
4158 }
4159
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004160 if( rec.data_offset != 0 )
4161 {
4162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4163 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4164 }
4165
Hanno Becker78f839d2019-03-14 12:56:23 +00004166 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004167 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4168 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004169 }
4170
Hanno Becker2b1e3542018-08-06 11:19:13 +01004171 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
4172
4173#if defined(MBEDTLS_SSL_PROTO_DTLS)
4174 /* In case of DTLS, double-check that we don't exceed
4175 * the remaining space in the datagram. */
4176 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4177 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004178 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004179 if( ret < 0 )
4180 return( ret );
4181
4182 if( protected_record_size > (size_t) ret )
4183 {
4184 /* Should never happen */
4185 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4186 }
4187 }
4188#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004190 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004191 "version = [%d:%d], msglen = %d",
4192 ssl->out_hdr[0], ssl->out_hdr[1],
4193 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004195 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004196 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004197
4198 ssl->out_left += protected_record_size;
4199 ssl->out_hdr += protected_record_size;
4200 ssl_update_out_pointers( ssl, ssl->transform_out );
4201
Hanno Becker04484622018-08-06 09:49:38 +01004202 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4203 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4204 break;
4205
4206 /* The loop goes to its end iff the counter is wrapping */
4207 if( i == ssl_ep_len( ssl ) )
4208 {
4209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4210 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4211 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004212 }
4213
Hanno Becker67bc7c32018-08-06 11:33:50 +01004214#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004215 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4216 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004217 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004218 size_t remaining;
4219 ret = ssl_get_remaining_payload_in_datagram( ssl );
4220 if( ret < 0 )
4221 {
4222 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4223 ret );
4224 return( ret );
4225 }
4226
4227 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004228 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004229 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004230 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004231 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004232 else
4233 {
Hanno Becker513815a2018-08-20 11:56:09 +01004234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004235 }
4236 }
4237#endif /* MBEDTLS_SSL_PROTO_DTLS */
4238
4239 if( ( flush == SSL_FORCE_FLUSH ) &&
4240 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004242 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004243 return( ret );
4244 }
4245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004247
4248 return( 0 );
4249}
4250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004251#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004252
4253static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4254{
4255 if( ssl->in_msglen < ssl->in_hslen ||
4256 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4257 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4258 {
4259 return( 1 );
4260 }
4261 return( 0 );
4262}
Hanno Becker44650b72018-08-16 12:51:11 +01004263
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004264static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004265{
4266 return( ( ssl->in_msg[9] << 16 ) |
4267 ( ssl->in_msg[10] << 8 ) |
4268 ssl->in_msg[11] );
4269}
4270
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004271static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004272{
4273 return( ( ssl->in_msg[6] << 16 ) |
4274 ( ssl->in_msg[7] << 8 ) |
4275 ssl->in_msg[8] );
4276}
4277
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004278static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004279{
4280 uint32_t msg_len, frag_off, frag_len;
4281
4282 msg_len = ssl_get_hs_total_len( ssl );
4283 frag_off = ssl_get_hs_frag_off( ssl );
4284 frag_len = ssl_get_hs_frag_len( ssl );
4285
4286 if( frag_off > msg_len )
4287 return( -1 );
4288
4289 if( frag_len > msg_len - frag_off )
4290 return( -1 );
4291
4292 if( frag_len + 12 > ssl->in_msglen )
4293 return( -1 );
4294
4295 return( 0 );
4296}
4297
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004298/*
4299 * Mark bits in bitmask (used for DTLS HS reassembly)
4300 */
4301static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4302{
4303 unsigned int start_bits, end_bits;
4304
4305 start_bits = 8 - ( offset % 8 );
4306 if( start_bits != 8 )
4307 {
4308 size_t first_byte_idx = offset / 8;
4309
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004310 /* Special case */
4311 if( len <= start_bits )
4312 {
4313 for( ; len != 0; len-- )
4314 mask[first_byte_idx] |= 1 << ( start_bits - len );
4315
4316 /* Avoid potential issues with offset or len becoming invalid */
4317 return;
4318 }
4319
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004320 offset += start_bits; /* Now offset % 8 == 0 */
4321 len -= start_bits;
4322
4323 for( ; start_bits != 0; start_bits-- )
4324 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4325 }
4326
4327 end_bits = len % 8;
4328 if( end_bits != 0 )
4329 {
4330 size_t last_byte_idx = ( offset + len ) / 8;
4331
4332 len -= end_bits; /* Now len % 8 == 0 */
4333
4334 for( ; end_bits != 0; end_bits-- )
4335 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4336 }
4337
4338 memset( mask + offset / 8, 0xFF, len / 8 );
4339}
4340
4341/*
4342 * Check that bitmask is full
4343 */
4344static int ssl_bitmask_check( unsigned char *mask, size_t len )
4345{
4346 size_t i;
4347
4348 for( i = 0; i < len / 8; i++ )
4349 if( mask[i] != 0xFF )
4350 return( -1 );
4351
4352 for( i = 0; i < len % 8; i++ )
4353 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4354 return( -1 );
4355
4356 return( 0 );
4357}
4358
Hanno Becker56e205e2018-08-16 09:06:12 +01004359/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004360static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004361 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004362{
Hanno Becker56e205e2018-08-16 09:06:12 +01004363 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004364
Hanno Becker56e205e2018-08-16 09:06:12 +01004365 alloc_len = 12; /* Handshake header */
4366 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004367
Hanno Beckerd07df862018-08-16 09:14:58 +01004368 if( add_bitmap )
4369 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004370
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004371 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004372}
Hanno Becker56e205e2018-08-16 09:06:12 +01004373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004374#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004375
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004376static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004377{
4378 return( ( ssl->in_msg[1] << 16 ) |
4379 ( ssl->in_msg[2] << 8 ) |
4380 ssl->in_msg[3] );
4381}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004382
Simon Butcher99000142016-10-13 17:21:01 +01004383int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004384{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004385 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004388 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004389 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004390 }
4391
Hanno Becker12555c62018-08-16 12:47:53 +01004392 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004394 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004395 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004396 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004398#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004399 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004400 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004401 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004402 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004403
Hanno Becker44650b72018-08-16 12:51:11 +01004404 if( ssl_check_hs_header( ssl ) != 0 )
4405 {
4406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4407 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4408 }
4409
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004410 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004411 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4412 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4413 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4414 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004415 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004416 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4417 {
4418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4419 recv_msg_seq,
4420 ssl->handshake->in_msg_seq ) );
4421 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4422 }
4423
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004424 /* Retransmit only on last message from previous flight, to avoid
4425 * too many retransmissions.
4426 * Besides, No sane server ever retransmits HelloVerifyRequest */
4427 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004428 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004431 "message_seq = %d, start_of_flight = %d",
4432 recv_msg_seq,
4433 ssl->handshake->in_flight_start_seq ) );
4434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004435 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004437 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004438 return( ret );
4439 }
4440 }
4441 else
4442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004444 "message_seq = %d, expected = %d",
4445 recv_msg_seq,
4446 ssl->handshake->in_msg_seq ) );
4447 }
4448
Hanno Becker90333da2017-10-10 11:27:13 +01004449 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004450 }
4451 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004452
Hanno Becker6d97ef52018-08-16 13:09:04 +01004453 /* Message reassembly is handled alongside buffering of future
4454 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004455 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004456 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004457 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004460 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004461 }
4462 }
4463 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004464#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004465 /* With TLS we don't handle fragmentation (for now) */
4466 if( ssl->in_msglen < ssl->in_hslen )
4467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4469 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004470 }
4471
Simon Butcher99000142016-10-13 17:21:01 +01004472 return( 0 );
4473}
4474
4475void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4476{
Hanno Becker0271f962018-08-16 13:23:47 +01004477 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004478
Hanno Becker0271f962018-08-16 13:23:47 +01004479 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004480 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004481 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004482 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004483
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004484 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004485#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004486 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004487 ssl->handshake != NULL )
4488 {
Hanno Becker0271f962018-08-16 13:23:47 +01004489 unsigned offset;
4490 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004491
Hanno Becker0271f962018-08-16 13:23:47 +01004492 /* Increment handshake sequence number */
4493 hs->in_msg_seq++;
4494
4495 /*
4496 * Clear up handshake buffering and reassembly structure.
4497 */
4498
4499 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004500 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004501
4502 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004503 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4504 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004505 offset++, hs_buf++ )
4506 {
4507 *hs_buf = *(hs_buf + 1);
4508 }
4509
4510 /* Create a fresh last entry */
4511 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004512 }
4513#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004514}
4515
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004516/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004517 * DTLS anti-replay: RFC 6347 4.1.2.6
4518 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004519 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4520 * Bit n is set iff record number in_window_top - n has been seen.
4521 *
4522 * Usually, in_window_top is the last record number seen and the lsb of
4523 * in_window is set. The only exception is the initial state (record number 0
4524 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004526#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4527static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004528{
4529 ssl->in_window_top = 0;
4530 ssl->in_window = 0;
4531}
4532
4533static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4534{
4535 return( ( (uint64_t) buf[0] << 40 ) |
4536 ( (uint64_t) buf[1] << 32 ) |
4537 ( (uint64_t) buf[2] << 24 ) |
4538 ( (uint64_t) buf[3] << 16 ) |
4539 ( (uint64_t) buf[4] << 8 ) |
4540 ( (uint64_t) buf[5] ) );
4541}
4542
4543/*
4544 * Return 0 if sequence number is acceptable, -1 otherwise
4545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004546int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004547{
4548 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4549 uint64_t bit;
4550
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004551 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004552 return( 0 );
4553
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004554 if( rec_seqnum > ssl->in_window_top )
4555 return( 0 );
4556
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004557 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004558
4559 if( bit >= 64 )
4560 return( -1 );
4561
4562 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4563 return( -1 );
4564
4565 return( 0 );
4566}
4567
4568/*
4569 * Update replay window on new validated record
4570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004571void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004572{
4573 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4574
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004575 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004576 return;
4577
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004578 if( rec_seqnum > ssl->in_window_top )
4579 {
4580 /* Update window_top and the contents of the window */
4581 uint64_t shift = rec_seqnum - ssl->in_window_top;
4582
4583 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004584 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004585 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004586 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004587 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004588 ssl->in_window |= 1;
4589 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004590
4591 ssl->in_window_top = rec_seqnum;
4592 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004593 else
4594 {
4595 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004596 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004597
4598 if( bit < 64 ) /* Always true, but be extra sure */
4599 ssl->in_window |= (uint64_t) 1 << bit;
4600 }
4601}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004602#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004603
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004604#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004605/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004606static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4607
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004608/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004609 * Without any SSL context, check if a datagram looks like a ClientHello with
4610 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004611 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004612 *
4613 * - if cookie is valid, return 0
4614 * - if ClientHello looks superficially valid but cookie is not,
4615 * fill obuf and set olen, then
4616 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4617 * - otherwise return a specific error code
4618 */
4619static int ssl_check_dtls_clihlo_cookie(
4620 mbedtls_ssl_cookie_write_t *f_cookie_write,
4621 mbedtls_ssl_cookie_check_t *f_cookie_check,
4622 void *p_cookie,
4623 const unsigned char *cli_id, size_t cli_id_len,
4624 const unsigned char *in, size_t in_len,
4625 unsigned char *obuf, size_t buf_len, size_t *olen )
4626{
4627 size_t sid_len, cookie_len;
4628 unsigned char *p;
4629
4630 if( f_cookie_write == NULL || f_cookie_check == NULL )
4631 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4632
4633 /*
4634 * Structure of ClientHello with record and handshake headers,
4635 * and expected values. We don't need to check a lot, more checks will be
4636 * done when actually parsing the ClientHello - skipping those checks
4637 * avoids code duplication and does not make cookie forging any easier.
4638 *
4639 * 0-0 ContentType type; copied, must be handshake
4640 * 1-2 ProtocolVersion version; copied
4641 * 3-4 uint16 epoch; copied, must be 0
4642 * 5-10 uint48 sequence_number; copied
4643 * 11-12 uint16 length; (ignored)
4644 *
4645 * 13-13 HandshakeType msg_type; (ignored)
4646 * 14-16 uint24 length; (ignored)
4647 * 17-18 uint16 message_seq; copied
4648 * 19-21 uint24 fragment_offset; copied, must be 0
4649 * 22-24 uint24 fragment_length; (ignored)
4650 *
4651 * 25-26 ProtocolVersion client_version; (ignored)
4652 * 27-58 Random random; (ignored)
4653 * 59-xx SessionID session_id; 1 byte len + sid_len content
4654 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4655 * ...
4656 *
4657 * Minimum length is 61 bytes.
4658 */
4659 if( in_len < 61 ||
4660 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4661 in[3] != 0 || in[4] != 0 ||
4662 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4663 {
4664 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4665 }
4666
4667 sid_len = in[59];
4668 if( sid_len > in_len - 61 )
4669 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4670
4671 cookie_len = in[60 + sid_len];
4672 if( cookie_len > in_len - 60 )
4673 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4674
4675 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4676 cli_id, cli_id_len ) == 0 )
4677 {
4678 /* Valid cookie */
4679 return( 0 );
4680 }
4681
4682 /*
4683 * If we get here, we've got an invalid cookie, let's prepare HVR.
4684 *
4685 * 0-0 ContentType type; copied
4686 * 1-2 ProtocolVersion version; copied
4687 * 3-4 uint16 epoch; copied
4688 * 5-10 uint48 sequence_number; copied
4689 * 11-12 uint16 length; olen - 13
4690 *
4691 * 13-13 HandshakeType msg_type; hello_verify_request
4692 * 14-16 uint24 length; olen - 25
4693 * 17-18 uint16 message_seq; copied
4694 * 19-21 uint24 fragment_offset; copied
4695 * 22-24 uint24 fragment_length; olen - 25
4696 *
4697 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4698 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4699 *
4700 * Minimum length is 28.
4701 */
4702 if( buf_len < 28 )
4703 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4704
4705 /* Copy most fields and adapt others */
4706 memcpy( obuf, in, 25 );
4707 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4708 obuf[25] = 0xfe;
4709 obuf[26] = 0xff;
4710
4711 /* Generate and write actual cookie */
4712 p = obuf + 28;
4713 if( f_cookie_write( p_cookie,
4714 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4715 {
4716 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4717 }
4718
4719 *olen = p - obuf;
4720
4721 /* Go back and fill length fields */
4722 obuf[27] = (unsigned char)( *olen - 28 );
4723
4724 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4725 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4726 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4727
4728 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4729 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4730
4731 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4732}
4733
4734/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004735 * Handle possible client reconnect with the same UDP quadruplet
4736 * (RFC 6347 Section 4.2.8).
4737 *
4738 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4739 * that looks like a ClientHello.
4740 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004741 * - if the input looks like a ClientHello without cookies,
4742 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004743 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004744 * - if the input looks like a ClientHello with a valid cookie,
4745 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004746 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004747 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004748 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004749 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004750 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4751 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004752 */
4753static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4754{
4755 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004756 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004757
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004758 ret = ssl_check_dtls_clihlo_cookie(
4759 ssl->conf->f_cookie_write,
4760 ssl->conf->f_cookie_check,
4761 ssl->conf->p_cookie,
4762 ssl->cli_id, ssl->cli_id_len,
4763 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004764 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004765
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004766 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4767
4768 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004769 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004770 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004771 * If the error is permanent we'll catch it later,
4772 * if it's not, then hopefully it'll work next time. */
4773 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4774
4775 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004776 }
4777
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004778 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004779 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004780 /* Got a valid cookie, partially reset context */
4781 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4782 {
4783 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4784 return( ret );
4785 }
4786
4787 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004788 }
4789
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004790 return( ret );
4791}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004792#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004793
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004794/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004795 * ContentType type;
4796 * ProtocolVersion version;
4797 * uint16 epoch; // DTLS only
4798 * uint48 sequence_number; // DTLS only
4799 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004800 *
4801 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004802 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004803 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4804 *
4805 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004806 * 1. proceed with the record if this function returns 0
4807 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4808 * 3. return CLIENT_RECONNECT if this function return that value
4809 * 4. drop the whole datagram if this function returns anything else.
4810 * Point 2 is needed when the peer is resending, and we have already received
4811 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004812 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004813static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004814{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004815 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004817 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 +02004818
Paul Bakker5121ce52009-01-03 21:22:43 +00004819 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004820 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004821 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004823 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004824 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004825 ssl->in_msgtype,
4826 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004827
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004828 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004829 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4830 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4831 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4832 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004835
4836#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004837 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4838 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004839 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4840#endif /* MBEDTLS_SSL_PROTO_DTLS */
4841 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4842 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004844 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004845 }
4846
4847 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004848 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4851 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004852 }
4853
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004854 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4857 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004858 }
4859
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004860 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004861 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004862 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4865 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004866 }
4867
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004868 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004869 * DTLS-related tests.
4870 * Check epoch before checking length constraint because
4871 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4872 * message gets duplicated before the corresponding Finished message,
4873 * the second ChangeCipherSpec should be discarded because it belongs
4874 * to an old epoch, but not because its length is shorter than
4875 * the minimum record length for packets using the new record transform.
4876 * Note that these two kinds of failures are handled differently,
4877 * as an unexpected record is silently skipped but an invalid
4878 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004879 */
4880#if defined(MBEDTLS_SSL_PROTO_DTLS)
4881 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4882 {
4883 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4884
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004885 /* Check epoch (and sequence number) with DTLS */
4886 if( rec_epoch != ssl->in_epoch )
4887 {
4888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4889 "expected %d, received %d",
4890 ssl->in_epoch, rec_epoch ) );
4891
4892#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4893 /*
4894 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4895 * access the first byte of record content (handshake type), as we
4896 * have an active transform (possibly iv_len != 0), so use the
4897 * fact that the record header len is 13 instead.
4898 */
4899 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4900 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4901 rec_epoch == 0 &&
4902 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4903 ssl->in_left > 13 &&
4904 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4905 {
4906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4907 "from the same port" ) );
4908 return( ssl_handle_possible_reconnect( ssl ) );
4909 }
4910 else
4911#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004912 {
4913 /* Consider buffering the record. */
4914 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4915 {
4916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4917 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4918 }
4919
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004920 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004921 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004922 }
4923
4924#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4925 /* Replay detection only works for the current epoch */
4926 if( rec_epoch == ssl->in_epoch &&
4927 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4928 {
4929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4930 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4931 }
4932#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004933
Hanno Becker52c6dc62017-05-26 16:07:36 +01004934 /* Drop unexpected ApplicationData records,
4935 * except at the beginning of renegotiations */
4936 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4937 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4938#if defined(MBEDTLS_SSL_RENEGOTIATION)
4939 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4940 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4941#endif
4942 )
4943 {
4944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4945 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4946 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004947 }
4948#endif /* MBEDTLS_SSL_PROTO_DTLS */
4949
Hanno Becker52c6dc62017-05-26 16:07:36 +01004950
4951 /* Check length against bounds of the current transform and version */
4952 if( ssl->transform_in == NULL )
4953 {
4954 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004955 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004956 {
4957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4958 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4959 }
4960 }
4961 else
4962 {
4963 if( ssl->in_msglen < ssl->transform_in->minlen )
4964 {
4965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4966 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4967 }
4968
4969#if defined(MBEDTLS_SSL_PROTO_SSL3)
4970 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004971 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004972 {
4973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4974 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4975 }
4976#endif
4977#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4978 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4979 /*
4980 * TLS encrypted messages can have up to 256 bytes of padding
4981 */
4982 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4983 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004984 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004985 {
4986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4987 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4988 }
4989#endif
4990 }
4991
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004992 return( 0 );
4993}
Paul Bakker5121ce52009-01-03 21:22:43 +00004994
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004995/*
4996 * If applicable, decrypt (and decompress) record content
4997 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004998static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004999{
5000 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005002 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
5003 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005005#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5006 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005010 ret = mbedtls_ssl_hw_record_read( ssl );
5011 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005013 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5014 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005015 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005016
5017 if( ret == 0 )
5018 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005019 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005020#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005021 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005022 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005023 mbedtls_record rec;
5024
5025 rec.buf = ssl->in_iv;
5026 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5027 - ( ssl->in_iv - ssl->in_buf );
5028 rec.data_len = ssl->in_msglen;
5029 rec.data_offset = 0;
5030
5031 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5032 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5033 ssl->conf->transport, rec.ver );
5034 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005035 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5036 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005038 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005039 return( ret );
5040 }
5041
Hanno Becker29800d22018-08-07 14:30:18 +01005042 if( ssl->in_iv + rec.data_offset != ssl->in_msg )
5043 {
5044 /* Should never happen */
5045 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5046 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005047
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005048 ssl->in_msglen = rec.data_len;
5049 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5050 ssl->in_len[1] = (unsigned char)( rec.data_len );
5051
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005052 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5053 ssl->in_msg, ssl->in_msglen );
5054
Angus Grattond8213d02016-05-25 20:56:48 +10005055 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5058 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005059 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005060 else if( ssl->in_msglen == 0 )
5061 {
5062#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5063 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5064 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5065 {
5066 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5068 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5069 }
5070#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5071
5072 ssl->nb_zero++;
5073
5074 /*
5075 * Three or more empty messages may be a DoS attack
5076 * (excessive CPU consumption).
5077 */
5078 if( ssl->nb_zero > 3 )
5079 {
5080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
5081 "messages, possible DoS attack" ) );
5082 /* Q: Is that the right error code? */
5083 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5084 }
5085 }
5086 else
5087 ssl->nb_zero = 0;
5088
5089#if defined(MBEDTLS_SSL_PROTO_DTLS)
5090 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5091 {
5092 ; /* in_ctr read from peer, not maintained internally */
5093 }
5094 else
5095#endif
5096 {
5097 unsigned i;
5098 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5099 if( ++ssl->in_ctr[i - 1] != 0 )
5100 break;
5101
5102 /* The loop goes to its end iff the counter is wrapping */
5103 if( i == ssl_ep_len( ssl ) )
5104 {
5105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5106 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5107 }
5108 }
5109
Paul Bakker5121ce52009-01-03 21:22:43 +00005110 }
5111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005112#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005113 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005114 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005115 {
5116 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005118 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005119 return( ret );
5120 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005121 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005122#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005124#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005125 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005127 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005128 }
5129#endif
5130
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005131 return( 0 );
5132}
5133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005134static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005135
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005136/*
5137 * Read a record.
5138 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005139 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5140 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5141 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005142 */
Hanno Becker1097b342018-08-15 14:09:41 +01005143
5144/* Helper functions for mbedtls_ssl_read_record(). */
5145static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005146static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5147static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005148
Hanno Becker327c93b2018-08-15 13:56:18 +01005149int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005150 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005151{
5152 int ret;
5153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005155
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005156 if( ssl->keep_current_message == 0 )
5157 {
5158 do {
Simon Butcher99000142016-10-13 17:21:01 +01005159
Hanno Becker26994592018-08-15 14:14:59 +01005160 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005161 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005162 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005163
Hanno Beckere74d5562018-08-15 14:26:08 +01005164 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005165 {
Hanno Becker40f50842018-08-15 14:48:01 +01005166#if defined(MBEDTLS_SSL_PROTO_DTLS)
5167 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005168
Hanno Becker40f50842018-08-15 14:48:01 +01005169 /* We only check for buffered messages if the
5170 * current datagram is fully consumed. */
5171 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005172 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005173 {
Hanno Becker40f50842018-08-15 14:48:01 +01005174 if( ssl_load_buffered_message( ssl ) == 0 )
5175 have_buffered = 1;
5176 }
5177
5178 if( have_buffered == 0 )
5179#endif /* MBEDTLS_SSL_PROTO_DTLS */
5180 {
5181 ret = ssl_get_next_record( ssl );
5182 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5183 continue;
5184
5185 if( ret != 0 )
5186 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005187 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005188 return( ret );
5189 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005190 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005191 }
5192
5193 ret = mbedtls_ssl_handle_message_type( ssl );
5194
Hanno Becker40f50842018-08-15 14:48:01 +01005195#if defined(MBEDTLS_SSL_PROTO_DTLS)
5196 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5197 {
5198 /* Buffer future message */
5199 ret = ssl_buffer_message( ssl );
5200 if( ret != 0 )
5201 return( ret );
5202
5203 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5204 }
5205#endif /* MBEDTLS_SSL_PROTO_DTLS */
5206
Hanno Becker90333da2017-10-10 11:27:13 +01005207 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5208 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005209
5210 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005211 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005212 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005213 return( ret );
5214 }
5215
Hanno Becker327c93b2018-08-15 13:56:18 +01005216 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005217 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005218 {
5219 mbedtls_ssl_update_handshake_status( ssl );
5220 }
Simon Butcher99000142016-10-13 17:21:01 +01005221 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005222 else
Simon Butcher99000142016-10-13 17:21:01 +01005223 {
Hanno Becker02f59072018-08-15 14:00:24 +01005224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005225 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005226 }
5227
5228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5229
5230 return( 0 );
5231}
5232
Hanno Becker40f50842018-08-15 14:48:01 +01005233#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005234static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005235{
Hanno Becker40f50842018-08-15 14:48:01 +01005236 if( ssl->in_left > ssl->next_record_offset )
5237 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005238
Hanno Becker40f50842018-08-15 14:48:01 +01005239 return( 0 );
5240}
5241
5242static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5243{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005244 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005245 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005246 int ret = 0;
5247
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005248 if( hs == NULL )
5249 return( -1 );
5250
Hanno Beckere00ae372018-08-20 09:39:42 +01005251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5252
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005253 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5254 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5255 {
5256 /* Check if we have seen a ChangeCipherSpec before.
5257 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005258 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005259 {
5260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5261 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005262 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005263 }
5264
Hanno Becker39b8bc92018-08-28 17:17:13 +01005265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005266 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5267 ssl->in_msglen = 1;
5268 ssl->in_msg[0] = 1;
5269
5270 /* As long as they are equal, the exact value doesn't matter. */
5271 ssl->in_left = 0;
5272 ssl->next_record_offset = 0;
5273
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005274 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005275 goto exit;
5276 }
Hanno Becker37f95322018-08-16 13:55:32 +01005277
Hanno Beckerb8f50142018-08-28 10:01:34 +01005278#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005279 /* Debug only */
5280 {
5281 unsigned offset;
5282 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5283 {
5284 hs_buf = &hs->buffering.hs[offset];
5285 if( hs_buf->is_valid == 1 )
5286 {
5287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5288 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005289 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005290 }
5291 }
5292 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005293#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005294
5295 /* Check if we have buffered and/or fully reassembled the
5296 * next handshake message. */
5297 hs_buf = &hs->buffering.hs[0];
5298 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5299 {
5300 /* Synthesize a record containing the buffered HS message. */
5301 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5302 ( hs_buf->data[2] << 8 ) |
5303 hs_buf->data[3];
5304
5305 /* Double-check that we haven't accidentally buffered
5306 * a message that doesn't fit into the input buffer. */
5307 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5308 {
5309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5310 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5311 }
5312
5313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5314 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5315 hs_buf->data, msg_len + 12 );
5316
5317 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5318 ssl->in_hslen = msg_len + 12;
5319 ssl->in_msglen = msg_len + 12;
5320 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5321
5322 ret = 0;
5323 goto exit;
5324 }
5325 else
5326 {
5327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5328 hs->in_msg_seq ) );
5329 }
5330
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005331 ret = -1;
5332
5333exit:
5334
5335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5336 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005337}
5338
Hanno Beckera02b0b42018-08-21 17:20:27 +01005339static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5340 size_t desired )
5341{
5342 int offset;
5343 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5345 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005346
Hanno Becker01315ea2018-08-21 17:22:17 +01005347 /* Get rid of future records epoch first, if such exist. */
5348 ssl_free_buffered_record( ssl );
5349
5350 /* Check if we have enough space available now. */
5351 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5352 hs->buffering.total_bytes_buffered ) )
5353 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005355 return( 0 );
5356 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005357
Hanno Becker4f432ad2018-08-28 10:02:32 +01005358 /* We don't have enough space to buffer the next expected handshake
5359 * message. Remove buffers used for future messages to gain space,
5360 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005361 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5362 offset >= 0; offset-- )
5363 {
5364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5365 offset ) );
5366
Hanno Beckerb309b922018-08-23 13:18:05 +01005367 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005368
5369 /* Check if we have enough space available now. */
5370 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5371 hs->buffering.total_bytes_buffered ) )
5372 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005374 return( 0 );
5375 }
5376 }
5377
5378 return( -1 );
5379}
5380
Hanno Becker40f50842018-08-15 14:48:01 +01005381static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5382{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005383 int ret = 0;
5384 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5385
5386 if( hs == NULL )
5387 return( 0 );
5388
5389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5390
5391 switch( ssl->in_msgtype )
5392 {
5393 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005395
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005396 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005397 break;
5398
5399 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005400 {
5401 unsigned recv_msg_seq_offset;
5402 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5403 mbedtls_ssl_hs_buffer *hs_buf;
5404 size_t msg_len = ssl->in_hslen - 12;
5405
5406 /* We should never receive an old handshake
5407 * message - double-check nonetheless. */
5408 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5409 {
5410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5411 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5412 }
5413
5414 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5415 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5416 {
5417 /* Silently ignore -- message too far in the future */
5418 MBEDTLS_SSL_DEBUG_MSG( 2,
5419 ( "Ignore future HS message with sequence number %u, "
5420 "buffering window %u - %u",
5421 recv_msg_seq, ssl->handshake->in_msg_seq,
5422 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5423
5424 goto exit;
5425 }
5426
5427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5428 recv_msg_seq, recv_msg_seq_offset ) );
5429
5430 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5431
5432 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005433 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005434 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005435 size_t reassembly_buf_sz;
5436
Hanno Becker37f95322018-08-16 13:55:32 +01005437 hs_buf->is_fragmented =
5438 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5439
5440 /* We copy the message back into the input buffer
5441 * after reassembly, so check that it's not too large.
5442 * This is an implementation-specific limitation
5443 * and not one from the standard, hence it is not
5444 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005445 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005446 {
5447 /* Ignore message */
5448 goto exit;
5449 }
5450
Hanno Beckere0b150f2018-08-21 15:51:03 +01005451 /* Check if we have enough space to buffer the message. */
5452 if( hs->buffering.total_bytes_buffered >
5453 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5454 {
5455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5456 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5457 }
5458
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005459 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5460 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005461
5462 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5463 hs->buffering.total_bytes_buffered ) )
5464 {
5465 if( recv_msg_seq_offset > 0 )
5466 {
5467 /* If we can't buffer a future message because
5468 * of space limitations -- ignore. */
5469 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",
5470 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5471 (unsigned) hs->buffering.total_bytes_buffered ) );
5472 goto exit;
5473 }
Hanno Beckere1801392018-08-21 16:51:05 +01005474 else
5475 {
5476 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",
5477 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5478 (unsigned) hs->buffering.total_bytes_buffered ) );
5479 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005480
Hanno Beckera02b0b42018-08-21 17:20:27 +01005481 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005482 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005483 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",
5484 (unsigned) msg_len,
5485 (unsigned) reassembly_buf_sz,
5486 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005487 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005488 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5489 goto exit;
5490 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005491 }
5492
5493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5494 msg_len ) );
5495
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005496 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5497 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005498 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005499 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005500 goto exit;
5501 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005502 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005503
5504 /* Prepare final header: copy msg_type, length and message_seq,
5505 * then add standardised fragment_offset and fragment_length */
5506 memcpy( hs_buf->data, ssl->in_msg, 6 );
5507 memset( hs_buf->data + 6, 0, 3 );
5508 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5509
5510 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005511
5512 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005513 }
5514 else
5515 {
5516 /* Make sure msg_type and length are consistent */
5517 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5518 {
5519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5520 /* Ignore */
5521 goto exit;
5522 }
5523 }
5524
Hanno Becker4422bbb2018-08-20 09:40:19 +01005525 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005526 {
5527 size_t frag_len, frag_off;
5528 unsigned char * const msg = hs_buf->data + 12;
5529
5530 /*
5531 * Check and copy current fragment
5532 */
5533
5534 /* Validation of header fields already done in
5535 * mbedtls_ssl_prepare_handshake_record(). */
5536 frag_off = ssl_get_hs_frag_off( ssl );
5537 frag_len = ssl_get_hs_frag_len( ssl );
5538
5539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5540 frag_off, frag_len ) );
5541 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5542
5543 if( hs_buf->is_fragmented )
5544 {
5545 unsigned char * const bitmask = msg + msg_len;
5546 ssl_bitmask_set( bitmask, frag_off, frag_len );
5547 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5548 msg_len ) == 0 );
5549 }
5550 else
5551 {
5552 hs_buf->is_complete = 1;
5553 }
5554
5555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5556 hs_buf->is_complete ? "" : "not yet " ) );
5557 }
5558
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005559 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005560 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005561
5562 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005563 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005564 break;
5565 }
5566
5567exit:
5568
5569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5570 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005571}
5572#endif /* MBEDTLS_SSL_PROTO_DTLS */
5573
Hanno Becker1097b342018-08-15 14:09:41 +01005574static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005575{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005576 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005577 * Consume last content-layer message and potentially
5578 * update in_msglen which keeps track of the contents'
5579 * consumption state.
5580 *
5581 * (1) Handshake messages:
5582 * Remove last handshake message, move content
5583 * and adapt in_msglen.
5584 *
5585 * (2) Alert messages:
5586 * Consume whole record content, in_msglen = 0.
5587 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005588 * (3) Change cipher spec:
5589 * Consume whole record content, in_msglen = 0.
5590 *
5591 * (4) Application data:
5592 * Don't do anything - the record layer provides
5593 * the application data as a stream transport
5594 * and consumes through mbedtls_ssl_read only.
5595 *
5596 */
5597
5598 /* Case (1): Handshake messages */
5599 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005600 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005601 /* Hard assertion to be sure that no application data
5602 * is in flight, as corrupting ssl->in_msglen during
5603 * ssl->in_offt != NULL is fatal. */
5604 if( ssl->in_offt != NULL )
5605 {
5606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5607 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5608 }
5609
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005610 /*
5611 * Get next Handshake message in the current record
5612 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005613
Hanno Becker4a810fb2017-05-24 16:27:30 +01005614 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005615 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005616 * current handshake content: If DTLS handshake
5617 * fragmentation is used, that's the fragment
5618 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005619 * size here is faulty and should be changed at
5620 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005621 * (2) While it doesn't seem to cause problems, one
5622 * has to be very careful not to assume that in_hslen
5623 * is always <= in_msglen in a sensible communication.
5624 * Again, it's wrong for DTLS handshake fragmentation.
5625 * The following check is therefore mandatory, and
5626 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005627 * Additionally, ssl->in_hslen might be arbitrarily out of
5628 * bounds after handling a DTLS message with an unexpected
5629 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005630 */
5631 if( ssl->in_hslen < ssl->in_msglen )
5632 {
5633 ssl->in_msglen -= ssl->in_hslen;
5634 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5635 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005636
Hanno Becker4a810fb2017-05-24 16:27:30 +01005637 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5638 ssl->in_msg, ssl->in_msglen );
5639 }
5640 else
5641 {
5642 ssl->in_msglen = 0;
5643 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005644
Hanno Becker4a810fb2017-05-24 16:27:30 +01005645 ssl->in_hslen = 0;
5646 }
5647 /* Case (4): Application data */
5648 else if( ssl->in_offt != NULL )
5649 {
5650 return( 0 );
5651 }
5652 /* Everything else (CCS & Alerts) */
5653 else
5654 {
5655 ssl->in_msglen = 0;
5656 }
5657
Hanno Becker1097b342018-08-15 14:09:41 +01005658 return( 0 );
5659}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005660
Hanno Beckere74d5562018-08-15 14:26:08 +01005661static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5662{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005663 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005664 return( 1 );
5665
5666 return( 0 );
5667}
5668
Hanno Becker5f066e72018-08-16 14:56:31 +01005669#if defined(MBEDTLS_SSL_PROTO_DTLS)
5670
5671static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5672{
5673 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5674 if( hs == NULL )
5675 return;
5676
Hanno Becker01315ea2018-08-21 17:22:17 +01005677 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005678 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005679 hs->buffering.total_bytes_buffered -=
5680 hs->buffering.future_record.len;
5681
5682 mbedtls_free( hs->buffering.future_record.data );
5683 hs->buffering.future_record.data = NULL;
5684 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005685}
5686
5687static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5688{
5689 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5690 unsigned char * rec;
5691 size_t rec_len;
5692 unsigned rec_epoch;
5693
5694 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5695 return( 0 );
5696
5697 if( hs == NULL )
5698 return( 0 );
5699
Hanno Becker5f066e72018-08-16 14:56:31 +01005700 rec = hs->buffering.future_record.data;
5701 rec_len = hs->buffering.future_record.len;
5702 rec_epoch = hs->buffering.future_record.epoch;
5703
5704 if( rec == NULL )
5705 return( 0 );
5706
Hanno Becker4cb782d2018-08-20 11:19:05 +01005707 /* Only consider loading future records if the
5708 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005709 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005710 return( 0 );
5711
Hanno Becker5f066e72018-08-16 14:56:31 +01005712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5713
5714 if( rec_epoch != ssl->in_epoch )
5715 {
5716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5717 goto exit;
5718 }
5719
5720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5721
5722 /* Double-check that the record is not too large */
5723 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5724 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5725 {
5726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5727 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5728 }
5729
5730 memcpy( ssl->in_hdr, rec, rec_len );
5731 ssl->in_left = rec_len;
5732 ssl->next_record_offset = 0;
5733
5734 ssl_free_buffered_record( ssl );
5735
5736exit:
5737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5738 return( 0 );
5739}
5740
5741static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5742{
5743 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5744 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005745 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005746
5747 /* Don't buffer future records outside handshakes. */
5748 if( hs == NULL )
5749 return( 0 );
5750
5751 /* Only buffer handshake records (we are only interested
5752 * in Finished messages). */
5753 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5754 return( 0 );
5755
5756 /* Don't buffer more than one future epoch record. */
5757 if( hs->buffering.future_record.data != NULL )
5758 return( 0 );
5759
Hanno Becker01315ea2018-08-21 17:22:17 +01005760 /* Don't buffer record if there's not enough buffering space remaining. */
5761 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5762 hs->buffering.total_bytes_buffered ) )
5763 {
5764 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",
5765 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5766 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005767 return( 0 );
5768 }
5769
Hanno Becker5f066e72018-08-16 14:56:31 +01005770 /* Buffer record */
5771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5772 ssl->in_epoch + 1 ) );
5773 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5774 rec_hdr_len + ssl->in_msglen );
5775
5776 /* ssl_parse_record_header() only considers records
5777 * of the next epoch as candidates for buffering. */
5778 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005779 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005780
5781 hs->buffering.future_record.data =
5782 mbedtls_calloc( 1, hs->buffering.future_record.len );
5783 if( hs->buffering.future_record.data == NULL )
5784 {
5785 /* If we run out of RAM trying to buffer a
5786 * record from the next epoch, just ignore. */
5787 return( 0 );
5788 }
5789
Hanno Becker01315ea2018-08-21 17:22:17 +01005790 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005791
Hanno Becker01315ea2018-08-21 17:22:17 +01005792 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005793 return( 0 );
5794}
5795
5796#endif /* MBEDTLS_SSL_PROTO_DTLS */
5797
Hanno Beckere74d5562018-08-15 14:26:08 +01005798static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005799{
5800 int ret;
5801
Hanno Becker5f066e72018-08-16 14:56:31 +01005802#if defined(MBEDTLS_SSL_PROTO_DTLS)
5803 /* We might have buffered a future record; if so,
5804 * and if the epoch matches now, load it.
5805 * On success, this call will set ssl->in_left to
5806 * the length of the buffered record, so that
5807 * the calls to ssl_fetch_input() below will
5808 * essentially be no-ops. */
5809 ret = ssl_load_buffered_record( ssl );
5810 if( ret != 0 )
5811 return( ret );
5812#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005814 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005817 return( ret );
5818 }
5819
5820 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005822#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005823 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5824 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005825 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005826 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5827 {
5828 ret = ssl_buffer_future_record( ssl );
5829 if( ret != 0 )
5830 return( ret );
5831
5832 /* Fall through to handling of unexpected records */
5833 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5834 }
5835
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005836 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5837 {
5838 /* Skip unexpected record (but not whole datagram) */
5839 ssl->next_record_offset = ssl->in_msglen
5840 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005841
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5843 "(header)" ) );
5844 }
5845 else
5846 {
5847 /* Skip invalid record and the rest of the datagram */
5848 ssl->next_record_offset = 0;
5849 ssl->in_left = 0;
5850
5851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5852 "(header)" ) );
5853 }
5854
5855 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005856 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005857 }
5858#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005859 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005860 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005861
5862 /*
5863 * Read and optionally decrypt the message contents
5864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5866 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005869 return( ret );
5870 }
5871
5872 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005873#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005874 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005876 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005877 if( ssl->next_record_offset < ssl->in_left )
5878 {
5879 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5880 }
5881 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005882 else
5883#endif
5884 ssl->in_left = 0;
5885
5886 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005889 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005890 {
5891 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005892 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5893 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005894 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005895 /* Except when waiting for Finished as a bad mac here
5896 * probably means something went wrong in the handshake
5897 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5898 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5899 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5900 {
5901#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5902 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5903 {
5904 mbedtls_ssl_send_alert_message( ssl,
5905 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5906 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5907 }
5908#endif
5909 return( ret );
5910 }
5911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005912#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005913 if( ssl->conf->badmac_limit != 0 &&
5914 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5917 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005918 }
5919#endif
5920
Hanno Becker4a810fb2017-05-24 16:27:30 +01005921 /* As above, invalid records cause
5922 * dismissal of the whole datagram. */
5923
5924 ssl->next_record_offset = 0;
5925 ssl->in_left = 0;
5926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005928 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005929 }
5930
5931 return( ret );
5932 }
5933 else
5934#endif
5935 {
5936 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005937#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5938 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005940 mbedtls_ssl_send_alert_message( ssl,
5941 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5942 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005943 }
5944#endif
5945 return( ret );
5946 }
5947 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005948
Simon Butcher99000142016-10-13 17:21:01 +01005949 return( 0 );
5950}
5951
5952int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5953{
5954 int ret;
5955
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005956 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005957 * Handle particular types of records
5958 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005959 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005960 {
Simon Butcher99000142016-10-13 17:21:01 +01005961 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5962 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005963 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005964 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005965 }
5966
Hanno Beckere678eaa2018-08-21 14:57:46 +01005967 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005968 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005969 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005970 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5972 ssl->in_msglen ) );
5973 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005974 }
5975
Hanno Beckere678eaa2018-08-21 14:57:46 +01005976 if( ssl->in_msg[0] != 1 )
5977 {
5978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5979 ssl->in_msg[0] ) );
5980 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5981 }
5982
5983#if defined(MBEDTLS_SSL_PROTO_DTLS)
5984 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5985 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5986 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5987 {
5988 if( ssl->handshake == NULL )
5989 {
5990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5991 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5992 }
5993
5994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5995 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5996 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005997#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005998 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006001 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006002 if( ssl->in_msglen != 2 )
6003 {
6004 /* Note: Standard allows for more than one 2 byte alert
6005 to be packed in a single message, but Mbed TLS doesn't
6006 currently support this. */
6007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6008 ssl->in_msglen ) );
6009 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6010 }
6011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006013 ssl->in_msg[0], ssl->in_msg[1] ) );
6014
6015 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006016 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006017 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006018 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006021 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006023 }
6024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6026 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6029 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006030 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006031
6032#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6033 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6034 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6035 {
Hanno Becker90333da2017-10-10 11:27:13 +01006036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006037 /* Will be handled when trying to parse ServerHello */
6038 return( 0 );
6039 }
6040#endif
6041
6042#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6043 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6044 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6045 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6046 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6047 {
6048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6049 /* Will be handled in mbedtls_ssl_parse_certificate() */
6050 return( 0 );
6051 }
6052#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6053
6054 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006055 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006056 }
6057
Hanno Beckerc76c6192017-06-06 10:03:17 +01006058#if defined(MBEDTLS_SSL_PROTO_DTLS)
6059 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6060 ssl->handshake != NULL &&
6061 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6062 {
6063 ssl_handshake_wrapup_free_hs_transform( ssl );
6064 }
6065#endif
6066
Paul Bakker5121ce52009-01-03 21:22:43 +00006067 return( 0 );
6068}
6069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006071{
6072 int ret;
6073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006074 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6075 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6076 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006077 {
6078 return( ret );
6079 }
6080
6081 return( 0 );
6082}
6083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006084int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006085 unsigned char level,
6086 unsigned char message )
6087{
6088 int ret;
6089
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006090 if( ssl == NULL || ssl->conf == NULL )
6091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006094 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006096 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006097 ssl->out_msglen = 2;
6098 ssl->out_msg[0] = level;
6099 ssl->out_msg[1] = message;
6100
Hanno Becker67bc7c32018-08-06 11:33:50 +01006101 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006103 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006104 return( ret );
6105 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006107
6108 return( 0 );
6109}
6110
Hanno Beckerb9d44792019-02-08 07:19:04 +00006111#if defined(MBEDTLS_X509_CRT_PARSE_C)
6112static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6113{
6114#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6115 if( session->peer_cert != NULL )
6116 {
6117 mbedtls_x509_crt_free( session->peer_cert );
6118 mbedtls_free( session->peer_cert );
6119 session->peer_cert = NULL;
6120 }
6121#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6122 if( session->peer_cert_digest != NULL )
6123 {
6124 /* Zeroization is not necessary. */
6125 mbedtls_free( session->peer_cert_digest );
6126 session->peer_cert_digest = NULL;
6127 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6128 session->peer_cert_digest_len = 0;
6129 }
6130#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6131}
6132#endif /* MBEDTLS_X509_CRT_PARSE_C */
6133
Paul Bakker5121ce52009-01-03 21:22:43 +00006134/*
6135 * Handshake functions
6136 */
Hanno Becker21489932019-02-05 13:20:55 +00006137#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006138/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006140{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006141 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6142 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006145
Hanno Becker7177a882019-02-05 13:36:46 +00006146 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006149 ssl->state++;
6150 return( 0 );
6151 }
6152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6154 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006155}
6156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006157int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006158{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006159 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6160 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006163
Hanno Becker7177a882019-02-05 13:36:46 +00006164 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006167 ssl->state++;
6168 return( 0 );
6169 }
6170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6172 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006173}
Gilles Peskinef9828522017-05-03 12:28:43 +02006174
Hanno Becker21489932019-02-05 13:20:55 +00006175#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006176/* Some certificate support -> implement write and parse */
6177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006178int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006179{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006180 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006181 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006182 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006183 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6184 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006187
Hanno Becker7177a882019-02-05 13:36:46 +00006188 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006191 ssl->state++;
6192 return( 0 );
6193 }
6194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006195#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006196 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006197 {
6198 if( ssl->client_auth == 0 )
6199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006201 ssl->state++;
6202 return( 0 );
6203 }
6204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006205#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006206 /*
6207 * If using SSLv3 and got no cert, send an Alert message
6208 * (otherwise an empty Certificate message will be sent).
6209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006210 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6211 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006212 {
6213 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006214 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6215 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6216 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006219 goto write_msg;
6220 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006221#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006222 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006223#endif /* MBEDTLS_SSL_CLI_C */
6224#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006225 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6230 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006231 }
6232 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006233#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006236
6237 /*
6238 * 0 . 0 handshake type
6239 * 1 . 3 handshake length
6240 * 4 . 6 length of all certs
6241 * 7 . 9 length of cert. 1
6242 * 10 . n-1 peer certificate
6243 * n . n+2 length of cert. 2
6244 * n+3 . ... upper level cert, etc.
6245 */
6246 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006247 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006248
Paul Bakker29087132010-03-21 21:03:34 +00006249 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006250 {
6251 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006252 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006255 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006256 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006257 }
6258
6259 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6260 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6261 ssl->out_msg[i + 2] = (unsigned char)( n );
6262
6263 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6264 i += n; crt = crt->next;
6265 }
6266
6267 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6268 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6269 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6270
6271 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006272 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6273 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006274
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006275#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006276write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006277#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006278
6279 ssl->state++;
6280
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006281 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006282 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006283 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006284 return( ret );
6285 }
6286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006288
Paul Bakkered27a042013-04-18 22:46:23 +02006289 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006290}
6291
Hanno Becker84879e32019-01-31 07:44:03 +00006292#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006293
6294#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006295static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6296 unsigned char *crt_buf,
6297 size_t crt_buf_len )
6298{
6299 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6300
6301 if( peer_crt == NULL )
6302 return( -1 );
6303
6304 if( peer_crt->raw.len != crt_buf_len )
6305 return( -1 );
6306
Hanno Becker46f34d02019-02-08 14:00:04 +00006307 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006308}
Hanno Becker177475a2019-02-05 17:02:46 +00006309#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6310static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6311 unsigned char *crt_buf,
6312 size_t crt_buf_len )
6313{
6314 int ret;
6315 unsigned char const * const peer_cert_digest =
6316 ssl->session->peer_cert_digest;
6317 mbedtls_md_type_t const peer_cert_digest_type =
6318 ssl->session->peer_cert_digest_type;
6319 mbedtls_md_info_t const * const digest_info =
6320 mbedtls_md_info_from_type( peer_cert_digest_type );
6321 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6322 size_t digest_len;
6323
6324 if( peer_cert_digest == NULL || digest_info == NULL )
6325 return( -1 );
6326
6327 digest_len = mbedtls_md_get_size( digest_info );
6328 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6329 return( -1 );
6330
6331 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6332 if( ret != 0 )
6333 return( -1 );
6334
6335 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6336}
6337#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006338#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006339
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006340/*
6341 * Once the certificate message is read, parse it into a cert chain and
6342 * perform basic checks, but leave actual verification to the caller
6343 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006344static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6345 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006346{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006347 int ret;
6348#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6349 int crt_cnt=0;
6350#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006351 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006352 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006354 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006357 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6358 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006359 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006360 }
6361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006362 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6363 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006366 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6367 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006368 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006369 }
6370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006371 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006372
Paul Bakker5121ce52009-01-03 21:22:43 +00006373 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006374 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006375 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006376 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006377
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006378 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006379 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006382 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6383 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006384 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006385 }
6386
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006387 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6388 i += 3;
6389
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006390 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006391 while( i < ssl->in_hslen )
6392 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006393 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006394 if ( i + 3 > ssl->in_hslen ) {
6395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006396 mbedtls_ssl_send_alert_message( ssl,
6397 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6398 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006399 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6400 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006401 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6402 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006403 if( ssl->in_msg[i] != 0 )
6404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006406 mbedtls_ssl_send_alert_message( ssl,
6407 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6408 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006409 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006410 }
6411
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006412 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006413 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6414 | (unsigned int) ssl->in_msg[i + 2];
6415 i += 3;
6416
6417 if( n < 128 || i + n > ssl->in_hslen )
6418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006420 mbedtls_ssl_send_alert_message( ssl,
6421 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6422 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006423 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006424 }
6425
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006426 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006427#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6428 if( crt_cnt++ == 0 &&
6429 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6430 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006431 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006432 /* During client-side renegotiation, check that the server's
6433 * end-CRTs hasn't changed compared to the initial handshake,
6434 * mitigating the triple handshake attack. On success, reuse
6435 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006436 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6437 if( ssl_check_peer_crt_unchanged( ssl,
6438 &ssl->in_msg[i],
6439 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006440 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6442 mbedtls_ssl_send_alert_message( ssl,
6443 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6444 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6445 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006446 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006447
6448 /* Now we can safely free the original chain. */
6449 ssl_clear_peer_cert( ssl->session );
6450 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006451#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6452
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006453 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006454#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006455 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006456#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006457 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006458 * it in-place from the input buffer instead of making a copy. */
6459 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6460#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006461 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006462 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006463 case 0: /*ok*/
6464 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6465 /* Ignore certificate with an unknown algorithm: maybe a
6466 prior certificate was already trusted. */
6467 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006468
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006469 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6470 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6471 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006472
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006473 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6474 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6475 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006476
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006477 default:
6478 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6479 crt_parse_der_failed:
6480 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6481 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6482 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006483 }
6484
6485 i += n;
6486 }
6487
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006488 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006489 return( 0 );
6490}
6491
Hanno Becker4a55f632019-02-05 12:49:06 +00006492#if defined(MBEDTLS_SSL_SRV_C)
6493static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6494{
6495 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6496 return( -1 );
6497
6498#if defined(MBEDTLS_SSL_PROTO_SSL3)
6499 /*
6500 * Check if the client sent an empty certificate
6501 */
6502 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6503 {
6504 if( ssl->in_msglen == 2 &&
6505 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6506 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6507 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6508 {
6509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6510 return( 0 );
6511 }
6512
6513 return( -1 );
6514 }
6515#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6516
6517#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6518 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6519 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6520 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6521 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6522 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6523 {
6524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6525 return( 0 );
6526 }
6527
6528 return( -1 );
6529#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6530 MBEDTLS_SSL_PROTO_TLS1_2 */
6531}
6532#endif /* MBEDTLS_SSL_SRV_C */
6533
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006534/* Check if a certificate message is expected.
6535 * Return either
6536 * - SSL_CERTIFICATE_EXPECTED, or
6537 * - SSL_CERTIFICATE_SKIP
6538 * indicating whether a Certificate message is expected or not.
6539 */
6540#define SSL_CERTIFICATE_EXPECTED 0
6541#define SSL_CERTIFICATE_SKIP 1
6542static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6543 int authmode )
6544{
6545 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006546 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006547
6548 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6549 return( SSL_CERTIFICATE_SKIP );
6550
6551#if defined(MBEDTLS_SSL_SRV_C)
6552 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6553 {
6554 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6555 return( SSL_CERTIFICATE_SKIP );
6556
6557 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6558 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006559 ssl->session_negotiate->verify_result =
6560 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6561 return( SSL_CERTIFICATE_SKIP );
6562 }
6563 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006564#else
6565 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006566#endif /* MBEDTLS_SSL_SRV_C */
6567
6568 return( SSL_CERTIFICATE_EXPECTED );
6569}
6570
Hanno Becker68636192019-02-05 14:36:34 +00006571static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6572 int authmode,
6573 mbedtls_x509_crt *chain,
6574 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006575{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006576 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006577 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006578 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006579 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006580
Hanno Becker8927c832019-04-03 12:52:50 +01006581 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6582 void *p_vrfy;
6583
Hanno Becker68636192019-02-05 14:36:34 +00006584 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6585 return( 0 );
6586
Hanno Becker8927c832019-04-03 12:52:50 +01006587 if( ssl->f_vrfy != NULL )
6588 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006589 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006590 f_vrfy = ssl->f_vrfy;
6591 p_vrfy = ssl->p_vrfy;
6592 }
6593 else
6594 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006595 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006596 f_vrfy = ssl->conf->f_vrfy;
6597 p_vrfy = ssl->conf->p_vrfy;
6598 }
6599
Hanno Becker68636192019-02-05 14:36:34 +00006600 /*
6601 * Main check: verify certificate
6602 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006603#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6604 if( ssl->conf->f_ca_cb != NULL )
6605 {
6606 ((void) rs_ctx);
6607 have_ca_chain = 1;
6608
6609 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006610 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006611 chain,
6612 ssl->conf->f_ca_cb,
6613 ssl->conf->p_ca_cb,
6614 ssl->conf->cert_profile,
6615 ssl->hostname,
6616 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006617 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006618 }
6619 else
6620#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6621 {
6622 mbedtls_x509_crt *ca_chain;
6623 mbedtls_x509_crl *ca_crl;
6624
6625#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6626 if( ssl->handshake->sni_ca_chain != NULL )
6627 {
6628 ca_chain = ssl->handshake->sni_ca_chain;
6629 ca_crl = ssl->handshake->sni_ca_crl;
6630 }
6631 else
6632#endif
6633 {
6634 ca_chain = ssl->conf->ca_chain;
6635 ca_crl = ssl->conf->ca_crl;
6636 }
6637
6638 if( ca_chain != NULL )
6639 have_ca_chain = 1;
6640
6641 ret = mbedtls_x509_crt_verify_restartable(
6642 chain,
6643 ca_chain, ca_crl,
6644 ssl->conf->cert_profile,
6645 ssl->hostname,
6646 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006647 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006648 }
Hanno Becker68636192019-02-05 14:36:34 +00006649
6650 if( ret != 0 )
6651 {
6652 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6653 }
6654
6655#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6656 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6657 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6658#endif
6659
6660 /*
6661 * Secondary checks: always done, but change 'ret' only if it was 0
6662 */
6663
6664#if defined(MBEDTLS_ECP_C)
6665 {
6666 const mbedtls_pk_context *pk = &chain->pk;
6667
6668 /* If certificate uses an EC key, make sure the curve is OK */
6669 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6670 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6671 {
6672 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6673
6674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6675 if( ret == 0 )
6676 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6677 }
6678 }
6679#endif /* MBEDTLS_ECP_C */
6680
6681 if( mbedtls_ssl_check_cert_usage( chain,
6682 ciphersuite_info,
6683 ! ssl->conf->endpoint,
6684 &ssl->session_negotiate->verify_result ) != 0 )
6685 {
6686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6687 if( ret == 0 )
6688 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6689 }
6690
6691 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6692 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6693 * with details encoded in the verification flags. All other kinds
6694 * of error codes, including those from the user provided f_vrfy
6695 * functions, are treated as fatal and lead to a failure of
6696 * ssl_parse_certificate even if verification was optional. */
6697 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6698 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6699 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6700 {
6701 ret = 0;
6702 }
6703
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006704 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006705 {
6706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6707 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6708 }
6709
6710 if( ret != 0 )
6711 {
6712 uint8_t alert;
6713
6714 /* The certificate may have been rejected for several reasons.
6715 Pick one and send the corresponding alert. Which alert to send
6716 may be a subject of debate in some cases. */
6717 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6718 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6719 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6720 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6721 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6722 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6723 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6724 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6725 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6726 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6727 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6728 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6729 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6730 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6731 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6732 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6733 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6734 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6735 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6736 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6737 else
6738 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6739 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6740 alert );
6741 }
6742
6743#if defined(MBEDTLS_DEBUG_C)
6744 if( ssl->session_negotiate->verify_result != 0 )
6745 {
6746 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6747 ssl->session_negotiate->verify_result ) );
6748 }
6749 else
6750 {
6751 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6752 }
6753#endif /* MBEDTLS_DEBUG_C */
6754
6755 return( ret );
6756}
6757
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006758#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6759static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6760 unsigned char *start, size_t len )
6761{
6762 int ret;
6763 /* Remember digest of the peer's end-CRT. */
6764 ssl->session_negotiate->peer_cert_digest =
6765 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6766 if( ssl->session_negotiate->peer_cert_digest == NULL )
6767 {
6768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6769 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6770 mbedtls_ssl_send_alert_message( ssl,
6771 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6772 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6773
6774 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6775 }
6776
6777 ret = mbedtls_md( mbedtls_md_info_from_type(
6778 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6779 start, len,
6780 ssl->session_negotiate->peer_cert_digest );
6781
6782 ssl->session_negotiate->peer_cert_digest_type =
6783 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6784 ssl->session_negotiate->peer_cert_digest_len =
6785 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6786
6787 return( ret );
6788}
6789
6790static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6791 unsigned char *start, size_t len )
6792{
6793 unsigned char *end = start + len;
6794 int ret;
6795
6796 /* Make a copy of the peer's raw public key. */
6797 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6798 ret = mbedtls_pk_parse_subpubkey( &start, end,
6799 &ssl->handshake->peer_pubkey );
6800 if( ret != 0 )
6801 {
6802 /* We should have parsed the public key before. */
6803 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6804 }
6805
6806 return( 0 );
6807}
6808#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6809
Hanno Becker68636192019-02-05 14:36:34 +00006810int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6811{
6812 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006813 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006814#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6815 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6816 ? ssl->handshake->sni_authmode
6817 : ssl->conf->authmode;
6818#else
6819 const int authmode = ssl->conf->authmode;
6820#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006821 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006822 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006823
6824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6825
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006826 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6827 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006828 {
6829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006830 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006831 }
6832
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006833#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6834 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006835 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006836 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006837 chain = ssl->handshake->ecrs_peer_cert;
6838 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006839 goto crt_verify;
6840 }
6841#endif
6842
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006843 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006844 {
6845 /* mbedtls_ssl_read_record may have sent an alert already. We
6846 let it decide whether to alert. */
6847 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006848 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006849 }
6850
Hanno Becker4a55f632019-02-05 12:49:06 +00006851#if defined(MBEDTLS_SSL_SRV_C)
6852 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6853 {
6854 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006855
6856 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006857 ret = 0;
6858 else
6859 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006860
Hanno Becker6bdfab22019-02-05 13:11:17 +00006861 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006862 }
6863#endif /* MBEDTLS_SSL_SRV_C */
6864
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006865 /* Clear existing peer CRT structure in case we tried to
6866 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006867 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006868
6869 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6870 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006871 {
6872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6873 sizeof( mbedtls_x509_crt ) ) );
6874 mbedtls_ssl_send_alert_message( ssl,
6875 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6876 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006877
Hanno Becker3dad3112019-02-05 17:19:52 +00006878 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6879 goto exit;
6880 }
6881 mbedtls_x509_crt_init( chain );
6882
6883 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006884 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006885 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006886
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006887#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6888 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006889 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006890
6891crt_verify:
6892 if( ssl->handshake->ecrs_enabled)
6893 rs_ctx = &ssl->handshake->ecrs_ctx;
6894#endif
6895
Hanno Becker68636192019-02-05 14:36:34 +00006896 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006897 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006898 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006899 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006900
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006901#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006902 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006903 unsigned char *crt_start, *pk_start;
6904 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006905
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006906 /* We parse the CRT chain without copying, so
6907 * these pointers point into the input buffer,
6908 * and are hence still valid after freeing the
6909 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006910
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006911 crt_start = chain->raw.p;
6912 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006913
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006914 pk_start = chain->pk_raw.p;
6915 pk_len = chain->pk_raw.len;
6916
6917 /* Free the CRT structures before computing
6918 * digest and copying the peer's public key. */
6919 mbedtls_x509_crt_free( chain );
6920 mbedtls_free( chain );
6921 chain = NULL;
6922
6923 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006924 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006925 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006926
6927 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6928 if( ret != 0 )
6929 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006930 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006931#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6932 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006933 ssl->session_negotiate->peer_cert = chain;
6934 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006935#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006938
Hanno Becker6bdfab22019-02-05 13:11:17 +00006939exit:
6940
Hanno Becker3dad3112019-02-05 17:19:52 +00006941 if( ret == 0 )
6942 ssl->state++;
6943
6944#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6945 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6946 {
6947 ssl->handshake->ecrs_peer_cert = chain;
6948 chain = NULL;
6949 }
6950#endif
6951
6952 if( chain != NULL )
6953 {
6954 mbedtls_x509_crt_free( chain );
6955 mbedtls_free( chain );
6956 }
6957
Paul Bakker5121ce52009-01-03 21:22:43 +00006958 return( ret );
6959}
Hanno Becker21489932019-02-05 13:20:55 +00006960#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006963{
6964 int ret;
6965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006969 ssl->out_msglen = 1;
6970 ssl->out_msg[0] = 1;
6971
Paul Bakker5121ce52009-01-03 21:22:43 +00006972 ssl->state++;
6973
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006974 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006975 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006976 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006977 return( ret );
6978 }
6979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006981
6982 return( 0 );
6983}
6984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006986{
6987 int ret;
6988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006990
Hanno Becker327c93b2018-08-15 13:56:18 +01006991 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006993 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006994 return( ret );
6995 }
6996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007000 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7001 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007003 }
7004
Hanno Beckere678eaa2018-08-21 14:57:46 +01007005 /* CCS records are only accepted if they have length 1 and content '1',
7006 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007007
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007008 /*
7009 * Switch to our negotiated transform and session parameters for inbound
7010 * data.
7011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007012 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007013 ssl->transform_in = ssl->transform_negotiate;
7014 ssl->session_in = ssl->session_negotiate;
7015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007016#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007017 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007020 ssl_dtls_replay_reset( ssl );
7021#endif
7022
7023 /* Increment epoch */
7024 if( ++ssl->in_epoch == 0 )
7025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007027 /* This is highly unlikely to happen for legitimate reasons, so
7028 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007029 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007030 }
7031 }
7032 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007033#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007034 memset( ssl->in_ctr, 0, 8 );
7035
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007036 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007038#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7039 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007044 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7045 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007046 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007047 }
7048 }
7049#endif
7050
Paul Bakker5121ce52009-01-03 21:22:43 +00007051 ssl->state++;
7052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007054
7055 return( 0 );
7056}
7057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007058void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7059 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007060{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007061 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007063#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7064 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7065 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007066 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007067 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007068#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007069#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7070#if defined(MBEDTLS_SHA512_C)
7071 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007072 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7073 else
7074#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075#if defined(MBEDTLS_SHA256_C)
7076 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007077 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007078 else
7079#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007080#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007083 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007084 }
Paul Bakker380da532012-04-18 16:10:25 +00007085}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007087void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007089#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7090 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007091 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7092 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007093#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7095#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007096#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007097 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007098 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7099#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007100 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007101#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007102#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007103#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007104#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007105 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007106 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007107#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007108 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007109#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007110#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007112}
7113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007115 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007116{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7118 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007119 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7120 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007121#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007122#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7123#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007124#if defined(MBEDTLS_USE_PSA_CRYPTO)
7125 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7126#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007127 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007128#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007129#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007131#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007132 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007133#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007134 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007135#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007136#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007137#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007138}
7139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7141 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7142static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007143 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007144{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007145 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7146 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007147}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007148#endif
Paul Bakker380da532012-04-18 16:10:25 +00007149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007150#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7151#if defined(MBEDTLS_SHA256_C)
7152static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007153 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007154{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007155#if defined(MBEDTLS_USE_PSA_CRYPTO)
7156 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7157#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007158 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007159#endif
Paul Bakker380da532012-04-18 16:10:25 +00007160}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007161#endif
Paul Bakker380da532012-04-18 16:10:25 +00007162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163#if defined(MBEDTLS_SHA512_C)
7164static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007165 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007166{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007167#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007168 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007169#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007170 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007171#endif
Paul Bakker380da532012-04-18 16:10:25 +00007172}
Paul Bakker769075d2012-11-24 11:26:46 +01007173#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007174#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007176#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007177static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007178 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007179{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007180 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007181 mbedtls_md5_context md5;
7182 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007183
Paul Bakker5121ce52009-01-03 21:22:43 +00007184 unsigned char padbuf[48];
7185 unsigned char md5sum[16];
7186 unsigned char sha1sum[20];
7187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007188 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007189 if( !session )
7190 session = ssl->session;
7191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007193
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007194 mbedtls_md5_init( &md5 );
7195 mbedtls_sha1_init( &sha1 );
7196
7197 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7198 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007199
7200 /*
7201 * SSLv3:
7202 * hash =
7203 * MD5( master + pad2 +
7204 * MD5( handshake + sender + master + pad1 ) )
7205 * + SHA1( master + pad2 +
7206 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007207 */
7208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007209#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007210 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7211 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007212#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007215 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7216 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007217#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007219 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007220 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007221
Paul Bakker1ef83d62012-04-11 12:09:53 +00007222 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007223
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007224 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7225 mbedtls_md5_update_ret( &md5, session->master, 48 );
7226 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7227 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007228
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007229 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7230 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7231 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7232 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007233
Paul Bakker1ef83d62012-04-11 12:09:53 +00007234 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007235
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007236 mbedtls_md5_starts_ret( &md5 );
7237 mbedtls_md5_update_ret( &md5, session->master, 48 );
7238 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7239 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7240 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007241
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007242 mbedtls_sha1_starts_ret( &sha1 );
7243 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7244 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7245 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7246 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007248 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007249
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007250 mbedtls_md5_free( &md5 );
7251 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007252
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007253 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7254 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7255 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007258}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007262static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007264{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007265 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007266 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007267 mbedtls_md5_context md5;
7268 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007269 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007271 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007272 if( !session )
7273 session = ssl->session;
7274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007276
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007277 mbedtls_md5_init( &md5 );
7278 mbedtls_sha1_init( &sha1 );
7279
7280 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7281 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007282
Paul Bakker1ef83d62012-04-11 12:09:53 +00007283 /*
7284 * TLSv1:
7285 * hash = PRF( master, finished_label,
7286 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7287 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007290 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7291 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007292#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007294#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007295 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7296 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007297#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007300 ? "client finished"
7301 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007302
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007303 mbedtls_md5_finish_ret( &md5, padbuf );
7304 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007305
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007306 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007307 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007309 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007310
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007311 mbedtls_md5_free( &md5 );
7312 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007313
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007314 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007317}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7321#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007322static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007323 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007324{
7325 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007326 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007327 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007328#if defined(MBEDTLS_USE_PSA_CRYPTO)
7329 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007330 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007331 psa_status_t status;
7332#else
7333 mbedtls_sha256_context sha256;
7334#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007337 if( !session )
7338 session = ssl->session;
7339
Andrzej Kurekeb342242019-01-29 09:14:33 -05007340 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7341 ? "client finished"
7342 : "server finished";
7343
7344#if defined(MBEDTLS_USE_PSA_CRYPTO)
7345 sha256_psa = psa_hash_operation_init();
7346
7347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7348
7349 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7350 if( status != PSA_SUCCESS )
7351 {
7352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7353 return;
7354 }
7355
7356 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7357 if( status != PSA_SUCCESS )
7358 {
7359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7360 return;
7361 }
7362 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7363#else
7364
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007365 mbedtls_sha256_init( &sha256 );
7366
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007368
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007369 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007370
7371 /*
7372 * TLSv1.2:
7373 * hash = PRF( master, finished_label,
7374 * Hash( handshake ) )[0.11]
7375 */
7376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377#if !defined(MBEDTLS_SHA256_ALT)
7378 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007379 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007380#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007381
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007382 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007383 mbedtls_sha256_free( &sha256 );
7384#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007385
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007386 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007387 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007390
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007391 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007394}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007397#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007398static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007400{
7401 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007402 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007403 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007404#if defined(MBEDTLS_USE_PSA_CRYPTO)
7405 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007406 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007407 psa_status_t status;
7408#else
7409 mbedtls_sha512_context sha512;
7410#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007412 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007413 if( !session )
7414 session = ssl->session;
7415
Andrzej Kurekeb342242019-01-29 09:14:33 -05007416 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7417 ? "client finished"
7418 : "server finished";
7419
7420#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007421 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007422
7423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7424
Andrzej Kurek972fba52019-01-30 03:29:12 -05007425 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007426 if( status != PSA_SUCCESS )
7427 {
7428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7429 return;
7430 }
7431
Andrzej Kurek972fba52019-01-30 03:29:12 -05007432 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007433 if( status != PSA_SUCCESS )
7434 {
7435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7436 return;
7437 }
7438 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7439#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007440 mbedtls_sha512_init( &sha512 );
7441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007443
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007444 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007445
7446 /*
7447 * TLSv1.2:
7448 * hash = PRF( master, finished_label,
7449 * Hash( handshake ) )[0.11]
7450 */
7451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007452#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007453 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7454 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007455#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007456
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007457 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007458 mbedtls_sha512_free( &sha512 );
7459#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007460
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007461 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007462 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007465
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007466 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007469}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470#endif /* MBEDTLS_SHA512_C */
7471#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007473static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007474{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007475 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007476
7477 /*
7478 * Free our handshake params
7479 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007480 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007481 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007482 ssl->handshake = NULL;
7483
7484 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007485 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007486 */
7487 if( ssl->transform )
7488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007489 mbedtls_ssl_transform_free( ssl->transform );
7490 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007491 }
7492 ssl->transform = ssl->transform_negotiate;
7493 ssl->transform_negotiate = NULL;
7494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007496}
7497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007499{
7500 int resume = ssl->handshake->resume;
7501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504#if defined(MBEDTLS_SSL_RENEGOTIATION)
7505 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007508 ssl->renego_records_seen = 0;
7509 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007510#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007511
7512 /*
7513 * Free the previous session and switch in the current one
7514 */
Paul Bakker0a597072012-09-25 21:55:46 +00007515 if( ssl->session )
7516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007518 /* RFC 7366 3.1: keep the EtM state */
7519 ssl->session_negotiate->encrypt_then_mac =
7520 ssl->session->encrypt_then_mac;
7521#endif
7522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523 mbedtls_ssl_session_free( ssl->session );
7524 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007525 }
7526 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007527 ssl->session_negotiate = NULL;
7528
Paul Bakker0a597072012-09-25 21:55:46 +00007529 /*
7530 * Add cache entry
7531 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007532 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007533 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007534 resume == 0 )
7535 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007536 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007538 }
Paul Bakker0a597072012-09-25 21:55:46 +00007539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007541 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007542 ssl->handshake->flight != NULL )
7543 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007544 /* Cancel handshake timer */
7545 ssl_set_timer( ssl, 0 );
7546
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007547 /* Keep last flight around in case we need to resend it:
7548 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007550 }
7551 else
7552#endif
7553 ssl_handshake_wrapup_free_hs_transform( ssl );
7554
Paul Bakker48916f92012-09-16 19:57:18 +00007555 ssl->state++;
7556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007558}
7559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007561{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007562 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007565
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007566 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007567
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007568 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007569
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007570 /*
7571 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7572 * may define some other value. Currently (early 2016), no defined
7573 * ciphersuite does this (and this is unlikely to change as activity has
7574 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007576 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007578#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007579 ssl->verify_data_len = hash_len;
7580 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007581#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007582
Paul Bakker5121ce52009-01-03 21:22:43 +00007583 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7585 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007586
7587 /*
7588 * In case of session resuming, invert the client and server
7589 * ChangeCipherSpec messages order.
7590 */
Paul Bakker0a597072012-09-25 21:55:46 +00007591 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007594 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007595 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007596#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007597#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007598 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007600#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007601 }
7602 else
7603 ssl->state++;
7604
Paul Bakker48916f92012-09-16 19:57:18 +00007605 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007606 * Switch to our negotiated transform and session parameters for outbound
7607 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007612 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007613 {
7614 unsigned char i;
7615
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007616 /* Remember current epoch settings for resending */
7617 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007618 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007619
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007620 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007621 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007622
7623 /* Increment epoch */
7624 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007625 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007626 break;
7627
7628 /* The loop goes to its end iff the counter is wrapping */
7629 if( i == 0 )
7630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7632 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007633 }
7634 }
7635 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007637 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007638
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007639 ssl->transform_out = ssl->transform_negotiate;
7640 ssl->session_out = ssl->session_negotiate;
7641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7643 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007647 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7648 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007649 }
7650 }
7651#endif
7652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007653#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007654 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007656#endif
7657
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007658 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007659 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007660 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007661 return( ret );
7662 }
7663
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007664#if defined(MBEDTLS_SSL_PROTO_DTLS)
7665 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7666 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7667 {
7668 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7669 return( ret );
7670 }
7671#endif
7672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007674
7675 return( 0 );
7676}
7677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007678#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007679#define SSL_MAX_HASH_LEN 36
7680#else
7681#define SSL_MAX_HASH_LEN 12
7682#endif
7683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007684int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007685{
Paul Bakker23986e52011-04-24 08:57:21 +00007686 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007687 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007688 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007691
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007692 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007693
Hanno Becker327c93b2018-08-15 13:56:18 +01007694 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007696 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007697 return( ret );
7698 }
7699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007703 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7704 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007706 }
7707
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007708 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709#if defined(MBEDTLS_SSL_PROTO_SSL3)
7710 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007711 hash_len = 36;
7712 else
7713#endif
7714 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7717 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007720 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7721 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007723 }
7724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007726 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007729 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7730 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007732 }
7733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007735 ssl->verify_data_len = hash_len;
7736 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007737#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007738
Paul Bakker0a597072012-09-25 21:55:46 +00007739 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007741#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007742 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007744#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007746 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007747 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007748#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007749 }
7750 else
7751 ssl->state++;
7752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007753#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007754 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007755 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007756#endif
7757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007759
7760 return( 0 );
7761}
7762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007763static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007764{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7768 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7769 mbedtls_md5_init( &handshake->fin_md5 );
7770 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007771 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7772 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007773#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7775#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007776#if defined(MBEDTLS_USE_PSA_CRYPTO)
7777 handshake->fin_sha256_psa = psa_hash_operation_init();
7778 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7779#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007780 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007781 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007782#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007783#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007785#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007786 handshake->fin_sha384_psa = psa_hash_operation_init();
7787 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007788#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007789 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007790 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007791#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007792#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007793#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007794
7795 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007796
7797#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7798 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7799 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7800#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802#if defined(MBEDTLS_DHM_C)
7803 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007804#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007805#if defined(MBEDTLS_ECDH_C)
7806 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007807#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007808#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007809 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007810#if defined(MBEDTLS_SSL_CLI_C)
7811 handshake->ecjpake_cache = NULL;
7812 handshake->ecjpake_cache_len = 0;
7813#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007814#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007815
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007816#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007817 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007818#endif
7819
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007820#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7821 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7822#endif
Hanno Becker75173122019-02-06 16:18:31 +00007823
7824#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7825 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7826 mbedtls_pk_init( &handshake->peer_pubkey );
7827#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007828}
7829
Hanno Beckera18d1322018-01-03 14:27:32 +00007830void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007831{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7835 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007836
Hanno Beckerd56ed242018-01-03 15:32:51 +00007837#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838 mbedtls_md_init( &transform->md_ctx_enc );
7839 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007840#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007841}
7842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007844{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007846}
7847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007848static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007849{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007850 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007851 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007853 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007855 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007856 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007857
7858 /*
7859 * Either the pointers are now NULL or cleared properly and can be freed.
7860 * Now allocate missing structures.
7861 */
7862 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007863 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007864 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007865 }
Paul Bakker48916f92012-09-16 19:57:18 +00007866
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007867 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007868 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007869 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007870 }
Paul Bakker48916f92012-09-16 19:57:18 +00007871
Paul Bakker82788fb2014-10-20 13:59:19 +02007872 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007873 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007874 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007875 }
Paul Bakker48916f92012-09-16 19:57:18 +00007876
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007877 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007878 if( ssl->handshake == NULL ||
7879 ssl->transform_negotiate == NULL ||
7880 ssl->session_negotiate == NULL )
7881 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884 mbedtls_free( ssl->handshake );
7885 mbedtls_free( ssl->transform_negotiate );
7886 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007887
7888 ssl->handshake = NULL;
7889 ssl->transform_negotiate = NULL;
7890 ssl->session_negotiate = NULL;
7891
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007892 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007893 }
7894
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007895 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007897 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007898 ssl_handshake_params_init( ssl->handshake );
7899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007901 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7902 {
7903 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007904
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007905 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7906 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7907 else
7908 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007909
7910 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007911 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007912#endif
7913
Paul Bakker48916f92012-09-16 19:57:18 +00007914 return( 0 );
7915}
7916
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007917#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007918/* Dummy cookie callbacks for defaults */
7919static int ssl_cookie_write_dummy( void *ctx,
7920 unsigned char **p, unsigned char *end,
7921 const unsigned char *cli_id, size_t cli_id_len )
7922{
7923 ((void) ctx);
7924 ((void) p);
7925 ((void) end);
7926 ((void) cli_id);
7927 ((void) cli_id_len);
7928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007930}
7931
7932static int ssl_cookie_check_dummy( void *ctx,
7933 const unsigned char *cookie, size_t cookie_len,
7934 const unsigned char *cli_id, size_t cli_id_len )
7935{
7936 ((void) ctx);
7937 ((void) cookie);
7938 ((void) cookie_len);
7939 ((void) cli_id);
7940 ((void) cli_id_len);
7941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007942 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007943}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007944#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007945
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007946/* Once ssl->out_hdr as the address of the beginning of the
7947 * next outgoing record is set, deduce the other pointers.
7948 *
7949 * Note: For TLS, we save the implicit record sequence number
7950 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7951 * and the caller has to make sure there's space for this.
7952 */
7953
7954static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7955 mbedtls_ssl_transform *transform )
7956{
7957#if defined(MBEDTLS_SSL_PROTO_DTLS)
7958 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7959 {
7960 ssl->out_ctr = ssl->out_hdr + 3;
7961 ssl->out_len = ssl->out_hdr + 11;
7962 ssl->out_iv = ssl->out_hdr + 13;
7963 }
7964 else
7965#endif
7966 {
7967 ssl->out_ctr = ssl->out_hdr - 8;
7968 ssl->out_len = ssl->out_hdr + 3;
7969 ssl->out_iv = ssl->out_hdr + 5;
7970 }
7971
7972 /* Adjust out_msg to make space for explicit IV, if used. */
7973 if( transform != NULL &&
7974 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7975 {
7976 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7977 }
7978 else
7979 ssl->out_msg = ssl->out_iv;
7980}
7981
7982/* Once ssl->in_hdr as the address of the beginning of the
7983 * next incoming record is set, deduce the other pointers.
7984 *
7985 * Note: For TLS, we save the implicit record sequence number
7986 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7987 * and the caller has to make sure there's space for this.
7988 */
7989
7990static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7991 mbedtls_ssl_transform *transform )
7992{
7993#if defined(MBEDTLS_SSL_PROTO_DTLS)
7994 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7995 {
7996 ssl->in_ctr = ssl->in_hdr + 3;
7997 ssl->in_len = ssl->in_hdr + 11;
7998 ssl->in_iv = ssl->in_hdr + 13;
7999 }
8000 else
8001#endif
8002 {
8003 ssl->in_ctr = ssl->in_hdr - 8;
8004 ssl->in_len = ssl->in_hdr + 3;
8005 ssl->in_iv = ssl->in_hdr + 5;
8006 }
8007
8008 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
8009 if( transform != NULL &&
8010 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8011 {
8012 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
8013 }
8014 else
8015 ssl->in_msg = ssl->in_iv;
8016}
8017
Paul Bakker5121ce52009-01-03 21:22:43 +00008018/*
8019 * Initialize an SSL context
8020 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008021void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8022{
8023 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8024}
8025
8026/*
8027 * Setup an SSL context
8028 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008029
8030static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8031{
8032 /* Set the incoming and outgoing record pointers. */
8033#if defined(MBEDTLS_SSL_PROTO_DTLS)
8034 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8035 {
8036 ssl->out_hdr = ssl->out_buf;
8037 ssl->in_hdr = ssl->in_buf;
8038 }
8039 else
8040#endif /* MBEDTLS_SSL_PROTO_DTLS */
8041 {
8042 ssl->out_hdr = ssl->out_buf + 8;
8043 ssl->in_hdr = ssl->in_buf + 8;
8044 }
8045
8046 /* Derive other internal pointers. */
8047 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
8048 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
8049}
8050
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008051int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008052 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008053{
Paul Bakker48916f92012-09-16 19:57:18 +00008054 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008055
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008056 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008057
8058 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008059 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008060 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008061
8062 /* Set to NULL in case of an error condition */
8063 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008064
Angus Grattond8213d02016-05-25 20:56:48 +10008065 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8066 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008067 {
Angus Grattond8213d02016-05-25 20:56:48 +10008068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008069 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008070 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008071 }
8072
8073 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8074 if( ssl->out_buf == NULL )
8075 {
8076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008077 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008078 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008079 }
8080
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008081 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008082
Paul Bakker48916f92012-09-16 19:57:18 +00008083 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008084 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008085
8086 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008087
8088error:
8089 mbedtls_free( ssl->in_buf );
8090 mbedtls_free( ssl->out_buf );
8091
8092 ssl->conf = NULL;
8093
8094 ssl->in_buf = NULL;
8095 ssl->out_buf = NULL;
8096
8097 ssl->in_hdr = NULL;
8098 ssl->in_ctr = NULL;
8099 ssl->in_len = NULL;
8100 ssl->in_iv = NULL;
8101 ssl->in_msg = NULL;
8102
8103 ssl->out_hdr = NULL;
8104 ssl->out_ctr = NULL;
8105 ssl->out_len = NULL;
8106 ssl->out_iv = NULL;
8107 ssl->out_msg = NULL;
8108
k-stachowiak9f7798e2018-07-31 16:52:32 +02008109 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008110}
8111
8112/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008113 * Reset an initialized and used SSL context for re-use while retaining
8114 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008115 *
8116 * If partial is non-zero, keep data in the input buffer and client ID.
8117 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008118 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008119static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008120{
Paul Bakker48916f92012-09-16 19:57:18 +00008121 int ret;
8122
Hanno Becker7e772132018-08-10 12:38:21 +01008123#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8124 !defined(MBEDTLS_SSL_SRV_C)
8125 ((void) partial);
8126#endif
8127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008128 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008129
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008130 /* Cancel any possibly running timer */
8131 ssl_set_timer( ssl, 0 );
8132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008133#if defined(MBEDTLS_SSL_RENEGOTIATION)
8134 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008135 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008136
8137 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8139 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008140#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008142
Paul Bakker7eb013f2011-10-06 12:37:39 +00008143 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008144 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008145
8146 ssl->in_msgtype = 0;
8147 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008148#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008149 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008150 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008151#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008152#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008153 ssl_dtls_replay_reset( ssl );
8154#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008155
8156 ssl->in_hslen = 0;
8157 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008158
8159 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008160
8161 ssl->out_msgtype = 0;
8162 ssl->out_msglen = 0;
8163 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8165 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008166 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008167#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008168
Hanno Becker19859472018-08-06 09:40:20 +01008169 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8170
Paul Bakker48916f92012-09-16 19:57:18 +00008171 ssl->transform_in = NULL;
8172 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008173
Hanno Becker78640902018-08-13 16:35:15 +01008174 ssl->session_in = NULL;
8175 ssl->session_out = NULL;
8176
Angus Grattond8213d02016-05-25 20:56:48 +10008177 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008178
8179#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008180 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008181#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8182 {
8183 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008184 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008185 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008187#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8188 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8191 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008193 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8194 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008195 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008196 }
8197#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008198
Paul Bakker48916f92012-09-16 19:57:18 +00008199 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201 mbedtls_ssl_transform_free( ssl->transform );
8202 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008203 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008204 }
Paul Bakker48916f92012-09-16 19:57:18 +00008205
Paul Bakkerc0463502013-02-14 11:19:38 +01008206 if( ssl->session )
8207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008208 mbedtls_ssl_session_free( ssl->session );
8209 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008210 ssl->session = NULL;
8211 }
8212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008213#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008214 ssl->alpn_chosen = NULL;
8215#endif
8216
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008217#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008218#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008219 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008220#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008221 {
8222 mbedtls_free( ssl->cli_id );
8223 ssl->cli_id = NULL;
8224 ssl->cli_id_len = 0;
8225 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008226#endif
8227
Paul Bakker48916f92012-09-16 19:57:18 +00008228 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8229 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008230
8231 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008232}
8233
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008234/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008235 * Reset an initialized and used SSL context for re-use while retaining
8236 * all application-set variables, function pointers and data.
8237 */
8238int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8239{
8240 return( ssl_session_reset_int( ssl, 0 ) );
8241}
8242
8243/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008244 * SSL set accessors
8245 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008246void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008247{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008248 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008249}
8250
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008251void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008252{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008253 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008254}
8255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008256#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008257void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008258{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008259 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008260}
8261#endif
8262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008263#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008264void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008265{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008266 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008267}
8268#endif
8269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008270#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008271
Hanno Becker1841b0a2018-08-24 11:13:57 +01008272void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8273 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008274{
8275 ssl->disable_datagram_packing = !allow_packing;
8276}
8277
8278void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8279 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008280{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008281 conf->hs_timeout_min = min;
8282 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008283}
8284#endif
8285
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008286void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008287{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008288 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008289}
8290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008291#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008292void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008293 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008294 void *p_vrfy )
8295{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008296 conf->f_vrfy = f_vrfy;
8297 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008298}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008300
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008301void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008302 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008303 void *p_rng )
8304{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008305 conf->f_rng = f_rng;
8306 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008307}
8308
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008309void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008310 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008311 void *p_dbg )
8312{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008313 conf->f_dbg = f_dbg;
8314 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008315}
8316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008317void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008318 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008319 mbedtls_ssl_send_t *f_send,
8320 mbedtls_ssl_recv_t *f_recv,
8321 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008322{
8323 ssl->p_bio = p_bio;
8324 ssl->f_send = f_send;
8325 ssl->f_recv = f_recv;
8326 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008327}
8328
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008329#if defined(MBEDTLS_SSL_PROTO_DTLS)
8330void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8331{
8332 ssl->mtu = mtu;
8333}
8334#endif
8335
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008336void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008337{
8338 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008339}
8340
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008341void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8342 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008343 mbedtls_ssl_set_timer_t *f_set_timer,
8344 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008345{
8346 ssl->p_timer = p_timer;
8347 ssl->f_set_timer = f_set_timer;
8348 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008349
8350 /* Make sure we start with no timer running */
8351 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008352}
8353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008355void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008356 void *p_cache,
8357 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8358 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008359{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008360 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008361 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008362 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008363}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008364#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008366#if defined(MBEDTLS_SSL_CLI_C)
8367int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008368{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008369 int ret;
8370
8371 if( ssl == NULL ||
8372 session == NULL ||
8373 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008374 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008376 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008377 }
8378
Hanno Becker52055ae2019-02-06 14:30:46 +00008379 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8380 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008381 return( ret );
8382
Paul Bakker0a597072012-09-25 21:55:46 +00008383 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008384
8385 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008386}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008387#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008388
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008389void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008390 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008391{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008392 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8393 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8394 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8395 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008396}
8397
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008398void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008399 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008400 int major, int minor )
8401{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008402 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008403 return;
8404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008406 return;
8407
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008408 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008409}
8410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008411#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008412void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008413 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008414{
8415 conf->cert_profile = profile;
8416}
8417
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008418/* Append a new keycert entry to a (possibly empty) list */
8419static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8420 mbedtls_x509_crt *cert,
8421 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008422{
niisato8ee24222018-06-25 19:05:48 +09008423 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008424
niisato8ee24222018-06-25 19:05:48 +09008425 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8426 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008427 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008428
niisato8ee24222018-06-25 19:05:48 +09008429 new_cert->cert = cert;
8430 new_cert->key = key;
8431 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008432
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008433 /* Update head is the list was null, else add to the end */
8434 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008435 {
niisato8ee24222018-06-25 19:05:48 +09008436 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008437 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008438 else
8439 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008440 mbedtls_ssl_key_cert *cur = *head;
8441 while( cur->next != NULL )
8442 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008443 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008444 }
8445
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008446 return( 0 );
8447}
8448
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008449int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008450 mbedtls_x509_crt *own_cert,
8451 mbedtls_pk_context *pk_key )
8452{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008453 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008454}
8455
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008456void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008457 mbedtls_x509_crt *ca_chain,
8458 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008459{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008460 conf->ca_chain = ca_chain;
8461 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008462
8463#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8464 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8465 * cannot be used together. */
8466 conf->f_ca_cb = NULL;
8467 conf->p_ca_cb = NULL;
8468#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008469}
Hanno Becker5adaad92019-03-27 16:54:37 +00008470
8471#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8472void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008473 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008474 void *p_ca_cb )
8475{
8476 conf->f_ca_cb = f_ca_cb;
8477 conf->p_ca_cb = p_ca_cb;
8478
8479 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8480 * cannot be used together. */
8481 conf->ca_chain = NULL;
8482 conf->ca_crl = NULL;
8483}
8484#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008485#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008486
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008487#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8488int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8489 mbedtls_x509_crt *own_cert,
8490 mbedtls_pk_context *pk_key )
8491{
8492 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8493 own_cert, pk_key ) );
8494}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008495
8496void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8497 mbedtls_x509_crt *ca_chain,
8498 mbedtls_x509_crl *ca_crl )
8499{
8500 ssl->handshake->sni_ca_chain = ca_chain;
8501 ssl->handshake->sni_ca_crl = ca_crl;
8502}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008503
8504void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8505 int authmode )
8506{
8507 ssl->handshake->sni_authmode = authmode;
8508}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008509#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8510
Hanno Becker8927c832019-04-03 12:52:50 +01008511#if defined(MBEDTLS_X509_CRT_PARSE_C)
8512void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8513 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8514 void *p_vrfy )
8515{
8516 ssl->f_vrfy = f_vrfy;
8517 ssl->p_vrfy = p_vrfy;
8518}
8519#endif
8520
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008521#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008522/*
8523 * Set EC J-PAKE password for current handshake
8524 */
8525int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8526 const unsigned char *pw,
8527 size_t pw_len )
8528{
8529 mbedtls_ecjpake_role role;
8530
Janos Follath8eb64132016-06-03 15:40:57 +01008531 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008532 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8533
8534 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8535 role = MBEDTLS_ECJPAKE_SERVER;
8536 else
8537 role = MBEDTLS_ECJPAKE_CLIENT;
8538
8539 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8540 role,
8541 MBEDTLS_MD_SHA256,
8542 MBEDTLS_ECP_DP_SECP256R1,
8543 pw, pw_len ) );
8544}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008545#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008547#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008548
8549static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8550{
8551 /* Remove reference to existing PSK, if any. */
8552#if defined(MBEDTLS_USE_PSA_CRYPTO)
8553 if( conf->psk_opaque != 0 )
8554 {
8555 /* The maintenance of the PSK key slot is the
8556 * user's responsibility. */
8557 conf->psk_opaque = 0;
8558 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008559 /* This and the following branch should never
8560 * be taken simultaenously as we maintain the
8561 * invariant that raw and opaque PSKs are never
8562 * configured simultaneously. As a safeguard,
8563 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008564#endif /* MBEDTLS_USE_PSA_CRYPTO */
8565 if( conf->psk != NULL )
8566 {
8567 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8568
8569 mbedtls_free( conf->psk );
8570 conf->psk = NULL;
8571 conf->psk_len = 0;
8572 }
8573
8574 /* Remove reference to PSK identity, if any. */
8575 if( conf->psk_identity != NULL )
8576 {
8577 mbedtls_free( conf->psk_identity );
8578 conf->psk_identity = NULL;
8579 conf->psk_identity_len = 0;
8580 }
8581}
8582
Hanno Becker7390c712018-11-15 13:33:04 +00008583/* This function assumes that PSK identity in the SSL config is unset.
8584 * It checks that the provided identity is well-formed and attempts
8585 * to make a copy of it in the SSL config.
8586 * On failure, the PSK identity in the config remains unset. */
8587static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8588 unsigned char const *psk_identity,
8589 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008590{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008591 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008592 if( psk_identity == NULL ||
8593 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008594 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008595 {
8596 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8597 }
8598
Hanno Becker7390c712018-11-15 13:33:04 +00008599 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8600 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008601 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008602
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008603 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008604 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008605
8606 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008607}
8608
Hanno Becker7390c712018-11-15 13:33:04 +00008609int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8610 const unsigned char *psk, size_t psk_len,
8611 const unsigned char *psk_identity, size_t psk_identity_len )
8612{
8613 int ret;
8614 /* Remove opaque/raw PSK + PSK Identity */
8615 ssl_conf_remove_psk( conf );
8616
8617 /* Check and set raw PSK */
8618 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8619 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8620 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8621 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8622 conf->psk_len = psk_len;
8623 memcpy( conf->psk, psk, conf->psk_len );
8624
8625 /* Check and set PSK Identity */
8626 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8627 if( ret != 0 )
8628 ssl_conf_remove_psk( conf );
8629
8630 return( ret );
8631}
8632
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008633static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8634{
8635#if defined(MBEDTLS_USE_PSA_CRYPTO)
8636 if( ssl->handshake->psk_opaque != 0 )
8637 {
8638 ssl->handshake->psk_opaque = 0;
8639 }
8640 else
8641#endif /* MBEDTLS_USE_PSA_CRYPTO */
8642 if( ssl->handshake->psk != NULL )
8643 {
8644 mbedtls_platform_zeroize( ssl->handshake->psk,
8645 ssl->handshake->psk_len );
8646 mbedtls_free( ssl->handshake->psk );
8647 ssl->handshake->psk_len = 0;
8648 }
8649}
8650
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008651int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8652 const unsigned char *psk, size_t psk_len )
8653{
8654 if( psk == NULL || ssl->handshake == NULL )
8655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8656
8657 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8659
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008660 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008661
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008662 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008663 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008664
8665 ssl->handshake->psk_len = psk_len;
8666 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8667
8668 return( 0 );
8669}
8670
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008671#if defined(MBEDTLS_USE_PSA_CRYPTO)
8672int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008673 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008674 const unsigned char *psk_identity,
8675 size_t psk_identity_len )
8676{
Hanno Becker7390c712018-11-15 13:33:04 +00008677 int ret;
8678 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008679 ssl_conf_remove_psk( conf );
8680
Hanno Becker7390c712018-11-15 13:33:04 +00008681 /* Check and set opaque PSK */
8682 if( psk_slot == 0 )
8683 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008684 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008685
8686 /* Check and set PSK Identity */
8687 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8688 psk_identity_len );
8689 if( ret != 0 )
8690 ssl_conf_remove_psk( conf );
8691
8692 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008693}
8694
8695int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008696 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008697{
8698 if( psk_slot == 0 || ssl->handshake == NULL )
8699 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8700
8701 ssl_remove_psk( ssl );
8702 ssl->handshake->psk_opaque = psk_slot;
8703 return( 0 );
8704}
8705#endif /* MBEDTLS_USE_PSA_CRYPTO */
8706
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008707void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008708 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008709 size_t),
8710 void *p_psk )
8711{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008712 conf->f_psk = f_psk;
8713 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008714}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008715#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008716
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008717#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008718
8719#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008720int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008721{
8722 int ret;
8723
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008724 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8725 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8726 {
8727 mbedtls_mpi_free( &conf->dhm_P );
8728 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008729 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008730 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008731
8732 return( 0 );
8733}
Hanno Becker470a8c42017-10-04 15:28:46 +01008734#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008735
Hanno Beckera90658f2017-10-04 15:29:08 +01008736int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8737 const unsigned char *dhm_P, size_t P_len,
8738 const unsigned char *dhm_G, size_t G_len )
8739{
8740 int ret;
8741
8742 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8743 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8744 {
8745 mbedtls_mpi_free( &conf->dhm_P );
8746 mbedtls_mpi_free( &conf->dhm_G );
8747 return( ret );
8748 }
8749
8750 return( 0 );
8751}
Paul Bakker5121ce52009-01-03 21:22:43 +00008752
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008753int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008754{
8755 int ret;
8756
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008757 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8758 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8759 {
8760 mbedtls_mpi_free( &conf->dhm_P );
8761 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008762 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008763 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008764
8765 return( 0 );
8766}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008767#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008768
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008769#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8770/*
8771 * Set the minimum length for Diffie-Hellman parameters
8772 */
8773void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8774 unsigned int bitlen )
8775{
8776 conf->dhm_min_bitlen = bitlen;
8777}
8778#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8779
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008780#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008781/*
8782 * Set allowed/preferred hashes for handshake signatures
8783 */
8784void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8785 const int *hashes )
8786{
8787 conf->sig_hashes = hashes;
8788}
Hanno Becker947194e2017-04-07 13:25:49 +01008789#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008790
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008791#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008792/*
8793 * Set the allowed elliptic curves
8794 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008795void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008796 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008797{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008798 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008799}
Hanno Becker947194e2017-04-07 13:25:49 +01008800#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008801
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008802#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008803int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008804{
Hanno Becker947194e2017-04-07 13:25:49 +01008805 /* Initialize to suppress unnecessary compiler warning */
8806 size_t hostname_len = 0;
8807
8808 /* Check if new hostname is valid before
8809 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008810 if( hostname != NULL )
8811 {
8812 hostname_len = strlen( hostname );
8813
8814 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8815 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8816 }
8817
8818 /* Now it's clear that we will overwrite the old hostname,
8819 * so we can free it safely */
8820
8821 if( ssl->hostname != NULL )
8822 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008823 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008824 mbedtls_free( ssl->hostname );
8825 }
8826
8827 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008828
Paul Bakker5121ce52009-01-03 21:22:43 +00008829 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008830 {
8831 ssl->hostname = NULL;
8832 }
8833 else
8834 {
8835 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008836 if( ssl->hostname == NULL )
8837 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008838
Hanno Becker947194e2017-04-07 13:25:49 +01008839 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008840
Hanno Becker947194e2017-04-07 13:25:49 +01008841 ssl->hostname[hostname_len] = '\0';
8842 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008843
8844 return( 0 );
8845}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008846#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008847
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008848#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008849void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008850 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008851 const unsigned char *, size_t),
8852 void *p_sni )
8853{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008854 conf->f_sni = f_sni;
8855 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008856}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008857#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008859#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008860int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008861{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008862 size_t cur_len, tot_len;
8863 const char **p;
8864
8865 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008866 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8867 * MUST NOT be truncated."
8868 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008869 */
8870 tot_len = 0;
8871 for( p = protos; *p != NULL; p++ )
8872 {
8873 cur_len = strlen( *p );
8874 tot_len += cur_len;
8875
8876 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008877 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008878 }
8879
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008880 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008881
8882 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008883}
8884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008885const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008886{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008887 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008888}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008889#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008890
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008891void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008892{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008893 conf->max_major_ver = major;
8894 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008895}
8896
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008897void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008898{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008899 conf->min_major_ver = major;
8900 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008901}
8902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008903#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008904void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008905{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008906 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008907}
8908#endif
8909
Janos Follath088ce432017-04-10 12:42:31 +01008910#if defined(MBEDTLS_SSL_SRV_C)
8911void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8912 char cert_req_ca_list )
8913{
8914 conf->cert_req_ca_list = cert_req_ca_list;
8915}
8916#endif
8917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008918#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008919void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008920{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008921 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008922}
8923#endif
8924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008925#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008926void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008927{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008928 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008929}
8930#endif
8931
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008932#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008933void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008934{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008935 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008936}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008937#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008939#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008940int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008941{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008942 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008943 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008945 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008946 }
8947
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008948 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008949
8950 return( 0 );
8951}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008952#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008954#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008955void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008956{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008957 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008958}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008959#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008961#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008962void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008963{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008964 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008965}
8966#endif
8967
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008968void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008969{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008970 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008971}
8972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008973#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008974void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008975{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008976 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008977}
8978
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008979void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008980{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008981 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008982}
8983
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008984void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008985 const unsigned char period[8] )
8986{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008987 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008988}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008989#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008991#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008992#if defined(MBEDTLS_SSL_CLI_C)
8993void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008994{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008995 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008996}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008997#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008998
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008999#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009000void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9001 mbedtls_ssl_ticket_write_t *f_ticket_write,
9002 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9003 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009004{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009005 conf->f_ticket_write = f_ticket_write;
9006 conf->f_ticket_parse = f_ticket_parse;
9007 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009008}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009009#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009010#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009011
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009012#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9013void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9014 mbedtls_ssl_export_keys_t *f_export_keys,
9015 void *p_export_keys )
9016{
9017 conf->f_export_keys = f_export_keys;
9018 conf->p_export_keys = p_export_keys;
9019}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009020
9021void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9022 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9023 void *p_export_keys )
9024{
9025 conf->f_export_keys_ext = f_export_keys_ext;
9026 conf->p_export_keys = p_export_keys;
9027}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009028#endif
9029
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009030#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009031void mbedtls_ssl_conf_async_private_cb(
9032 mbedtls_ssl_config *conf,
9033 mbedtls_ssl_async_sign_t *f_async_sign,
9034 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9035 mbedtls_ssl_async_resume_t *f_async_resume,
9036 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009037 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009038{
9039 conf->f_async_sign_start = f_async_sign;
9040 conf->f_async_decrypt_start = f_async_decrypt;
9041 conf->f_async_resume = f_async_resume;
9042 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009043 conf->p_async_config_data = async_config_data;
9044}
9045
Gilles Peskine8f97af72018-04-26 11:46:10 +02009046void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9047{
9048 return( conf->p_async_config_data );
9049}
9050
Gilles Peskine1febfef2018-04-30 11:54:39 +02009051void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009052{
9053 if( ssl->handshake == NULL )
9054 return( NULL );
9055 else
9056 return( ssl->handshake->user_async_ctx );
9057}
9058
Gilles Peskine1febfef2018-04-30 11:54:39 +02009059void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009060 void *ctx )
9061{
9062 if( ssl->handshake != NULL )
9063 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009064}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009065#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009066
Paul Bakker5121ce52009-01-03 21:22:43 +00009067/*
9068 * SSL get accessors
9069 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009070size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009071{
9072 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9073}
9074
Hanno Becker8b170a02017-10-10 11:51:19 +01009075int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9076{
9077 /*
9078 * Case A: We're currently holding back
9079 * a message for further processing.
9080 */
9081
9082 if( ssl->keep_current_message == 1 )
9083 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009084 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009085 return( 1 );
9086 }
9087
9088 /*
9089 * Case B: Further records are pending in the current datagram.
9090 */
9091
9092#if defined(MBEDTLS_SSL_PROTO_DTLS)
9093 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9094 ssl->in_left > ssl->next_record_offset )
9095 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009096 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009097 return( 1 );
9098 }
9099#endif /* MBEDTLS_SSL_PROTO_DTLS */
9100
9101 /*
9102 * Case C: A handshake message is being processed.
9103 */
9104
Hanno Becker8b170a02017-10-10 11:51:19 +01009105 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9106 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009107 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009108 return( 1 );
9109 }
9110
9111 /*
9112 * Case D: An application data message is being processed
9113 */
9114 if( ssl->in_offt != NULL )
9115 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009117 return( 1 );
9118 }
9119
9120 /*
9121 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009122 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009123 * we implement support for multiple alerts in single records.
9124 */
9125
9126 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9127 return( 0 );
9128}
9129
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009130uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009131{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009132 if( ssl->session != NULL )
9133 return( ssl->session->verify_result );
9134
9135 if( ssl->session_negotiate != NULL )
9136 return( ssl->session_negotiate->verify_result );
9137
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009138 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009139}
9140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009141const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009142{
Paul Bakker926c8e42013-03-06 10:23:34 +01009143 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009144 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009146 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009147}
9148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009149const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009150{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009151#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009152 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009153 {
9154 switch( ssl->minor_ver )
9155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009156 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009157 return( "DTLSv1.0" );
9158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009159 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009160 return( "DTLSv1.2" );
9161
9162 default:
9163 return( "unknown (DTLS)" );
9164 }
9165 }
9166#endif
9167
Paul Bakker43ca69c2011-01-15 17:35:19 +00009168 switch( ssl->minor_ver )
9169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009170 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009171 return( "SSLv3.0" );
9172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009173 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009174 return( "TLSv1.0" );
9175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009176 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009177 return( "TLSv1.1" );
9178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009179 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009180 return( "TLSv1.2" );
9181
Paul Bakker43ca69c2011-01-15 17:35:19 +00009182 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009183 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009184 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009185}
9186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009187int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009188{
Hanno Becker3136ede2018-08-17 15:28:19 +01009189 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009190 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009191 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009192
Hanno Becker78640902018-08-13 16:35:15 +01009193 if( transform == NULL )
9194 return( (int) mbedtls_ssl_hdr_len( ssl ) );
9195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009196#if defined(MBEDTLS_ZLIB_SUPPORT)
9197 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9198 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009199#endif
9200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009201 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009203 case MBEDTLS_MODE_GCM:
9204 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009205 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009206 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009207 transform_expansion = transform->minlen;
9208 break;
9209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009210 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009211
9212 block_size = mbedtls_cipher_get_block_size(
9213 &transform->cipher_ctx_enc );
9214
Hanno Becker3136ede2018-08-17 15:28:19 +01009215 /* Expansion due to the addition of the MAC. */
9216 transform_expansion += transform->maclen;
9217
9218 /* Expansion due to the addition of CBC padding;
9219 * Theoretically up to 256 bytes, but we never use
9220 * more than the block size of the underlying cipher. */
9221 transform_expansion += block_size;
9222
9223 /* For TLS 1.1 or higher, an explicit IV is added
9224 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009225#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9226 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009227 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009228#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009229
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009230 break;
9231
9232 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009234 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009235 }
9236
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02009237 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009238}
9239
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009240#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9241size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9242{
9243 size_t max_len;
9244
9245 /*
9246 * Assume mfl_code is correct since it was checked when set
9247 */
Angus Grattond8213d02016-05-25 20:56:48 +10009248 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009249
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009250 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009251 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009252 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009253 {
Angus Grattond8213d02016-05-25 20:56:48 +10009254 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009255 }
9256
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009257 /* During a handshake, use the value being negotiated */
9258 if( ssl->session_negotiate != NULL &&
9259 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9260 {
9261 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9262 }
9263
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009264 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009265}
9266#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9267
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009268#if defined(MBEDTLS_SSL_PROTO_DTLS)
9269static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9270{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009271 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9272 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9273 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9274 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9275 return ( 0 );
9276
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009277 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9278 return( ssl->mtu );
9279
9280 if( ssl->mtu == 0 )
9281 return( ssl->handshake->mtu );
9282
9283 return( ssl->mtu < ssl->handshake->mtu ?
9284 ssl->mtu : ssl->handshake->mtu );
9285}
9286#endif /* MBEDTLS_SSL_PROTO_DTLS */
9287
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009288int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9289{
9290 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9291
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009292#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9293 !defined(MBEDTLS_SSL_PROTO_DTLS)
9294 (void) ssl;
9295#endif
9296
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009297#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9298 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9299
9300 if( max_len > mfl )
9301 max_len = mfl;
9302#endif
9303
9304#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009305 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009306 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009307 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009308 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9309 const size_t overhead = (size_t) ret;
9310
9311 if( ret < 0 )
9312 return( ret );
9313
9314 if( mtu <= overhead )
9315 {
9316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9317 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9318 }
9319
9320 if( max_len > mtu - overhead )
9321 max_len = mtu - overhead;
9322 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009323#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009324
Hanno Becker0defedb2018-08-10 12:35:02 +01009325#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9326 !defined(MBEDTLS_SSL_PROTO_DTLS)
9327 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009328#endif
9329
9330 return( (int) max_len );
9331}
9332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009333#if defined(MBEDTLS_X509_CRT_PARSE_C)
9334const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009335{
9336 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009337 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009338
Hanno Beckere6824572019-02-07 13:18:46 +00009339#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009340 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009341#else
9342 return( NULL );
9343#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009344}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009345#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009347#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009348int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9349 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009350{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009351 if( ssl == NULL ||
9352 dst == NULL ||
9353 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009354 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009356 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009357 }
9358
Hanno Becker52055ae2019-02-06 14:30:46 +00009359 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009360}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009361#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009362
Paul Bakker5121ce52009-01-03 21:22:43 +00009363/*
Paul Bakker1961b702013-01-25 14:49:24 +01009364 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009366int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009367{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009368 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009369
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009370 if( ssl == NULL || ssl->conf == NULL )
9371 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009373#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009374 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009375 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009376#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009378 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009379 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009380#endif
9381
Paul Bakker1961b702013-01-25 14:49:24 +01009382 return( ret );
9383}
9384
9385/*
9386 * Perform the SSL handshake
9387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009388int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009389{
9390 int ret = 0;
9391
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009392 if( ssl == NULL || ssl->conf == NULL )
9393 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009397 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009399 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009400
9401 if( ret != 0 )
9402 break;
9403 }
9404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009406
9407 return( ret );
9408}
9409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009410#if defined(MBEDTLS_SSL_RENEGOTIATION)
9411#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009412/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009413 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009415static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009416{
9417 int ret;
9418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009420
9421 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009422 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9423 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009424
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009425 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009426 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009427 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009428 return( ret );
9429 }
9430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009432
9433 return( 0 );
9434}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009435#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009436
9437/*
9438 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009439 * - any side: calling mbedtls_ssl_renegotiate(),
9440 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9441 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009442 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009443 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009444 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009445 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009446static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009447{
9448 int ret;
9449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009451
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009452 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9453 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009454
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009455 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9456 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009457#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009458 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009459 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009460 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009461 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009462 ssl->handshake->out_msg_seq = 1;
9463 else
9464 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009465 }
9466#endif
9467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009468 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9469 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009471 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009473 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009474 return( ret );
9475 }
9476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009478
9479 return( 0 );
9480}
9481
9482/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009483 * Renegotiate current connection on client,
9484 * or request renegotiation on server
9485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009487{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009488 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009489
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009490 if( ssl == NULL || ssl->conf == NULL )
9491 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009493#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009494 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009495 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009497 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009500 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009501
9502 /* Did we already try/start sending HelloRequest? */
9503 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009504 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009505
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009506 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009507 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009508#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009510#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009511 /*
9512 * On client, either start the renegotiation process or,
9513 * if already in progress, continue the handshake
9514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009515 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9518 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009519
9520 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009523 return( ret );
9524 }
9525 }
9526 else
9527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009528 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009530 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009531 return( ret );
9532 }
9533 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009535
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009536 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009537}
9538
9539/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009540 * Check record counters and renegotiate if they're above the limit.
9541 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009542static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009543{
Andres AG2196c7f2016-12-15 17:01:16 +00009544 size_t ep_len = ssl_ep_len( ssl );
9545 int in_ctr_cmp;
9546 int out_ctr_cmp;
9547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009548 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9549 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009550 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009551 {
9552 return( 0 );
9553 }
9554
Andres AG2196c7f2016-12-15 17:01:16 +00009555 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9556 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009557 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009558 ssl->conf->renego_period + ep_len, 8 - ep_len );
9559
9560 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009561 {
9562 return( 0 );
9563 }
9564
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009567}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009568#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009569
9570/*
9571 * Receive application data decrypted from the SSL layer
9572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009573int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009574{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009575 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009576 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009577
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009578 if( ssl == NULL || ssl->conf == NULL )
9579 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009583#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009584 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009586 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009587 return( ret );
9588
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009589 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009590 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009591 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009592 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009593 return( ret );
9594 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009595 }
9596#endif
9597
Hanno Becker4a810fb2017-05-24 16:27:30 +01009598 /*
9599 * Check if renegotiation is necessary and/or handshake is
9600 * in process. If yes, perform/continue, and fall through
9601 * if an unexpected packet is received while the client
9602 * is waiting for the ServerHello.
9603 *
9604 * (There is no equivalent to the last condition on
9605 * the server-side as it is not treated as within
9606 * a handshake while waiting for the ClientHello
9607 * after a renegotiation request.)
9608 */
9609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009610#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009611 ret = ssl_check_ctr_renegotiate( ssl );
9612 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9613 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009615 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009616 return( ret );
9617 }
9618#endif
9619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009620 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009622 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009623 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9624 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009626 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009627 return( ret );
9628 }
9629 }
9630
Hanno Beckere41158b2017-10-23 13:30:32 +01009631 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009632 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009633 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009634 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009635 if( ssl->f_get_timer != NULL &&
9636 ssl->f_get_timer( ssl->p_timer ) == -1 )
9637 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009638 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009639 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009640
Hanno Becker327c93b2018-08-15 13:56:18 +01009641 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009642 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009643 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9644 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009645
Hanno Becker4a810fb2017-05-24 16:27:30 +01009646 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9647 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009648 }
9649
9650 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009651 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009652 {
9653 /*
9654 * OpenSSL sends empty messages to randomize the IV
9655 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009656 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009659 return( 0 );
9660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009661 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009662 return( ret );
9663 }
9664 }
9665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009666 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009669
Hanno Becker4a810fb2017-05-24 16:27:30 +01009670 /*
9671 * - For client-side, expect SERVER_HELLO_REQUEST.
9672 * - For server-side, expect CLIENT_HELLO.
9673 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9674 */
9675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009676#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009677 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009679 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009682
9683 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009684#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009685 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009686 {
9687 continue;
9688 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009689#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009690 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009691 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009692#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009693
Hanno Becker4a810fb2017-05-24 16:27:30 +01009694#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009695 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009696 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009699
9700 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009701#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009702 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009703 {
9704 continue;
9705 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009706#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009707 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009708 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009709#endif /* MBEDTLS_SSL_SRV_C */
9710
Hanno Becker21df7f92017-10-17 11:03:26 +01009711#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009712 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009713 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9714 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9715 ssl->conf->allow_legacy_renegotiation ==
9716 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9717 {
9718 /*
9719 * Accept renegotiation request
9720 */
Paul Bakker48916f92012-09-16 19:57:18 +00009721
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009722 /* DTLS clients need to know renego is server-initiated */
9723#if defined(MBEDTLS_SSL_PROTO_DTLS)
9724 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9725 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9726 {
9727 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9728 }
9729#endif
9730 ret = ssl_start_renegotiation( ssl );
9731 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9732 ret != 0 )
9733 {
9734 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9735 return( ret );
9736 }
9737 }
9738 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009739#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009740 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009741 /*
9742 * Refuse renegotiation
9743 */
9744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009745 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009747#if defined(MBEDTLS_SSL_PROTO_SSL3)
9748 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009749 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009750 /* SSLv3 does not have a "no_renegotiation" warning, so
9751 we send a fatal alert and abort the connection. */
9752 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9753 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9754 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009755 }
9756 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009757#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9758#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9759 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9760 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009762 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9763 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9764 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009765 {
9766 return( ret );
9767 }
Paul Bakker48916f92012-09-16 19:57:18 +00009768 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009769 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9771 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9774 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009775 }
Paul Bakker48916f92012-09-16 19:57:18 +00009776 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009777
Hanno Becker90333da2017-10-10 11:27:13 +01009778 /* At this point, we don't know whether the renegotiation has been
9779 * completed or not. The cases to consider are the following:
9780 * 1) The renegotiation is complete. In this case, no new record
9781 * has been read yet.
9782 * 2) The renegotiation is incomplete because the client received
9783 * an application data record while awaiting the ServerHello.
9784 * 3) The renegotiation is incomplete because the client received
9785 * a non-handshake, non-application data message while awaiting
9786 * the ServerHello.
9787 * In each of these case, looping will be the proper action:
9788 * - For 1), the next iteration will read a new record and check
9789 * if it's application data.
9790 * - For 2), the loop condition isn't satisfied as application data
9791 * is present, hence continue is the same as break
9792 * - For 3), the loop condition is satisfied and read_record
9793 * will re-deliver the message that was held back by the client
9794 * when expecting the ServerHello.
9795 */
9796 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009797 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009798#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009799 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009800 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009801 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009802 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009803 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009806 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009808 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009809 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009810 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009811#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009813 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9814 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009817 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009818 }
9819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9823 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009824 }
9825
9826 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009827
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009828 /* We're going to return something now, cancel timer,
9829 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009830 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009831 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009832
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009833#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009834 /* If we requested renego but received AppData, resend HelloRequest.
9835 * Do it now, after setting in_offt, to avoid taking this branch
9836 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009838 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009840 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009841 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009844 return( ret );
9845 }
9846 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009848#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009849 }
9850
9851 n = ( len < ssl->in_msglen )
9852 ? len : ssl->in_msglen;
9853
9854 memcpy( buf, ssl->in_offt, n );
9855 ssl->in_msglen -= n;
9856
9857 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009858 {
9859 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009860 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009861 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009862 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009863 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009864 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009865 /* more data available */
9866 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009867 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009870
Paul Bakker23986e52011-04-24 08:57:21 +00009871 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009872}
9873
9874/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009875 * Send application data to be encrypted by the SSL layer, taking care of max
9876 * fragment length and buffer size.
9877 *
9878 * According to RFC 5246 Section 6.2.1:
9879 *
9880 * Zero-length fragments of Application data MAY be sent as they are
9881 * potentially useful as a traffic analysis countermeasure.
9882 *
9883 * Therefore, it is possible that the input message length is 0 and the
9884 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009885 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009886static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009887 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009888{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009889 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9890 const size_t max_len = (size_t) ret;
9891
9892 if( ret < 0 )
9893 {
9894 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9895 return( ret );
9896 }
9897
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009898 if( len > max_len )
9899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009900#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009901 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009904 "maximum fragment length: %d > %d",
9905 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009906 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009907 }
9908 else
9909#endif
9910 len = max_len;
9911 }
Paul Bakker887bd502011-06-08 13:10:54 +00009912
Paul Bakker5121ce52009-01-03 21:22:43 +00009913 if( ssl->out_left != 0 )
9914 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009915 /*
9916 * The user has previously tried to send the data and
9917 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9918 * written. In this case, we expect the high-level write function
9919 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9920 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009921 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009924 return( ret );
9925 }
9926 }
Paul Bakker887bd502011-06-08 13:10:54 +00009927 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009928 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009929 /*
9930 * The user is trying to send a message the first time, so we need to
9931 * copy the data into the internal buffers and setup the data structure
9932 * to keep track of partial writes
9933 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009934 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009935 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009936 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009937
Hanno Becker67bc7c32018-08-06 11:33:50 +01009938 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009940 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009941 return( ret );
9942 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009943 }
9944
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009945 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009946}
9947
9948/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009949 * Write application data, doing 1/n-1 splitting if necessary.
9950 *
9951 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009952 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009953 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009954 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009955#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009956static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009957 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009958{
9959 int ret;
9960
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009961 if( ssl->conf->cbc_record_splitting ==
9962 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009963 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009964 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9965 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9966 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009967 {
9968 return( ssl_write_real( ssl, buf, len ) );
9969 }
9970
9971 if( ssl->split_done == 0 )
9972 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009973 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009974 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009975 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009976 }
9977
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009978 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9979 return( ret );
9980 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009981
9982 return( ret + 1 );
9983}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009984#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009985
9986/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009987 * Write application data (public-facing wrapper)
9988 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009989int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009990{
9991 int ret;
9992
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009994
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009995 if( ssl == NULL || ssl->conf == NULL )
9996 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9997
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009998#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009999 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10000 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010001 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010002 return( ret );
10003 }
10004#endif
10005
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010006 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010007 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010008 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010009 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010011 return( ret );
10012 }
10013 }
10014
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010015#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010016 ret = ssl_write_split( ssl, buf, len );
10017#else
10018 ret = ssl_write_real( ssl, buf, len );
10019#endif
10020
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010022
10023 return( ret );
10024}
10025
10026/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010027 * Notify the peer that the connection is being closed
10028 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010029int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010030{
10031 int ret;
10032
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010033 if( ssl == NULL || ssl->conf == NULL )
10034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010037
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010038 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010039 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010041 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010043 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10044 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10045 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010047 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010048 return( ret );
10049 }
10050 }
10051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010053
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010054 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010055}
10056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010057void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010058{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010059 if( transform == NULL )
10060 return;
10061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010062#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010063 deflateEnd( &transform->ctx_deflate );
10064 inflateEnd( &transform->ctx_inflate );
10065#endif
10066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010067 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10068 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010069
Hanno Beckerd56ed242018-01-03 15:32:51 +000010070#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010071 mbedtls_md_free( &transform->md_ctx_enc );
10072 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010073#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010074
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010075 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010076}
10077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010078#if defined(MBEDTLS_X509_CRT_PARSE_C)
10079static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010080{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010081 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010082
10083 while( cur != NULL )
10084 {
10085 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010086 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010087 cur = next;
10088 }
10089}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010090#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010091
Hanno Becker0271f962018-08-16 13:23:47 +010010092#if defined(MBEDTLS_SSL_PROTO_DTLS)
10093
10094static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10095{
10096 unsigned offset;
10097 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10098
10099 if( hs == NULL )
10100 return;
10101
Hanno Becker283f5ef2018-08-24 09:34:47 +010010102 ssl_free_buffered_record( ssl );
10103
Hanno Becker0271f962018-08-16 13:23:47 +010010104 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010105 ssl_buffering_free_slot( ssl, offset );
10106}
10107
10108static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10109 uint8_t slot )
10110{
10111 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10112 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010113
10114 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10115 return;
10116
Hanno Beckere605b192018-08-21 15:59:07 +010010117 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010118 {
Hanno Beckere605b192018-08-21 15:59:07 +010010119 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010120 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010121 mbedtls_free( hs_buf->data );
10122 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010123 }
10124}
10125
10126#endif /* MBEDTLS_SSL_PROTO_DTLS */
10127
Gilles Peskine9b562d52018-04-25 20:32:43 +020010128void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010129{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010130 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10131
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010132 if( handshake == NULL )
10133 return;
10134
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010135#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10136 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10137 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010138 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010139 handshake->async_in_progress = 0;
10140 }
10141#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10142
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010143#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10144 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10145 mbedtls_md5_free( &handshake->fin_md5 );
10146 mbedtls_sha1_free( &handshake->fin_sha1 );
10147#endif
10148#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10149#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010150#if defined(MBEDTLS_USE_PSA_CRYPTO)
10151 psa_hash_abort( &handshake->fin_sha256_psa );
10152#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010153 mbedtls_sha256_free( &handshake->fin_sha256 );
10154#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010155#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010156#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010157#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010158 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010159#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010160 mbedtls_sha512_free( &handshake->fin_sha512 );
10161#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010162#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010163#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010165#if defined(MBEDTLS_DHM_C)
10166 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010167#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010168#if defined(MBEDTLS_ECDH_C)
10169 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010170#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010171#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010172 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010173#if defined(MBEDTLS_SSL_CLI_C)
10174 mbedtls_free( handshake->ecjpake_cache );
10175 handshake->ecjpake_cache = NULL;
10176 handshake->ecjpake_cache_len = 0;
10177#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010178#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010179
Janos Follath4ae5c292016-02-10 11:27:43 +000010180#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10181 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010182 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010183 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010184#endif
10185
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010186#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10187 if( handshake->psk != NULL )
10188 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010189 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010190 mbedtls_free( handshake->psk );
10191 }
10192#endif
10193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010194#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10195 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010196 /*
10197 * Free only the linked list wrapper, not the keys themselves
10198 * since the belong to the SNI callback
10199 */
10200 if( handshake->sni_key_cert != NULL )
10201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010202 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010203
10204 while( cur != NULL )
10205 {
10206 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010207 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010208 cur = next;
10209 }
10210 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010211#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010212
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010213#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010214 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010215 if( handshake->ecrs_peer_cert != NULL )
10216 {
10217 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10218 mbedtls_free( handshake->ecrs_peer_cert );
10219 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010220#endif
10221
Hanno Becker75173122019-02-06 16:18:31 +000010222#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10223 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10224 mbedtls_pk_free( &handshake->peer_pubkey );
10225#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010227#if defined(MBEDTLS_SSL_PROTO_DTLS)
10228 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010229 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010230 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010231#endif
10232
Hanno Becker4a63ed42019-01-08 11:39:35 +000010233#if defined(MBEDTLS_ECDH_C) && \
10234 defined(MBEDTLS_USE_PSA_CRYPTO)
10235 psa_destroy_key( handshake->ecdh_psa_privkey );
10236#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10237
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010238 mbedtls_platform_zeroize( handshake,
10239 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010240}
10241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010242void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010243{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010244 if( session == NULL )
10245 return;
10246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010247#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010248 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010249#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010250
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010251#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010252 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010253#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010254
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010255 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010256}
10257
Paul Bakker5121ce52009-01-03 21:22:43 +000010258/*
10259 * Free an SSL context
10260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010261void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010262{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010263 if( ssl == NULL )
10264 return;
10265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010267
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010268 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010269 {
Angus Grattond8213d02016-05-25 20:56:48 +100010270 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010271 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010272 }
10273
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010274 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010275 {
Angus Grattond8213d02016-05-25 20:56:48 +100010276 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010277 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010278 }
10279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010280#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010281 if( ssl->compress_buf != NULL )
10282 {
Angus Grattond8213d02016-05-25 20:56:48 +100010283 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010284 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010285 }
10286#endif
10287
Paul Bakker48916f92012-09-16 19:57:18 +000010288 if( ssl->transform )
10289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010290 mbedtls_ssl_transform_free( ssl->transform );
10291 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010292 }
10293
10294 if( ssl->handshake )
10295 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010296 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010297 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10298 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010300 mbedtls_free( ssl->handshake );
10301 mbedtls_free( ssl->transform_negotiate );
10302 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010303 }
10304
Paul Bakkerc0463502013-02-14 11:19:38 +010010305 if( ssl->session )
10306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010307 mbedtls_ssl_session_free( ssl->session );
10308 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010309 }
10310
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010311#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010312 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010313 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010314 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010315 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010316 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010317#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010319#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10320 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10323 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010324 }
10325#endif
10326
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010327#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010328 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010329#endif
10330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010332
Paul Bakker86f04f42013-02-14 11:20:09 +010010333 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010334 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010335}
10336
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010337/*
10338 * Initialze mbedtls_ssl_config
10339 */
10340void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10341{
10342 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10343}
10344
Simon Butcherc97b6972015-12-27 23:48:17 +000010345#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010346static int ssl_preset_default_hashes[] = {
10347#if defined(MBEDTLS_SHA512_C)
10348 MBEDTLS_MD_SHA512,
10349 MBEDTLS_MD_SHA384,
10350#endif
10351#if defined(MBEDTLS_SHA256_C)
10352 MBEDTLS_MD_SHA256,
10353 MBEDTLS_MD_SHA224,
10354#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010355#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010356 MBEDTLS_MD_SHA1,
10357#endif
10358 MBEDTLS_MD_NONE
10359};
Simon Butcherc97b6972015-12-27 23:48:17 +000010360#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010361
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010362static int ssl_preset_suiteb_ciphersuites[] = {
10363 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10364 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10365 0
10366};
10367
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010368#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010369static int ssl_preset_suiteb_hashes[] = {
10370 MBEDTLS_MD_SHA256,
10371 MBEDTLS_MD_SHA384,
10372 MBEDTLS_MD_NONE
10373};
10374#endif
10375
10376#if defined(MBEDTLS_ECP_C)
10377static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10378 MBEDTLS_ECP_DP_SECP256R1,
10379 MBEDTLS_ECP_DP_SECP384R1,
10380 MBEDTLS_ECP_DP_NONE
10381};
10382#endif
10383
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010384/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010385 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010386 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010387int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010388 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010389{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010390#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010391 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010392#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010393
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010394 /* Use the functions here so that they are covered in tests,
10395 * but otherwise access member directly for efficiency */
10396 mbedtls_ssl_conf_endpoint( conf, endpoint );
10397 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010398
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010399 /*
10400 * Things that are common to all presets
10401 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010402#if defined(MBEDTLS_SSL_CLI_C)
10403 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10404 {
10405 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10406#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10407 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10408#endif
10409 }
10410#endif
10411
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010412#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010413 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010414#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010415
10416#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10417 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10418#endif
10419
10420#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10421 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10422#endif
10423
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010424#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10425 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10426#endif
10427
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010428#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010429 conf->f_cookie_write = ssl_cookie_write_dummy;
10430 conf->f_cookie_check = ssl_cookie_check_dummy;
10431#endif
10432
10433#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10434 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10435#endif
10436
Janos Follath088ce432017-04-10 12:42:31 +010010437#if defined(MBEDTLS_SSL_SRV_C)
10438 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10439#endif
10440
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010441#if defined(MBEDTLS_SSL_PROTO_DTLS)
10442 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10443 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10444#endif
10445
10446#if defined(MBEDTLS_SSL_RENEGOTIATION)
10447 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010448 memset( conf->renego_period, 0x00, 2 );
10449 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010450#endif
10451
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010452#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10453 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10454 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010455 const unsigned char dhm_p[] =
10456 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10457 const unsigned char dhm_g[] =
10458 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10459
Hanno Beckera90658f2017-10-04 15:29:08 +010010460 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10461 dhm_p, sizeof( dhm_p ),
10462 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010463 {
10464 return( ret );
10465 }
10466 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010467#endif
10468
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010469 /*
10470 * Preset-specific defaults
10471 */
10472 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010473 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010474 /*
10475 * NSA Suite B
10476 */
10477 case MBEDTLS_SSL_PRESET_SUITEB:
10478 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10479 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10480 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10481 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10482
10483 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10484 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10485 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10486 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10487 ssl_preset_suiteb_ciphersuites;
10488
10489#if defined(MBEDTLS_X509_CRT_PARSE_C)
10490 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010491#endif
10492
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010493#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010494 conf->sig_hashes = ssl_preset_suiteb_hashes;
10495#endif
10496
10497#if defined(MBEDTLS_ECP_C)
10498 conf->curve_list = ssl_preset_suiteb_curves;
10499#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010500 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010501
10502 /*
10503 * Default
10504 */
10505 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010506 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10507 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10508 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10509 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10510 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10511 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10512 MBEDTLS_SSL_MIN_MINOR_VERSION :
10513 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010514 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10515 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10516
10517#if defined(MBEDTLS_SSL_PROTO_DTLS)
10518 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10519 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10520#endif
10521
10522 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10523 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10524 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10525 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10526 mbedtls_ssl_list_ciphersuites();
10527
10528#if defined(MBEDTLS_X509_CRT_PARSE_C)
10529 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10530#endif
10531
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010532#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010533 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010534#endif
10535
10536#if defined(MBEDTLS_ECP_C)
10537 conf->curve_list = mbedtls_ecp_grp_id_list();
10538#endif
10539
10540#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10541 conf->dhm_min_bitlen = 1024;
10542#endif
10543 }
10544
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010545 return( 0 );
10546}
10547
10548/*
10549 * Free mbedtls_ssl_config
10550 */
10551void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10552{
10553#if defined(MBEDTLS_DHM_C)
10554 mbedtls_mpi_free( &conf->dhm_P );
10555 mbedtls_mpi_free( &conf->dhm_G );
10556#endif
10557
10558#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10559 if( conf->psk != NULL )
10560 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010561 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010562 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010563 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010564 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010565 }
10566
10567 if( conf->psk_identity != NULL )
10568 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010569 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010570 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010571 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010572 conf->psk_identity_len = 0;
10573 }
10574#endif
10575
10576#if defined(MBEDTLS_X509_CRT_PARSE_C)
10577 ssl_key_cert_free( conf->key_cert );
10578#endif
10579
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010580 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010581}
10582
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010583#if defined(MBEDTLS_PK_C) && \
10584 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010585/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010586 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010588unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010589{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010590#if defined(MBEDTLS_RSA_C)
10591 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10592 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010593#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010594#if defined(MBEDTLS_ECDSA_C)
10595 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10596 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010597#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010598 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010599}
10600
Hanno Becker7e5437a2017-04-28 17:15:26 +010010601unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10602{
10603 switch( type ) {
10604 case MBEDTLS_PK_RSA:
10605 return( MBEDTLS_SSL_SIG_RSA );
10606 case MBEDTLS_PK_ECDSA:
10607 case MBEDTLS_PK_ECKEY:
10608 return( MBEDTLS_SSL_SIG_ECDSA );
10609 default:
10610 return( MBEDTLS_SSL_SIG_ANON );
10611 }
10612}
10613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010614mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010615{
10616 switch( sig )
10617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010618#if defined(MBEDTLS_RSA_C)
10619 case MBEDTLS_SSL_SIG_RSA:
10620 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010621#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010622#if defined(MBEDTLS_ECDSA_C)
10623 case MBEDTLS_SSL_SIG_ECDSA:
10624 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010625#endif
10626 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010627 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010628 }
10629}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010630#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010631
Hanno Becker7e5437a2017-04-28 17:15:26 +010010632#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10633 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10634
10635/* Find an entry in a signature-hash set matching a given hash algorithm. */
10636mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10637 mbedtls_pk_type_t sig_alg )
10638{
10639 switch( sig_alg )
10640 {
10641 case MBEDTLS_PK_RSA:
10642 return( set->rsa );
10643 case MBEDTLS_PK_ECDSA:
10644 return( set->ecdsa );
10645 default:
10646 return( MBEDTLS_MD_NONE );
10647 }
10648}
10649
10650/* Add a signature-hash-pair to a signature-hash set */
10651void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10652 mbedtls_pk_type_t sig_alg,
10653 mbedtls_md_type_t md_alg )
10654{
10655 switch( sig_alg )
10656 {
10657 case MBEDTLS_PK_RSA:
10658 if( set->rsa == MBEDTLS_MD_NONE )
10659 set->rsa = md_alg;
10660 break;
10661
10662 case MBEDTLS_PK_ECDSA:
10663 if( set->ecdsa == MBEDTLS_MD_NONE )
10664 set->ecdsa = md_alg;
10665 break;
10666
10667 default:
10668 break;
10669 }
10670}
10671
10672/* Allow exactly one hash algorithm for each signature. */
10673void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10674 mbedtls_md_type_t md_alg )
10675{
10676 set->rsa = md_alg;
10677 set->ecdsa = md_alg;
10678}
10679
10680#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10681 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10682
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010683/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010684 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010686mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010687{
10688 switch( hash )
10689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010690#if defined(MBEDTLS_MD5_C)
10691 case MBEDTLS_SSL_HASH_MD5:
10692 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010693#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010694#if defined(MBEDTLS_SHA1_C)
10695 case MBEDTLS_SSL_HASH_SHA1:
10696 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010697#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010698#if defined(MBEDTLS_SHA256_C)
10699 case MBEDTLS_SSL_HASH_SHA224:
10700 return( MBEDTLS_MD_SHA224 );
10701 case MBEDTLS_SSL_HASH_SHA256:
10702 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010703#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010704#if defined(MBEDTLS_SHA512_C)
10705 case MBEDTLS_SSL_HASH_SHA384:
10706 return( MBEDTLS_MD_SHA384 );
10707 case MBEDTLS_SSL_HASH_SHA512:
10708 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010709#endif
10710 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010711 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010712 }
10713}
10714
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010715/*
10716 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10717 */
10718unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10719{
10720 switch( md )
10721 {
10722#if defined(MBEDTLS_MD5_C)
10723 case MBEDTLS_MD_MD5:
10724 return( MBEDTLS_SSL_HASH_MD5 );
10725#endif
10726#if defined(MBEDTLS_SHA1_C)
10727 case MBEDTLS_MD_SHA1:
10728 return( MBEDTLS_SSL_HASH_SHA1 );
10729#endif
10730#if defined(MBEDTLS_SHA256_C)
10731 case MBEDTLS_MD_SHA224:
10732 return( MBEDTLS_SSL_HASH_SHA224 );
10733 case MBEDTLS_MD_SHA256:
10734 return( MBEDTLS_SSL_HASH_SHA256 );
10735#endif
10736#if defined(MBEDTLS_SHA512_C)
10737 case MBEDTLS_MD_SHA384:
10738 return( MBEDTLS_SSL_HASH_SHA384 );
10739 case MBEDTLS_MD_SHA512:
10740 return( MBEDTLS_SSL_HASH_SHA512 );
10741#endif
10742 default:
10743 return( MBEDTLS_SSL_HASH_NONE );
10744 }
10745}
10746
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010747#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010748/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010749 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010750 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010751 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010752int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010753{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010754 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010755
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010756 if( ssl->conf->curve_list == NULL )
10757 return( -1 );
10758
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010759 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010760 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010761 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010762
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010763 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010764}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010765#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010766
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010767#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010768/*
10769 * Check if a hash proposed by the peer is in our list.
10770 * Return 0 if we're willing to use it, -1 otherwise.
10771 */
10772int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10773 mbedtls_md_type_t md )
10774{
10775 const int *cur;
10776
10777 if( ssl->conf->sig_hashes == NULL )
10778 return( -1 );
10779
10780 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10781 if( *cur == (int) md )
10782 return( 0 );
10783
10784 return( -1 );
10785}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010786#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010788#if defined(MBEDTLS_X509_CRT_PARSE_C)
10789int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10790 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010791 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010792 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010793{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010794 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010795#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010796 int usage = 0;
10797#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010798#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010799 const char *ext_oid;
10800 size_t ext_len;
10801#endif
10802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010803#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10804 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010805 ((void) cert);
10806 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010807 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010808#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010810#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10811 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010812 {
10813 /* Server part of the key exchange */
10814 switch( ciphersuite->key_exchange )
10815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010816 case MBEDTLS_KEY_EXCHANGE_RSA:
10817 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010818 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010819 break;
10820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010821 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10822 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10823 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10824 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010825 break;
10826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010827 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10828 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010829 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010830 break;
10831
10832 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010833 case MBEDTLS_KEY_EXCHANGE_NONE:
10834 case MBEDTLS_KEY_EXCHANGE_PSK:
10835 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10836 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010837 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010838 usage = 0;
10839 }
10840 }
10841 else
10842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010843 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10844 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010845 }
10846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010847 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010848 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010849 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010850 ret = -1;
10851 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010852#else
10853 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010854#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010856#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10857 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010859 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10860 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010861 }
10862 else
10863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010864 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10865 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010866 }
10867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010868 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010869 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010870 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010871 ret = -1;
10872 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010873#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010874
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010875 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010876}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010877#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010878
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010879/*
10880 * Convert version numbers to/from wire format
10881 * and, for DTLS, to/from TLS equivalent.
10882 *
10883 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010884 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010885 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10886 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10887 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010888void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010889 unsigned char ver[2] )
10890{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010891#if defined(MBEDTLS_SSL_PROTO_DTLS)
10892 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010894 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010895 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10896
10897 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10898 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10899 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010900 else
10901#else
10902 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010903#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010904 {
10905 ver[0] = (unsigned char) major;
10906 ver[1] = (unsigned char) minor;
10907 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010908}
10909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010910void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010911 const unsigned char ver[2] )
10912{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010913#if defined(MBEDTLS_SSL_PROTO_DTLS)
10914 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010915 {
10916 *major = 255 - ver[0] + 2;
10917 *minor = 255 - ver[1] + 1;
10918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010919 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010920 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10921 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010922 else
10923#else
10924 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010925#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010926 {
10927 *major = ver[0];
10928 *minor = ver[1];
10929 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010930}
10931
Simon Butcher99000142016-10-13 17:21:01 +010010932int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10933{
10934#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10935 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10936 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10937
10938 switch( md )
10939 {
10940#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10941#if defined(MBEDTLS_MD5_C)
10942 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010943 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010944#endif
10945#if defined(MBEDTLS_SHA1_C)
10946 case MBEDTLS_SSL_HASH_SHA1:
10947 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10948 break;
10949#endif
10950#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10951#if defined(MBEDTLS_SHA512_C)
10952 case MBEDTLS_SSL_HASH_SHA384:
10953 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10954 break;
10955#endif
10956#if defined(MBEDTLS_SHA256_C)
10957 case MBEDTLS_SSL_HASH_SHA256:
10958 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10959 break;
10960#endif
10961 default:
10962 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10963 }
10964
10965 return 0;
10966#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10967 (void) ssl;
10968 (void) md;
10969
10970 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10971#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10972}
10973
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010974#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10975 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10976int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10977 unsigned char *output,
10978 unsigned char *data, size_t data_len )
10979{
10980 int ret = 0;
10981 mbedtls_md5_context mbedtls_md5;
10982 mbedtls_sha1_context mbedtls_sha1;
10983
10984 mbedtls_md5_init( &mbedtls_md5 );
10985 mbedtls_sha1_init( &mbedtls_sha1 );
10986
10987 /*
10988 * digitally-signed struct {
10989 * opaque md5_hash[16];
10990 * opaque sha_hash[20];
10991 * };
10992 *
10993 * md5_hash
10994 * MD5(ClientHello.random + ServerHello.random
10995 * + ServerParams);
10996 * sha_hash
10997 * SHA(ClientHello.random + ServerHello.random
10998 * + ServerParams);
10999 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011000 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011001 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011003 goto exit;
11004 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011005 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011006 ssl->handshake->randbytes, 64 ) ) != 0 )
11007 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011008 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011009 goto exit;
11010 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011011 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011012 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011013 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011014 goto exit;
11015 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011016 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011017 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011019 goto exit;
11020 }
11021
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011022 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011023 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011024 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011025 goto exit;
11026 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011027 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011028 ssl->handshake->randbytes, 64 ) ) != 0 )
11029 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011031 goto exit;
11032 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011033 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011034 data_len ) ) != 0 )
11035 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011037 goto exit;
11038 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011039 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011040 output + 16 ) ) != 0 )
11041 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011042 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011043 goto exit;
11044 }
11045
11046exit:
11047 mbedtls_md5_free( &mbedtls_md5 );
11048 mbedtls_sha1_free( &mbedtls_sha1 );
11049
11050 if( ret != 0 )
11051 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11052 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11053
11054 return( ret );
11055
11056}
11057#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11058 MBEDTLS_SSL_PROTO_TLS1_1 */
11059
11060#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11061 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011062
11063#if defined(MBEDTLS_USE_PSA_CRYPTO)
11064int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11065 unsigned char *hash, size_t *hashlen,
11066 unsigned char *data, size_t data_len,
11067 mbedtls_md_type_t md_alg )
11068{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011069 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011070 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011071 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11072
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011073 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011074
11075 if( ( status = psa_hash_setup( &hash_operation,
11076 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011077 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011078 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011079 goto exit;
11080 }
11081
Andrzej Kurek814feff2019-01-14 04:35:19 -050011082 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11083 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011084 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011085 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011086 goto exit;
11087 }
11088
Andrzej Kurek814feff2019-01-14 04:35:19 -050011089 if( ( status = psa_hash_update( &hash_operation,
11090 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011091 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011092 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011093 goto exit;
11094 }
11095
Andrzej Kurek814feff2019-01-14 04:35:19 -050011096 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11097 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011098 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011099 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011100 goto exit;
11101 }
11102
11103exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011104 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011105 {
11106 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11107 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011108 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011109 {
11110 case PSA_ERROR_NOT_SUPPORTED:
11111 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011112 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011113 case PSA_ERROR_BUFFER_TOO_SMALL:
11114 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11115 case PSA_ERROR_INSUFFICIENT_MEMORY:
11116 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11117 default:
11118 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11119 }
11120 }
11121 return( 0 );
11122}
11123
11124#else
11125
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011126int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011127 unsigned char *hash, size_t *hashlen,
11128 unsigned char *data, size_t data_len,
11129 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011130{
11131 int ret = 0;
11132 mbedtls_md_context_t ctx;
11133 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011134 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011135
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011136 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011137
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011138 mbedtls_md_init( &ctx );
11139
11140 /*
11141 * digitally-signed struct {
11142 * opaque client_random[32];
11143 * opaque server_random[32];
11144 * ServerDHParams params;
11145 * };
11146 */
11147 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11148 {
11149 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11150 goto exit;
11151 }
11152 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11153 {
11154 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11155 goto exit;
11156 }
11157 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11158 {
11159 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11160 goto exit;
11161 }
11162 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11163 {
11164 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11165 goto exit;
11166 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011167 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011168 {
11169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11170 goto exit;
11171 }
11172
11173exit:
11174 mbedtls_md_free( &ctx );
11175
11176 if( ret != 0 )
11177 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11178 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11179
11180 return( ret );
11181}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011182#endif /* MBEDTLS_USE_PSA_CRYPTO */
11183
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011184#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11185 MBEDTLS_SSL_PROTO_TLS1_2 */
11186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011187#endif /* MBEDTLS_SSL_TLS_C */