blob: 84afaedc9adb33f9f7a2784843e6ec7ca7b056ba [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)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01001971/* This functions transforms a DTLS plaintext fragment and a record content
1972 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001973 *
1974 * struct {
1975 * opaque content[DTLSPlaintext.length];
1976 * ContentType real_type;
1977 * uint8 zeros[length_of_padding];
1978 * } DTLSInnerPlaintext;
1979 *
1980 * Input:
1981 * - `content`: The beginning of the buffer holding the
1982 * plaintext to be wrapped.
1983 * - `*content_size`: The length of the plaintext in Bytes.
1984 * - `max_len`: The number of Bytes available starting from
1985 * `content`. This must be `>= *content_size`.
1986 * - `rec_type`: The desired record content type.
1987 *
1988 * Output:
1989 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1990 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1991 *
1992 * Returns:
1993 * - `0` on success.
1994 * - A negative error code if `max_len` didn't offer enough space
1995 * for the expansion.
1996 */
1997static int ssl_cid_build_inner_plaintext( unsigned char *content,
1998 size_t *content_size,
1999 size_t remaining,
2000 uint8_t rec_type )
2001{
2002 size_t len = *content_size;
2003 size_t pad = ~len & 0xF; /* Pad to a multiple of 16 */
2004
2005 /* Write real content type */
2006 if( remaining == 0 )
2007 return( -1 );
2008 content[ len ] = rec_type;
2009 len++;
2010 remaining--;
2011
2012 if( remaining < pad )
2013 return( -1 );
2014 memset( content + len, 0, pad );
2015 len += pad;
2016 remaining -= pad;
2017
2018 *content_size = len;
2019 return( 0 );
2020}
2021
Hanno Becker07dc97d2019-05-20 15:08:01 +01002022/* This function parses a DTLSInnerPlaintext structure.
2023 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002024static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2025 size_t *content_size,
2026 uint8_t *rec_type )
2027{
2028 size_t remaining = *content_size;
2029
2030 /* Determine length of padding by skipping zeroes from the back. */
2031 do
2032 {
2033 if( remaining == 0 )
2034 return( -1 );
2035 remaining--;
2036 } while( content[ remaining ] == 0 );
2037
2038 *content_size = remaining;
2039 *rec_type = content[ remaining ];
2040
2041 return( 0 );
2042}
2043#endif /* MBEDTLS_SSL_CID */
2044
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002045/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002046 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002047static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002048 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002049 mbedtls_record *rec )
2050{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002051 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002052 *
2053 * additional_data = seq_num + TLSCompressed.type +
2054 * TLSCompressed.version + TLSCompressed.length;
2055 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002056 * For the CID extension, this is extended as follows
2057 * (quoting draft-ietf-tls-dtls-connection-id-05,
2058 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002059 *
2060 * additional_data = seq_num + DTLSPlaintext.type +
2061 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002062 * cid +
2063 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002064 * length_of_DTLSInnerPlaintext;
2065 */
2066
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002067 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2068 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002069 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002070
2071#if defined(MBEDTLS_SSL_CID)
2072 memcpy( add_data + 11, rec->cid, rec->cid_len );
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002073 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2074 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2075 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2076 *add_data_len = 13 + 1 + rec->cid_len;
Hanno Becker43c24b82019-05-01 09:45:57 +01002077#else /* MBEDTLS_SSL_CID */
2078 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2079 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
Hanno Beckercab87e62019-04-29 13:52:53 +01002080 *add_data_len = 13;
2081#endif /* MBEDTLS_SSL_CID */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002082}
2083
Hanno Beckera18d1322018-01-03 14:27:32 +00002084int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2085 mbedtls_ssl_transform *transform,
2086 mbedtls_record *rec,
2087 int (*f_rng)(void *, unsigned char *, size_t),
2088 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002089{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002091 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002092 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002093 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002094 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002095 size_t post_avail;
2096
2097 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002098#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002099 ((void) ssl);
2100#endif
2101
2102 /* The PRNG is used for dynamic IV generation that's used
2103 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2104#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2105 ( defined(MBEDTLS_AES_C) || \
2106 defined(MBEDTLS_ARIA_C) || \
2107 defined(MBEDTLS_CAMELLIA_C) ) && \
2108 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2109 ((void) f_rng);
2110 ((void) p_rng);
2111#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002114
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002115 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002116 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2118 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2119 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002120 if( rec == NULL
2121 || rec->buf == NULL
2122 || rec->buf_len < rec->data_offset
2123 || rec->buf_len - rec->data_offset < rec->data_len
2124#if defined(MBEDTLS_SSL_CID)
2125 || rec->cid_len != 0
2126#endif
2127 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002128 {
2129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002131 }
2132
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002133 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002134 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002136 data, rec->data_len );
2137
2138 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2139
2140 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2141 {
2142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2143 (unsigned) rec->data_len,
2144 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2145 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2146 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002147
Hanno Beckercab87e62019-04-29 13:52:53 +01002148#if defined(MBEDTLS_SSL_CID)
2149 /*
2150 * Add CID information
2151 */
2152 rec->cid_len = transform->out_cid_len;
2153 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2154 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002155
2156 if( rec->cid_len != 0 )
2157 {
2158 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002159 * Wrap plaintext into DTLSInnerPlaintext structure.
2160 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002161 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002162 * Note that this changes `rec->data_len`, and hence
2163 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002164 */
2165 if( ssl_cid_build_inner_plaintext( data,
2166 &rec->data_len,
2167 post_avail,
2168 rec->type ) != 0 )
2169 {
2170 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2171 }
2172
2173 rec->type = MBEDTLS_SSL_MSG_CID;
2174 }
Hanno Beckercab87e62019-04-29 13:52:53 +01002175#endif /* MBEDTLS_SSL_CID */
2176
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002177 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2178
Paul Bakker5121ce52009-01-03 21:22:43 +00002179 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002180 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 */
Hanno Becker52344c22018-01-03 15:24:20 +00002182#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 if( mode == MBEDTLS_MODE_STREAM ||
2184 ( mode == MBEDTLS_MODE_CBC
2185#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002186 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002187#endif
2188 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002189 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002190 if( post_avail < transform->maclen )
2191 {
2192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2193 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2194 }
2195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002197 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002198 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002199 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002200 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2201 data, rec->data_len, rec->ctr, rec->type, mac );
2202 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002203 }
2204 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002205#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2207 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002208 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002209 {
Hanno Becker992b6872017-11-09 18:57:39 +00002210 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2211
Hanno Beckercab87e62019-04-29 13:52:53 +01002212 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002213
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002214 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002215 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002216 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2217 data, rec->data_len );
2218 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2219 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2220
2221 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002222 }
2223 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002224#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2227 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002228 }
2229
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002230 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2231 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002232
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002233 rec->data_len += transform->maclen;
2234 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002235 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002236 }
Hanno Becker52344c22018-01-03 15:24:20 +00002237#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002238
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002239 /*
2240 * Encrypt
2241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2243 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002245 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002246 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002248 "including %d bytes of padding",
2249 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002250
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002251 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2252 transform->iv_enc, transform->ivlen,
2253 data, rec->data_len,
2254 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002257 return( ret );
2258 }
2259
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002260 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2263 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002264 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 }
Paul Bakker68884e32013-01-07 18:20:04 +01002266 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002268
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002269#if defined(MBEDTLS_GCM_C) || \
2270 defined(MBEDTLS_CCM_C) || \
2271 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002273 mode == MBEDTLS_MODE_CCM ||
2274 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002275 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002276 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002277 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002278 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002279
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002280 /* Check that there's space for both the authentication tag
2281 * and the explicit IV before and after the record content. */
2282 if( post_avail < transform->taglen ||
2283 rec->data_offset < explicit_iv_len )
2284 {
2285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2286 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2287 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002288
Paul Bakker68884e32013-01-07 18:20:04 +01002289 /*
2290 * Generate IV
2291 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002292 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2293 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002294 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002295 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002296 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2297 explicit_iv_len );
2298 /* Prefix record content with explicit IV. */
2299 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002300 }
2301 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2302 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002303 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002304 unsigned char i;
2305
2306 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2307
2308 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002309 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002310 }
2311 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002312 {
2313 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2315 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002316 }
2317
Hanno Beckercab87e62019-04-29 13:52:53 +01002318 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002319
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002320 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2321 iv, transform->ivlen );
2322 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002323 data - explicit_iv_len, explicit_iv_len );
2324 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002325 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002327 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002328 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002329
Paul Bakker68884e32013-01-07 18:20:04 +01002330 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002331 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002332 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002333
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002334 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002335 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002336 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002337 data, rec->data_len, /* source */
2338 data, &rec->data_len, /* destination */
2339 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002342 return( ret );
2343 }
2344
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002345 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2346 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002347
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002348 rec->data_len += transform->taglen + explicit_iv_len;
2349 rec->data_offset -= explicit_iv_len;
2350 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002351 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002352 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002353 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2355#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002356 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002358 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002359 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002360 size_t padlen, i;
2361 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002362
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002363 /* Currently we're always using minimal padding
2364 * (up to 255 bytes would be allowed). */
2365 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2366 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002367 padlen = 0;
2368
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002369 /* Check there's enough space in the buffer for the padding. */
2370 if( post_avail < padlen + 1 )
2371 {
2372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2373 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2374 }
2375
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002377 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002378
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002379 rec->data_len += padlen + 1;
2380 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002383 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002384 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2385 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002386 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002387 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002388 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002389 if( f_rng == NULL )
2390 {
2391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2392 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2393 }
2394
2395 if( rec->data_offset < transform->ivlen )
2396 {
2397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2398 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2399 }
2400
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002401 /*
2402 * Generate IV
2403 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002404 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002405 if( ret != 0 )
2406 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002407
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002408 memcpy( data - transform->ivlen, transform->iv_enc,
2409 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002410
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002411 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002415 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002416 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002417 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002418
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002419 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2420 transform->iv_enc,
2421 transform->ivlen,
2422 data, rec->data_len,
2423 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002426 return( ret );
2427 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002428
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002429 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2432 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002433 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002436 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002437 {
2438 /*
2439 * Save IV in SSL3 and TLS1
2440 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002441 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2442 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002443 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002444 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002445#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002446 {
2447 data -= transform->ivlen;
2448 rec->data_offset -= transform->ivlen;
2449 rec->data_len += transform->ivlen;
2450 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002453 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002454 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002455 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2456
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002457 /*
2458 * MAC(MAC_write_key, seq_num +
2459 * TLSCipherText.type +
2460 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002461 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002462 * IV + // except for TLS 1.0
2463 * ENC(content + padding + padding_length));
2464 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002465
2466 if( post_avail < transform->maclen)
2467 {
2468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2469 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2470 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002471
Hanno Beckercab87e62019-04-29 13:52:53 +01002472 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002475 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002476 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002477
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002478 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002479 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002480 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2481 data, rec->data_len );
2482 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2483 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002484
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002485 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002486
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002487 rec->data_len += transform->maclen;
2488 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002489 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002490 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002493 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002495 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2498 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002499 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002501 /* Make extra sure authentication was performed, exactly once */
2502 if( auth_done != 1 )
2503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2505 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002506 }
2507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
2510 return( 0 );
2511}
2512
Hanno Beckera18d1322018-01-03 14:27:32 +00002513int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2514 mbedtls_ssl_transform *transform,
2515 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002516{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002517 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002519 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002520#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002521 size_t padlen = 0, correct = 1;
2522#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002523 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002524 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002525 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002526
Hanno Beckera18d1322018-01-03 14:27:32 +00002527#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002528 ((void) ssl);
2529#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002532 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002533 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2535 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2536 }
2537 if( rec == NULL ||
2538 rec->buf == NULL ||
2539 rec->buf_len < rec->data_offset ||
2540 rec->buf_len - rec->data_offset < rec->data_len )
2541 {
2542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002544 }
2545
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002546 data = rec->buf + rec->data_offset;
2547 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002548
Hanno Beckercab87e62019-04-29 13:52:53 +01002549#if defined(MBEDTLS_SSL_CID)
2550 /*
2551 * Match record's CID with incoming CID.
2552 */
2553
Hanno Beckerf44e55d2019-04-30 16:56:40 +01002554 /* Uncomment this once CID parsing is in place */
Hanno Beckercab87e62019-04-29 13:52:53 +01002555 /* if( rec->cid_len != transform->in_cid_len || */
2556 /* memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) */
2557 /* { */
2558 /* return( MBEDTLS_ERR_SSL_INVALID_RECORD ); */
2559 /* } */
Hanno Beckerf44e55d2019-04-30 16:56:40 +01002560
2561 /* Remove this once CID parsing is in place */
Hanno Beckercab87e62019-04-29 13:52:53 +01002562 rec->cid_len = transform->in_cid_len;
2563 memcpy( rec->cid, transform->in_cid, transform->in_cid_len );
2564 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
2565#endif /* MBEDTLS_SSL_CID */
2566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2568 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002569 {
2570 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002571 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2572 transform->iv_dec,
2573 transform->ivlen,
2574 data, rec->data_len,
2575 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002578 return( ret );
2579 }
2580
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002581 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2584 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002585 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 }
Paul Bakker68884e32013-01-07 18:20:04 +01002587 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002589#if defined(MBEDTLS_GCM_C) || \
2590 defined(MBEDTLS_CCM_C) || \
2591 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002593 mode == MBEDTLS_MODE_CCM ||
2594 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002595 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002596 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002597 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002598
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002599 /*
2600 * Compute and update sizes
2601 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002602 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002605 "+ taglen (%d)", rec->data_len,
2606 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002608 }
Paul Bakker68884e32013-01-07 18:20:04 +01002609
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002610 /*
2611 * Prepare IV
2612 */
2613 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2614 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002615 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002616 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002617 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002618
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002619 }
2620 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2621 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002622 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002623 unsigned char i;
2624
2625 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2626
2627 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002628 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002629 }
2630 else
2631 {
2632 /* Reminder if we ever add an AEAD mode with a different size */
2633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2634 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2635 }
2636
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002637 data += explicit_iv_len;
2638 rec->data_offset += explicit_iv_len;
2639 rec->data_len -= explicit_iv_len + transform->taglen;
2640
Hanno Beckercab87e62019-04-29 13:52:53 +01002641 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002642 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002643 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002644
2645 memcpy( transform->iv_dec + transform->fixed_ivlen,
2646 data - explicit_iv_len, explicit_iv_len );
2647
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002648 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002649 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002650 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002651
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002652
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002653 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002654 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002655 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002656 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2657 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002658 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002659 data, rec->data_len,
2660 data, &olen,
2661 data + rec->data_len,
2662 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2667 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002668
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002669 return( ret );
2670 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002671 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002672
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002673 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2676 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002677 }
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002678
Paul Bakkerca4ab492012-04-18 14:23:57 +00002679 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002680 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2682#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002683 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002685 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002686 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002687
Paul Bakker5121ce52009-01-03 21:22:43 +00002688 /*
Paul Bakker45829992013-01-03 14:52:21 +01002689 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002692 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2693 {
2694 /* The ciphertext is prefixed with the CBC IV. */
2695 minlen += transform->ivlen;
2696 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002697#endif
Paul Bakker45829992013-01-03 14:52:21 +01002698
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002699 /* Size considerations:
2700 *
2701 * - The CBC cipher text must not be empty and hence
2702 * at least of size transform->ivlen.
2703 *
2704 * Together with the potential IV-prefix, this explains
2705 * the first of the two checks below.
2706 *
2707 * - The record must contain a MAC, either in plain or
2708 * encrypted, depending on whether Encrypt-then-MAC
2709 * is used or not.
2710 * - If it is, the message contains the IV-prefix,
2711 * the CBC ciphertext, and the MAC.
2712 * - If it is not, the padded plaintext, and hence
2713 * the CBC ciphertext, has at least length maclen + 1
2714 * because there is at least the padding length byte.
2715 *
2716 * As the CBC ciphertext is not empty, both cases give the
2717 * lower bound minlen + maclen + 1 on the record size, which
2718 * we test for in the second check below.
2719 */
2720 if( rec->data_len < minlen + transform->ivlen ||
2721 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002724 "+ 1 ) ( + expl IV )", rec->data_len,
2725 transform->ivlen,
2726 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002728 }
2729
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002730 /*
2731 * Authenticate before decrypt if enabled
2732 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002734 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002735 {
Hanno Becker992b6872017-11-09 18:57:39 +00002736 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002739
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002740 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2741 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002742
Hanno Beckercab87e62019-04-29 13:52:53 +01002743 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002744
Hanno Beckercab87e62019-04-29 13:52:53 +01002745 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2746 add_data_len );
2747 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2748 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002749 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2750 data, rec->data_len );
2751 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2752 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002753
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002754 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2755 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002756 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002757 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002758
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002759 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2760 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002764 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002765 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002766 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002768
2769 /*
2770 * Check length sanity
2771 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002772 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002775 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002777 }
2778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002780 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002781 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002782 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002783 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002784 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002785 /* This is safe because data_len >= minlen + maclen + 1 initially,
2786 * and at this point we have at most subtracted maclen (note that
2787 * minlen == transform->ivlen here). */
2788 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002789
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002790 data += transform->ivlen;
2791 rec->data_offset += transform->ivlen;
2792 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002793 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002795
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002796 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2797 transform->iv_dec, transform->ivlen,
2798 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002801 return( ret );
2802 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002803
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002804 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2807 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002808 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002811 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002812 {
2813 /*
2814 * Save IV in SSL3 and TLS1
2815 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002816 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2817 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002818 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002819#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002820
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002821 /* Safe since data_len >= minlen + maclen + 1, so after having
2822 * subtracted at most minlen and maclen up to this point,
2823 * data_len > 0. */
2824 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002825
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002826 if( auth_done == 1 )
2827 {
2828 correct *= ( rec->data_len >= padlen + 1 );
2829 padlen *= ( rec->data_len >= padlen + 1 );
2830 }
2831 else
Paul Bakker45829992013-01-03 14:52:21 +01002832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002834 if( rec->data_len < transform->maclen + padlen + 1 )
2835 {
2836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2837 rec->data_len,
2838 transform->maclen,
2839 padlen + 1 ) );
2840 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002841#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002842
2843 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2844 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002845 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002846
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002847 padlen++;
2848
2849 /* Regardless of the validity of the padding,
2850 * we have data_len >= padlen here. */
2851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002853 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002854 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002855 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857#if defined(MBEDTLS_SSL_DEBUG_ALL)
2858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002859 "should be no more than %d",
2860 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002861#endif
Paul Bakker45829992013-01-03 14:52:21 +01002862 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002863 }
2864 }
2865 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2867#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2868 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002869 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002870 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002871 /* The padding check involves a series of up to 256
2872 * consecutive memory reads at the end of the record
2873 * plaintext buffer. In order to hide the length and
2874 * validity of the padding, always perform exactly
2875 * `min(256,plaintext_len)` reads (but take into account
2876 * only the last `padlen` bytes for the padding check). */
2877 size_t pad_count = 0;
2878 size_t real_count = 0;
2879 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002880
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002881 /* Index of first padding byte; it has been ensured above
2882 * that the subtraction is safe. */
2883 size_t const padding_idx = rec->data_len - padlen;
2884 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2885 size_t const start_idx = rec->data_len - num_checks;
2886 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002887
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002888 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002889 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002890 real_count |= ( idx >= padding_idx );
2891 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002892 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002893 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002896 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002898#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002899 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002900 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002901 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2903 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2906 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002907 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002908
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002909 /* If the padding was found to be invalid, padlen == 0
2910 * and the subtraction is safe. If the padding was found valid,
2911 * padlen hasn't been changed and the previous assertion
2912 * data_len >= padlen still holds. */
2913 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002914 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002915 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002917 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2920 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002921 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002922
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002923#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002925 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002926#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002927
2928 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002929 * Authenticate if not done yet.
2930 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002931 */
Hanno Becker52344c22018-01-03 15:24:20 +00002932#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002933 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002934 {
Hanno Becker992b6872017-11-09 18:57:39 +00002935 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002936
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002937 /* If the initial value of padlen was such that
2938 * data_len < maclen + padlen + 1, then padlen
2939 * got reset to 1, and the initial check
2940 * data_len >= minlen + maclen + 1
2941 * guarantees that at this point we still
2942 * have at least data_len >= maclen.
2943 *
2944 * If the initial value of padlen was such that
2945 * data_len >= maclen + padlen + 1, then we have
2946 * subtracted either padlen + 1 (if the padding was correct)
2947 * or 0 (if the padding was incorrect) since then,
2948 * hence data_len >= maclen in any case.
2949 */
2950 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002951
Hanno Beckercab87e62019-04-29 13:52:53 +01002952 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002955 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002956 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002957 ssl_mac( &transform->md_ctx_dec,
2958 transform->mac_dec,
2959 data, rec->data_len,
2960 rec->ctr, rec->type,
2961 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002962 }
2963 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2965#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2966 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002967 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002968 {
2969 /*
2970 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002971 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002972 *
2973 * Known timing attacks:
2974 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2975 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002976 * To compensate for different timings for the MAC calculation
2977 * depending on how much padding was removed (which is determined
2978 * by padlen), process extra_run more blocks through the hash
2979 * function.
2980 *
2981 * The formula in the paper is
2982 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2983 * where L1 is the size of the header plus the decrypted message
2984 * plus CBC padding and L2 is the size of the header plus the
2985 * decrypted message. This is for an underlying hash function
2986 * with 64-byte blocks.
2987 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2988 * correctly. We round down instead of up, so -56 is the correct
2989 * value for our calculations instead of -55.
2990 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002991 * Repeat the formula rather than defining a block_size variable.
2992 * This avoids requiring division by a variable at runtime
2993 * (which would be marginally less efficient and would require
2994 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002995 */
2996 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002997 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002998
2999 /*
3000 * The next two sizes are the minimum and maximum values of
3001 * in_msglen over all padlen values.
3002 *
3003 * They're independent of padlen, since we previously did
3004 * in_msglen -= padlen.
3005 *
3006 * Note that max_len + maclen is never more than the buffer
3007 * length, as we previously did in_msglen -= maclen too.
3008 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003009 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003010 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3011
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003012 memset( tmp, 0, sizeof( tmp ) );
3013
3014 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003015 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003016#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3017 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003018 case MBEDTLS_MD_MD5:
3019 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003020 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003021 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003022 extra_run =
3023 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3024 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003025 break;
3026#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003027#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003028 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003029 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003030 extra_run =
3031 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3032 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003033 break;
3034#endif
3035 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003037 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3038 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003039
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003040 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003041
Hanno Beckercab87e62019-04-29 13:52:53 +01003042 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3043 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003044 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3045 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003046 /* Make sure we access everything even when padlen > 0. This
3047 * makes the synchronisation requirements for just-in-time
3048 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003049 ssl_read_memory( data + rec->data_len, padlen );
3050 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003051
3052 /* Call mbedtls_md_process at least once due to cache attacks
3053 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003054 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003055 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003056
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003057 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003058
3059 /* Make sure we access all the memory that could contain the MAC,
3060 * before we check it in the next code block. This makes the
3061 * synchronisation requirements for just-in-time Prime+Probe
3062 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003063 ssl_read_memory( data + min_len,
3064 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003065 }
3066 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3068 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3071 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003072 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003073
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003074#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003075 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3076 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003077#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003078
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003079 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3080 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082#if defined(MBEDTLS_SSL_DEBUG_ALL)
3083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003084#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003085 correct = 0;
3086 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003087 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003088 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003089
3090 /*
3091 * Finally check the correct flag
3092 */
3093 if( correct == 0 )
3094 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003095#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003096
3097 /* Make extra sure authentication was performed, exactly once */
3098 if( auth_done != 1 )
3099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3101 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003102 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003103
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003104#if defined(MBEDTLS_SSL_CID)
3105 if( rec->cid_len != 0 )
3106 {
3107 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3108 &rec->type );
3109 if( ret != 0 )
3110 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3111 }
3112#endif /* MBEDTLS_SSL_CID */
3113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003115
3116 return( 0 );
3117}
3118
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003119#undef MAC_NONE
3120#undef MAC_PLAINTEXT
3121#undef MAC_CIPHERTEXT
3122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003124/*
3125 * Compression/decompression functions
3126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003128{
3129 int ret;
3130 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003131 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003132 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003133 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003136
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003137 if( len_pre == 0 )
3138 return( 0 );
3139
Paul Bakker2770fbd2012-07-03 13:30:23 +00003140 memcpy( msg_pre, ssl->out_msg, len_pre );
3141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003143 ssl->out_msglen ) );
3144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003146 ssl->out_msg, ssl->out_msglen );
3147
Paul Bakker48916f92012-09-16 19:57:18 +00003148 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3149 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3150 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003151 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003152
Paul Bakker48916f92012-09-16 19:57:18 +00003153 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003154 if( ret != Z_OK )
3155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3157 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003158 }
3159
Angus Grattond8213d02016-05-25 20:56:48 +10003160 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003161 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003164 ssl->out_msglen ) );
3165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003167 ssl->out_msg, ssl->out_msglen );
3168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003170
3171 return( 0 );
3172}
3173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003175{
3176 int ret;
3177 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003178 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003179 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003180 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003183
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003184 if( len_pre == 0 )
3185 return( 0 );
3186
Paul Bakker2770fbd2012-07-03 13:30:23 +00003187 memcpy( msg_pre, ssl->in_msg, len_pre );
3188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003190 ssl->in_msglen ) );
3191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003193 ssl->in_msg, ssl->in_msglen );
3194
Paul Bakker48916f92012-09-16 19:57:18 +00003195 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3196 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3197 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003198 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003199 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003200
Paul Bakker48916f92012-09-16 19:57:18 +00003201 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003202 if( ret != Z_OK )
3203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3205 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003206 }
3207
Angus Grattond8213d02016-05-25 20:56:48 +10003208 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003209 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003212 ssl->in_msglen ) );
3213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003215 ssl->in_msg, ssl->in_msglen );
3216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003218
3219 return( 0 );
3220}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3224static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226#if defined(MBEDTLS_SSL_PROTO_DTLS)
3227static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003228{
3229 /* If renegotiation is not enforced, retransmit until we would reach max
3230 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003231 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003232 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003233 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003234 unsigned char doublings = 1;
3235
3236 while( ratio != 0 )
3237 {
3238 ++doublings;
3239 ratio >>= 1;
3240 }
3241
3242 if( ++ssl->renego_records_seen > doublings )
3243 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003245 return( 0 );
3246 }
3247 }
3248
3249 return( ssl_write_hello_request( ssl ) );
3250}
3251#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003253
Paul Bakker5121ce52009-01-03 21:22:43 +00003254/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003255 * Fill the input message buffer by appending data to it.
3256 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003257 *
3258 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3259 * available (from this read and/or a previous one). Otherwise, an error code
3260 * is returned (possibly EOF or WANT_READ).
3261 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003262 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3263 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3264 * since we always read a whole datagram at once.
3265 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003266 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003267 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003268 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003269int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003270{
Paul Bakker23986e52011-04-24 08:57:21 +00003271 int ret;
3272 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003275
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003276 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003279 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003280 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003281 }
3282
Angus Grattond8213d02016-05-25 20:56:48 +10003283 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3286 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003287 }
3288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003290 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003291 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003292 uint32_t timeout;
3293
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003294 /* Just to be sure */
3295 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3296 {
3297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3298 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3299 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3300 }
3301
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003302 /*
3303 * The point is, we need to always read a full datagram at once, so we
3304 * sometimes read more then requested, and handle the additional data.
3305 * It could be the rest of the current record (while fetching the
3306 * header) and/or some other records in the same datagram.
3307 */
3308
3309 /*
3310 * Move to the next record in the already read datagram if applicable
3311 */
3312 if( ssl->next_record_offset != 0 )
3313 {
3314 if( ssl->in_left < ssl->next_record_offset )
3315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3317 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003318 }
3319
3320 ssl->in_left -= ssl->next_record_offset;
3321
3322 if( ssl->in_left != 0 )
3323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003324 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003325 ssl->next_record_offset ) );
3326 memmove( ssl->in_hdr,
3327 ssl->in_hdr + ssl->next_record_offset,
3328 ssl->in_left );
3329 }
3330
3331 ssl->next_record_offset = 0;
3332 }
3333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003335 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003336
3337 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003338 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003339 */
3340 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003343 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003344 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003345
3346 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003347 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003348 * are not at the beginning of a new record, the caller did something
3349 * wrong.
3350 */
3351 if( ssl->in_left != 0 )
3352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3354 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003355 }
3356
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003357 /*
3358 * Don't even try to read if time's out already.
3359 * This avoids by-passing the timer when repeatedly receiving messages
3360 * that will end up being dropped.
3361 */
3362 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003363 {
3364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003365 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003366 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003367 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003368 {
Angus Grattond8213d02016-05-25 20:56:48 +10003369 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003372 timeout = ssl->handshake->retransmit_timeout;
3373 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003374 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003377
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003378 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003379 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3380 timeout );
3381 else
3382 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003384 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003385
3386 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003388 }
3389
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003390 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003393 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003396 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003397 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003400 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003401 }
3402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003406 return( ret );
3407 }
3408
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003409 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003410 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003412 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003414 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003415 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003418 return( ret );
3419 }
3420
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003421 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003422 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003424 }
3425
Paul Bakker5121ce52009-01-03 21:22:43 +00003426 if( ret < 0 )
3427 return( ret );
3428
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003429 ssl->in_left = ret;
3430 }
3431 else
3432#endif
3433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003435 ssl->in_left, nb_want ) );
3436
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003437 while( ssl->in_left < nb_want )
3438 {
3439 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003440
3441 if( ssl_check_timer( ssl ) != 0 )
3442 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3443 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003444 {
3445 if( ssl->f_recv_timeout != NULL )
3446 {
3447 ret = ssl->f_recv_timeout( ssl->p_bio,
3448 ssl->in_hdr + ssl->in_left, len,
3449 ssl->conf->read_timeout );
3450 }
3451 else
3452 {
3453 ret = ssl->f_recv( ssl->p_bio,
3454 ssl->in_hdr + ssl->in_left, len );
3455 }
3456 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003459 ssl->in_left, nb_want ) );
3460 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003461
3462 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003464
3465 if( ret < 0 )
3466 return( ret );
3467
mohammad160352aecb92018-03-28 23:41:40 -07003468 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003469 {
Darryl Green11999bb2018-03-13 15:22:58 +00003470 MBEDTLS_SSL_DEBUG_MSG( 1,
3471 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003472 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003473 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3474 }
3475
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003476 ssl->in_left += ret;
3477 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003478 }
3479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003481
3482 return( 0 );
3483}
3484
3485/*
3486 * Flush any data not yet written
3487 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003489{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003490 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003491 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003494
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003495 if( ssl->f_send == NULL )
3496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003498 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003500 }
3501
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003502 /* Avoid incrementing counter if data is flushed */
3503 if( ssl->out_left == 0 )
3504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003506 return( 0 );
3507 }
3508
Paul Bakker5121ce52009-01-03 21:22:43 +00003509 while( ssl->out_left > 0 )
3510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3512 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003513
Hanno Becker2b1e3542018-08-06 11:19:13 +01003514 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003515 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003517 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003518
3519 if( ret <= 0 )
3520 return( ret );
3521
mohammad160352aecb92018-03-28 23:41:40 -07003522 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003523 {
Darryl Green11999bb2018-03-13 15:22:58 +00003524 MBEDTLS_SSL_DEBUG_MSG( 1,
3525 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003526 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003527 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3528 }
3529
Paul Bakker5121ce52009-01-03 21:22:43 +00003530 ssl->out_left -= ret;
3531 }
3532
Hanno Becker2b1e3542018-08-06 11:19:13 +01003533#if defined(MBEDTLS_SSL_PROTO_DTLS)
3534 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003535 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003536 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003537 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003538 else
3539#endif
3540 {
3541 ssl->out_hdr = ssl->out_buf + 8;
3542 }
3543 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003546
3547 return( 0 );
3548}
3549
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003550/*
3551 * Functions to handle the DTLS retransmission state machine
3552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003554/*
3555 * Append current handshake message to current outgoing flight
3556 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003558{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3561 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3562 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003563
3564 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003565 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003566 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003569 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003570 }
3571
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003572 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003573 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003576 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003577 }
3578
3579 /* Copy current handshake message with headers */
3580 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3581 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003582 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003583 msg->next = NULL;
3584
3585 /* Append to the current flight */
3586 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003587 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003588 else
3589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003591 while( cur->next != NULL )
3592 cur = cur->next;
3593 cur->next = msg;
3594 }
3595
Hanno Becker3b235902018-08-06 09:54:53 +01003596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003597 return( 0 );
3598}
3599
3600/*
3601 * Free the current flight of handshake messages
3602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003604{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 mbedtls_ssl_flight_item *cur = flight;
3606 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003607
3608 while( cur != NULL )
3609 {
3610 next = cur->next;
3611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612 mbedtls_free( cur->p );
3613 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003614
3615 cur = next;
3616 }
3617}
3618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3620static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003621#endif
3622
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003623/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003624 * Swap transform_out and out_ctr with the alternative ones
3625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003627{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003629 unsigned char tmp_out_ctr[8];
3630
3631 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003634 return;
3635 }
3636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003638
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003639 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003640 tmp_transform = ssl->transform_out;
3641 ssl->transform_out = ssl->handshake->alt_transform_out;
3642 ssl->handshake->alt_transform_out = tmp_transform;
3643
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003644 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003645 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3646 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003647 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003648
3649 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003650 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3653 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3658 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003659 }
3660 }
3661#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003662}
3663
3664/*
3665 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003666 */
3667int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3668{
3669 int ret = 0;
3670
3671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3672
3673 ret = mbedtls_ssl_flight_transmit( ssl );
3674
3675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3676
3677 return( ret );
3678}
3679
3680/*
3681 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003682 *
3683 * Need to remember the current message in case flush_output returns
3684 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003685 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003686 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003687int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003688{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003689 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003692 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003693 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003695
3696 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003697 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003698 ssl_swap_epochs( ssl );
3699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003700 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003701 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003702
3703 while( ssl->handshake->cur_msg != NULL )
3704 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003705 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003706 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003707
Hanno Beckere1dcb032018-08-17 16:47:58 +01003708 int const is_finished =
3709 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3710 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3711
Hanno Becker04da1892018-08-14 13:22:10 +01003712 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3713 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3714
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003715 /* Swap epochs before sending Finished: we can't do it after
3716 * sending ChangeCipherSpec, in case write returns WANT_READ.
3717 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003718 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003719 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003721 ssl_swap_epochs( ssl );
3722 }
3723
Hanno Becker67bc7c32018-08-06 11:33:50 +01003724 ret = ssl_get_remaining_payload_in_datagram( ssl );
3725 if( ret < 0 )
3726 return( ret );
3727 max_frag_len = (size_t) ret;
3728
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003729 /* CCS is copied as is, while HS messages may need fragmentation */
3730 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3731 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003732 if( max_frag_len == 0 )
3733 {
3734 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3735 return( ret );
3736
3737 continue;
3738 }
3739
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003740 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003741 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003742 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003743
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003744 /* Update position inside current message */
3745 ssl->handshake->cur_msg_p += cur->len;
3746 }
3747 else
3748 {
3749 const unsigned char * const p = ssl->handshake->cur_msg_p;
3750 const size_t hs_len = cur->len - 12;
3751 const size_t frag_off = p - ( cur->p + 12 );
3752 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003753 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003754
Hanno Beckere1dcb032018-08-17 16:47:58 +01003755 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003756 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003757 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003758 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003759
Hanno Becker67bc7c32018-08-06 11:33:50 +01003760 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3761 return( ret );
3762
3763 continue;
3764 }
3765 max_hs_frag_len = max_frag_len - 12;
3766
3767 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3768 max_hs_frag_len : rem_len;
3769
3770 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003771 {
3772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003773 (unsigned) cur_hs_frag_len,
3774 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003775 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003776
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003777 /* Messages are stored with handshake headers as if not fragmented,
3778 * copy beginning of headers then fill fragmentation fields.
3779 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3780 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003781
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003782 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3783 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3784 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3785
Hanno Becker67bc7c32018-08-06 11:33:50 +01003786 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3787 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3788 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003789
3790 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3791
Hanno Becker3f7b9732018-08-28 09:53:25 +01003792 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003793 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3794 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003795 ssl->out_msgtype = cur->type;
3796
3797 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003798 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003799 }
3800
3801 /* If done with the current message move to the next one if any */
3802 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3803 {
3804 if( cur->next != NULL )
3805 {
3806 ssl->handshake->cur_msg = cur->next;
3807 ssl->handshake->cur_msg_p = cur->next->p + 12;
3808 }
3809 else
3810 {
3811 ssl->handshake->cur_msg = NULL;
3812 ssl->handshake->cur_msg_p = NULL;
3813 }
3814 }
3815
3816 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003817 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003819 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003820 return( ret );
3821 }
3822 }
3823
Hanno Becker67bc7c32018-08-06 11:33:50 +01003824 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3825 return( ret );
3826
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003827 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3829 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003830 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003832 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003833 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3834 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003835
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003837
3838 return( 0 );
3839}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003840
3841/*
3842 * To be called when the last message of an incoming flight is received.
3843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003845{
3846 /* We won't need to resend that one any more */
3847 ssl_flight_free( ssl->handshake->flight );
3848 ssl->handshake->flight = NULL;
3849 ssl->handshake->cur_msg = NULL;
3850
3851 /* The next incoming flight will start with this msg_seq */
3852 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3853
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003854 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003855 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003856
Hanno Becker0271f962018-08-16 13:23:47 +01003857 /* Clear future message buffering structure. */
3858 ssl_buffering_free( ssl );
3859
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003860 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003861 ssl_set_timer( ssl, 0 );
3862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3864 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003867 }
3868 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003869 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003870}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003871
3872/*
3873 * To be called when the last message of an outgoing flight is send.
3874 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003875void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003876{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003877 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003878 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3881 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003883 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003884 }
3885 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003886 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003887}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003888#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003889
Paul Bakker5121ce52009-01-03 21:22:43 +00003890/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003891 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003892 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003893
3894/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003895 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003896 *
3897 * - fill in handshake headers
3898 * - update handshake checksum
3899 * - DTLS: save message for resending
3900 * - then pass to the record layer
3901 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003902 * DTLS: except for HelloRequest, messages are only queued, and will only be
3903 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003904 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003905 * Inputs:
3906 * - ssl->out_msglen: 4 + actual handshake message len
3907 * (4 is the size of handshake headers for TLS)
3908 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3909 * - ssl->out_msg + 4: the handshake message body
3910 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003911 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003912 * - ssl->out_msglen: the length of the record contents
3913 * (including handshake headers but excluding record headers)
3914 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003915 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003916int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003917{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003918 int ret;
3919 const size_t hs_len = ssl->out_msglen - 4;
3920 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003921
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3923
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003924 /*
3925 * Sanity checks
3926 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003927 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003928 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3929 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003930 /* In SSLv3, the client might send a NoCertificate alert. */
3931#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3932 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3933 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3934 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3935#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3936 {
3937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3938 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3939 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003940 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003941
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003942 /* Whenever we send anything different from a
3943 * HelloRequest we should be in a handshake - double check. */
3944 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3945 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003946 ssl->handshake == NULL )
3947 {
3948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3949 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3950 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003952#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003953 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003954 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003956 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3958 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003959 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003960#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003961
Hanno Beckerb50a2532018-08-06 11:52:54 +01003962 /* Double-check that we did not exceed the bounds
3963 * of the outgoing record buffer.
3964 * This should never fail as the various message
3965 * writing functions must obey the bounds of the
3966 * outgoing record buffer, but better be safe.
3967 *
3968 * Note: We deliberately do not check for the MTU or MFL here.
3969 */
3970 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3971 {
3972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3973 "size %u, maximum %u",
3974 (unsigned) ssl->out_msglen,
3975 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3976 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3977 }
3978
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003979 /*
3980 * Fill handshake headers
3981 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003982 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003983 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003984 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3985 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3986 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003987
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003988 /*
3989 * DTLS has additional fields in the Handshake layer,
3990 * between the length field and the actual payload:
3991 * uint16 message_seq;
3992 * uint24 fragment_offset;
3993 * uint24 fragment_length;
3994 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003995#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003996 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003997 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003998 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003999 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004000 {
4001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4002 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004003 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004004 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004005 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4006 }
4007
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004008 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004009 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004010
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004011 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004012 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004013 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004014 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4015 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4016 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004017 }
4018 else
4019 {
4020 ssl->out_msg[4] = 0;
4021 ssl->out_msg[5] = 0;
4022 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004023
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004024 /* Handshake hashes are computed without fragmentation,
4025 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004026 memset( ssl->out_msg + 6, 0x00, 3 );
4027 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004028 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004029#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004030
Hanno Becker0207e532018-08-28 10:28:28 +01004031 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004032 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4033 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004034 }
4035
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004036 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004037#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004038 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004039 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4040 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004041 {
4042 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004044 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004045 return( ret );
4046 }
4047 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004048 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004049#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004050 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004051 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004052 {
4053 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4054 return( ret );
4055 }
4056 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004057
4058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4059
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004060 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004061}
4062
4063/*
4064 * Record layer functions
4065 */
4066
4067/*
4068 * Write current record.
4069 *
4070 * Uses:
4071 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4072 * - ssl->out_msglen: length of the record content (excl headers)
4073 * - ssl->out_msg: record content
4074 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004075int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004076{
4077 int ret, done = 0;
4078 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004079 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004080
4081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004083#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004084 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004085 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004086 {
4087 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004090 return( ret );
4091 }
4092
4093 len = ssl->out_msglen;
4094 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004097#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4098 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004100 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004102 ret = mbedtls_ssl_hw_record_write( ssl );
4103 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4106 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004107 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004108
4109 if( ret == 0 )
4110 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004111 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004112#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004113 if( !done )
4114 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004115 unsigned i;
4116 size_t protected_record_size;
4117
Paul Bakker05ef8352012-05-08 09:17:57 +00004118 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004120 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004121
Hanno Becker19859472018-08-06 09:40:20 +01004122 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004123 ssl->out_len[0] = (unsigned char)( len >> 8 );
4124 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004125
Paul Bakker48916f92012-09-16 19:57:18 +00004126 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004127 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004128 mbedtls_record rec;
4129
4130 rec.buf = ssl->out_iv;
4131 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4132 ( ssl->out_iv - ssl->out_buf );
4133 rec.data_len = ssl->out_msglen;
4134 rec.data_offset = ssl->out_msg - rec.buf;
4135
4136 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4137 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4138 ssl->conf->transport, rec.ver );
4139 rec.type = ssl->out_msgtype;
4140
Hanno Becker43c24b82019-05-01 09:45:57 +01004141#if defined(MBEDTLS_SSL_CID)
4142 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004143 rec.cid_len = 0;
Hanno Becker43c24b82019-05-01 09:45:57 +01004144#endif /* MBEDTLS_SSL_CID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004145
Hanno Beckera18d1322018-01-03 14:27:32 +00004146 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004147 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004149 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004150 return( ret );
4151 }
4152
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004153 if( rec.data_offset != 0 )
4154 {
4155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4156 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4157 }
4158
Hanno Becker78f839d2019-03-14 12:56:23 +00004159 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004160 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4161 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004162 }
4163
Hanno Becker2b1e3542018-08-06 11:19:13 +01004164 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
4165
4166#if defined(MBEDTLS_SSL_PROTO_DTLS)
4167 /* In case of DTLS, double-check that we don't exceed
4168 * the remaining space in the datagram. */
4169 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4170 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004171 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004172 if( ret < 0 )
4173 return( ret );
4174
4175 if( protected_record_size > (size_t) ret )
4176 {
4177 /* Should never happen */
4178 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4179 }
4180 }
4181#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004183 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004184 "version = [%d:%d], msglen = %d",
4185 ssl->out_hdr[0], ssl->out_hdr[1],
4186 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004188 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004189 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004190
4191 ssl->out_left += protected_record_size;
4192 ssl->out_hdr += protected_record_size;
4193 ssl_update_out_pointers( ssl, ssl->transform_out );
4194
Hanno Becker04484622018-08-06 09:49:38 +01004195 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4196 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4197 break;
4198
4199 /* The loop goes to its end iff the counter is wrapping */
4200 if( i == ssl_ep_len( ssl ) )
4201 {
4202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4203 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4204 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004205 }
4206
Hanno Becker67bc7c32018-08-06 11:33:50 +01004207#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004208 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4209 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004210 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004211 size_t remaining;
4212 ret = ssl_get_remaining_payload_in_datagram( ssl );
4213 if( ret < 0 )
4214 {
4215 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4216 ret );
4217 return( ret );
4218 }
4219
4220 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004221 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004222 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004223 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004224 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004225 else
4226 {
Hanno Becker513815a2018-08-20 11:56:09 +01004227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004228 }
4229 }
4230#endif /* MBEDTLS_SSL_PROTO_DTLS */
4231
4232 if( ( flush == SSL_FORCE_FLUSH ) &&
4233 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004235 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004236 return( ret );
4237 }
4238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004240
4241 return( 0 );
4242}
4243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004244#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004245
4246static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4247{
4248 if( ssl->in_msglen < ssl->in_hslen ||
4249 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4250 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4251 {
4252 return( 1 );
4253 }
4254 return( 0 );
4255}
Hanno Becker44650b72018-08-16 12:51:11 +01004256
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004257static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004258{
4259 return( ( ssl->in_msg[9] << 16 ) |
4260 ( ssl->in_msg[10] << 8 ) |
4261 ssl->in_msg[11] );
4262}
4263
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004264static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004265{
4266 return( ( ssl->in_msg[6] << 16 ) |
4267 ( ssl->in_msg[7] << 8 ) |
4268 ssl->in_msg[8] );
4269}
4270
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004271static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004272{
4273 uint32_t msg_len, frag_off, frag_len;
4274
4275 msg_len = ssl_get_hs_total_len( ssl );
4276 frag_off = ssl_get_hs_frag_off( ssl );
4277 frag_len = ssl_get_hs_frag_len( ssl );
4278
4279 if( frag_off > msg_len )
4280 return( -1 );
4281
4282 if( frag_len > msg_len - frag_off )
4283 return( -1 );
4284
4285 if( frag_len + 12 > ssl->in_msglen )
4286 return( -1 );
4287
4288 return( 0 );
4289}
4290
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004291/*
4292 * Mark bits in bitmask (used for DTLS HS reassembly)
4293 */
4294static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4295{
4296 unsigned int start_bits, end_bits;
4297
4298 start_bits = 8 - ( offset % 8 );
4299 if( start_bits != 8 )
4300 {
4301 size_t first_byte_idx = offset / 8;
4302
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004303 /* Special case */
4304 if( len <= start_bits )
4305 {
4306 for( ; len != 0; len-- )
4307 mask[first_byte_idx] |= 1 << ( start_bits - len );
4308
4309 /* Avoid potential issues with offset or len becoming invalid */
4310 return;
4311 }
4312
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004313 offset += start_bits; /* Now offset % 8 == 0 */
4314 len -= start_bits;
4315
4316 for( ; start_bits != 0; start_bits-- )
4317 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4318 }
4319
4320 end_bits = len % 8;
4321 if( end_bits != 0 )
4322 {
4323 size_t last_byte_idx = ( offset + len ) / 8;
4324
4325 len -= end_bits; /* Now len % 8 == 0 */
4326
4327 for( ; end_bits != 0; end_bits-- )
4328 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4329 }
4330
4331 memset( mask + offset / 8, 0xFF, len / 8 );
4332}
4333
4334/*
4335 * Check that bitmask is full
4336 */
4337static int ssl_bitmask_check( unsigned char *mask, size_t len )
4338{
4339 size_t i;
4340
4341 for( i = 0; i < len / 8; i++ )
4342 if( mask[i] != 0xFF )
4343 return( -1 );
4344
4345 for( i = 0; i < len % 8; i++ )
4346 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4347 return( -1 );
4348
4349 return( 0 );
4350}
4351
Hanno Becker56e205e2018-08-16 09:06:12 +01004352/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004353static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004354 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004355{
Hanno Becker56e205e2018-08-16 09:06:12 +01004356 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004357
Hanno Becker56e205e2018-08-16 09:06:12 +01004358 alloc_len = 12; /* Handshake header */
4359 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004360
Hanno Beckerd07df862018-08-16 09:14:58 +01004361 if( add_bitmap )
4362 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004363
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004364 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004365}
Hanno Becker56e205e2018-08-16 09:06:12 +01004366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004367#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004368
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004369static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004370{
4371 return( ( ssl->in_msg[1] << 16 ) |
4372 ( ssl->in_msg[2] << 8 ) |
4373 ssl->in_msg[3] );
4374}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004375
Simon Butcher99000142016-10-13 17:21:01 +01004376int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004377{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004378 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004381 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004382 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004383 }
4384
Hanno Becker12555c62018-08-16 12:47:53 +01004385 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004387 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004388 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004389 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004391#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004392 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004393 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004394 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004395 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004396
Hanno Becker44650b72018-08-16 12:51:11 +01004397 if( ssl_check_hs_header( ssl ) != 0 )
4398 {
4399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4400 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4401 }
4402
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004403 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004404 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4405 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4406 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4407 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004408 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004409 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4410 {
4411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4412 recv_msg_seq,
4413 ssl->handshake->in_msg_seq ) );
4414 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4415 }
4416
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004417 /* Retransmit only on last message from previous flight, to avoid
4418 * too many retransmissions.
4419 * Besides, No sane server ever retransmits HelloVerifyRequest */
4420 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004421 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004424 "message_seq = %d, start_of_flight = %d",
4425 recv_msg_seq,
4426 ssl->handshake->in_flight_start_seq ) );
4427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004428 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004431 return( ret );
4432 }
4433 }
4434 else
4435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004437 "message_seq = %d, expected = %d",
4438 recv_msg_seq,
4439 ssl->handshake->in_msg_seq ) );
4440 }
4441
Hanno Becker90333da2017-10-10 11:27:13 +01004442 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004443 }
4444 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004445
Hanno Becker6d97ef52018-08-16 13:09:04 +01004446 /* Message reassembly is handled alongside buffering of future
4447 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004448 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004449 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004450 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004453 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004454 }
4455 }
4456 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004457#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004458 /* With TLS we don't handle fragmentation (for now) */
4459 if( ssl->in_msglen < ssl->in_hslen )
4460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4462 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004463 }
4464
Simon Butcher99000142016-10-13 17:21:01 +01004465 return( 0 );
4466}
4467
4468void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4469{
Hanno Becker0271f962018-08-16 13:23:47 +01004470 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004471
Hanno Becker0271f962018-08-16 13:23:47 +01004472 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004473 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004474 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004475 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004476
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004477 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004478#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004479 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004480 ssl->handshake != NULL )
4481 {
Hanno Becker0271f962018-08-16 13:23:47 +01004482 unsigned offset;
4483 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004484
Hanno Becker0271f962018-08-16 13:23:47 +01004485 /* Increment handshake sequence number */
4486 hs->in_msg_seq++;
4487
4488 /*
4489 * Clear up handshake buffering and reassembly structure.
4490 */
4491
4492 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004493 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004494
4495 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004496 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4497 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004498 offset++, hs_buf++ )
4499 {
4500 *hs_buf = *(hs_buf + 1);
4501 }
4502
4503 /* Create a fresh last entry */
4504 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004505 }
4506#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004507}
4508
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004509/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004510 * DTLS anti-replay: RFC 6347 4.1.2.6
4511 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004512 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4513 * Bit n is set iff record number in_window_top - n has been seen.
4514 *
4515 * Usually, in_window_top is the last record number seen and the lsb of
4516 * in_window is set. The only exception is the initial state (record number 0
4517 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004519#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4520static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004521{
4522 ssl->in_window_top = 0;
4523 ssl->in_window = 0;
4524}
4525
4526static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4527{
4528 return( ( (uint64_t) buf[0] << 40 ) |
4529 ( (uint64_t) buf[1] << 32 ) |
4530 ( (uint64_t) buf[2] << 24 ) |
4531 ( (uint64_t) buf[3] << 16 ) |
4532 ( (uint64_t) buf[4] << 8 ) |
4533 ( (uint64_t) buf[5] ) );
4534}
4535
4536/*
4537 * Return 0 if sequence number is acceptable, -1 otherwise
4538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004540{
4541 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4542 uint64_t bit;
4543
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004544 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004545 return( 0 );
4546
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004547 if( rec_seqnum > ssl->in_window_top )
4548 return( 0 );
4549
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004550 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004551
4552 if( bit >= 64 )
4553 return( -1 );
4554
4555 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4556 return( -1 );
4557
4558 return( 0 );
4559}
4560
4561/*
4562 * Update replay window on new validated record
4563 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004564void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004565{
4566 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4567
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004568 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004569 return;
4570
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004571 if( rec_seqnum > ssl->in_window_top )
4572 {
4573 /* Update window_top and the contents of the window */
4574 uint64_t shift = rec_seqnum - ssl->in_window_top;
4575
4576 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004577 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004578 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004579 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004580 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004581 ssl->in_window |= 1;
4582 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004583
4584 ssl->in_window_top = rec_seqnum;
4585 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004586 else
4587 {
4588 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004589 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004590
4591 if( bit < 64 ) /* Always true, but be extra sure */
4592 ssl->in_window |= (uint64_t) 1 << bit;
4593 }
4594}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004595#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004596
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004597#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004598/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004599static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4600
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004601/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004602 * Without any SSL context, check if a datagram looks like a ClientHello with
4603 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004604 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004605 *
4606 * - if cookie is valid, return 0
4607 * - if ClientHello looks superficially valid but cookie is not,
4608 * fill obuf and set olen, then
4609 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4610 * - otherwise return a specific error code
4611 */
4612static int ssl_check_dtls_clihlo_cookie(
4613 mbedtls_ssl_cookie_write_t *f_cookie_write,
4614 mbedtls_ssl_cookie_check_t *f_cookie_check,
4615 void *p_cookie,
4616 const unsigned char *cli_id, size_t cli_id_len,
4617 const unsigned char *in, size_t in_len,
4618 unsigned char *obuf, size_t buf_len, size_t *olen )
4619{
4620 size_t sid_len, cookie_len;
4621 unsigned char *p;
4622
4623 if( f_cookie_write == NULL || f_cookie_check == NULL )
4624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4625
4626 /*
4627 * Structure of ClientHello with record and handshake headers,
4628 * and expected values. We don't need to check a lot, more checks will be
4629 * done when actually parsing the ClientHello - skipping those checks
4630 * avoids code duplication and does not make cookie forging any easier.
4631 *
4632 * 0-0 ContentType type; copied, must be handshake
4633 * 1-2 ProtocolVersion version; copied
4634 * 3-4 uint16 epoch; copied, must be 0
4635 * 5-10 uint48 sequence_number; copied
4636 * 11-12 uint16 length; (ignored)
4637 *
4638 * 13-13 HandshakeType msg_type; (ignored)
4639 * 14-16 uint24 length; (ignored)
4640 * 17-18 uint16 message_seq; copied
4641 * 19-21 uint24 fragment_offset; copied, must be 0
4642 * 22-24 uint24 fragment_length; (ignored)
4643 *
4644 * 25-26 ProtocolVersion client_version; (ignored)
4645 * 27-58 Random random; (ignored)
4646 * 59-xx SessionID session_id; 1 byte len + sid_len content
4647 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4648 * ...
4649 *
4650 * Minimum length is 61 bytes.
4651 */
4652 if( in_len < 61 ||
4653 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4654 in[3] != 0 || in[4] != 0 ||
4655 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4656 {
4657 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4658 }
4659
4660 sid_len = in[59];
4661 if( sid_len > in_len - 61 )
4662 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4663
4664 cookie_len = in[60 + sid_len];
4665 if( cookie_len > in_len - 60 )
4666 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4667
4668 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4669 cli_id, cli_id_len ) == 0 )
4670 {
4671 /* Valid cookie */
4672 return( 0 );
4673 }
4674
4675 /*
4676 * If we get here, we've got an invalid cookie, let's prepare HVR.
4677 *
4678 * 0-0 ContentType type; copied
4679 * 1-2 ProtocolVersion version; copied
4680 * 3-4 uint16 epoch; copied
4681 * 5-10 uint48 sequence_number; copied
4682 * 11-12 uint16 length; olen - 13
4683 *
4684 * 13-13 HandshakeType msg_type; hello_verify_request
4685 * 14-16 uint24 length; olen - 25
4686 * 17-18 uint16 message_seq; copied
4687 * 19-21 uint24 fragment_offset; copied
4688 * 22-24 uint24 fragment_length; olen - 25
4689 *
4690 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4691 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4692 *
4693 * Minimum length is 28.
4694 */
4695 if( buf_len < 28 )
4696 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4697
4698 /* Copy most fields and adapt others */
4699 memcpy( obuf, in, 25 );
4700 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4701 obuf[25] = 0xfe;
4702 obuf[26] = 0xff;
4703
4704 /* Generate and write actual cookie */
4705 p = obuf + 28;
4706 if( f_cookie_write( p_cookie,
4707 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4708 {
4709 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4710 }
4711
4712 *olen = p - obuf;
4713
4714 /* Go back and fill length fields */
4715 obuf[27] = (unsigned char)( *olen - 28 );
4716
4717 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4718 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4719 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4720
4721 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4722 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4723
4724 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4725}
4726
4727/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004728 * Handle possible client reconnect with the same UDP quadruplet
4729 * (RFC 6347 Section 4.2.8).
4730 *
4731 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4732 * that looks like a ClientHello.
4733 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004734 * - if the input looks like a ClientHello without cookies,
4735 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004736 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004737 * - if the input looks like a ClientHello with a valid cookie,
4738 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004739 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004740 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004741 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004742 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004743 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4744 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004745 */
4746static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4747{
4748 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004749 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004750
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004751 ret = ssl_check_dtls_clihlo_cookie(
4752 ssl->conf->f_cookie_write,
4753 ssl->conf->f_cookie_check,
4754 ssl->conf->p_cookie,
4755 ssl->cli_id, ssl->cli_id_len,
4756 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004757 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004758
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004759 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4760
4761 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004762 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004763 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004764 * If the error is permanent we'll catch it later,
4765 * if it's not, then hopefully it'll work next time. */
4766 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4767
4768 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004769 }
4770
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004771 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004772 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004773 /* Got a valid cookie, partially reset context */
4774 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4775 {
4776 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4777 return( ret );
4778 }
4779
4780 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004781 }
4782
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004783 return( ret );
4784}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004785#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004786
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004787/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004788 * ContentType type;
4789 * ProtocolVersion version;
4790 * uint16 epoch; // DTLS only
4791 * uint48 sequence_number; // DTLS only
4792 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004793 *
4794 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004795 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004796 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4797 *
4798 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004799 * 1. proceed with the record if this function returns 0
4800 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4801 * 3. return CLIENT_RECONNECT if this function return that value
4802 * 4. drop the whole datagram if this function returns anything else.
4803 * Point 2 is needed when the peer is resending, and we have already received
4804 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004805 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004806static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004807{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004808 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004810 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 +02004811
Paul Bakker5121ce52009-01-03 21:22:43 +00004812 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004813 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004814 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004817 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004818 ssl->in_msgtype,
4819 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004820
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004821 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004822 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4823 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4824 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4825 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004828
4829#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004830 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4831 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004832 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4833#endif /* MBEDTLS_SSL_PROTO_DTLS */
4834 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4835 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004837 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004838 }
4839
4840 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004841 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4844 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004845 }
4846
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004847 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4850 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004851 }
4852
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004853 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004854 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004855 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4858 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004859 }
4860
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004861 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004862 * DTLS-related tests.
4863 * Check epoch before checking length constraint because
4864 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4865 * message gets duplicated before the corresponding Finished message,
4866 * the second ChangeCipherSpec should be discarded because it belongs
4867 * to an old epoch, but not because its length is shorter than
4868 * the minimum record length for packets using the new record transform.
4869 * Note that these two kinds of failures are handled differently,
4870 * as an unexpected record is silently skipped but an invalid
4871 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004872 */
4873#if defined(MBEDTLS_SSL_PROTO_DTLS)
4874 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4875 {
4876 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4877
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004878 /* Check epoch (and sequence number) with DTLS */
4879 if( rec_epoch != ssl->in_epoch )
4880 {
4881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4882 "expected %d, received %d",
4883 ssl->in_epoch, rec_epoch ) );
4884
4885#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4886 /*
4887 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4888 * access the first byte of record content (handshake type), as we
4889 * have an active transform (possibly iv_len != 0), so use the
4890 * fact that the record header len is 13 instead.
4891 */
4892 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4893 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4894 rec_epoch == 0 &&
4895 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4896 ssl->in_left > 13 &&
4897 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4898 {
4899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4900 "from the same port" ) );
4901 return( ssl_handle_possible_reconnect( ssl ) );
4902 }
4903 else
4904#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004905 {
4906 /* Consider buffering the record. */
4907 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4908 {
4909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4910 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4911 }
4912
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004913 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004914 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004915 }
4916
4917#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4918 /* Replay detection only works for the current epoch */
4919 if( rec_epoch == ssl->in_epoch &&
4920 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4921 {
4922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4923 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4924 }
4925#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004926
Hanno Becker52c6dc62017-05-26 16:07:36 +01004927 /* Drop unexpected ApplicationData records,
4928 * except at the beginning of renegotiations */
4929 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4930 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4931#if defined(MBEDTLS_SSL_RENEGOTIATION)
4932 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4933 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4934#endif
4935 )
4936 {
4937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4938 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4939 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004940 }
4941#endif /* MBEDTLS_SSL_PROTO_DTLS */
4942
Hanno Becker52c6dc62017-05-26 16:07:36 +01004943
4944 /* Check length against bounds of the current transform and version */
4945 if( ssl->transform_in == NULL )
4946 {
4947 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004948 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004949 {
4950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4951 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4952 }
4953 }
4954 else
4955 {
4956 if( ssl->in_msglen < ssl->transform_in->minlen )
4957 {
4958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4959 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4960 }
4961
4962#if defined(MBEDTLS_SSL_PROTO_SSL3)
4963 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004964 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004965 {
4966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4967 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4968 }
4969#endif
4970#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4971 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4972 /*
4973 * TLS encrypted messages can have up to 256 bytes of padding
4974 */
4975 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4976 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004977 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004978 {
4979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4980 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4981 }
4982#endif
4983 }
4984
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004985 return( 0 );
4986}
Paul Bakker5121ce52009-01-03 21:22:43 +00004987
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004988/*
4989 * If applicable, decrypt (and decompress) record content
4990 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004991static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004992{
4993 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004995 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4996 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004998#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4999 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005003 ret = mbedtls_ssl_hw_record_read( ssl );
5004 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005006 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5007 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005008 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005009
5010 if( ret == 0 )
5011 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005012 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005013#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005014 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005015 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005016 mbedtls_record rec;
5017
5018 rec.buf = ssl->in_iv;
5019 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5020 - ( ssl->in_iv - ssl->in_buf );
5021 rec.data_len = ssl->in_msglen;
5022 rec.data_offset = 0;
5023
5024 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5025 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5026 ssl->conf->transport, rec.ver );
5027 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005028 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5029 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005031 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005032 return( ret );
5033 }
5034
Hanno Becker29800d22018-08-07 14:30:18 +01005035 if( ssl->in_iv + rec.data_offset != ssl->in_msg )
5036 {
5037 /* Should never happen */
5038 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5039 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005040
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005041 ssl->in_msglen = rec.data_len;
5042 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5043 ssl->in_len[1] = (unsigned char)( rec.data_len );
5044
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005045 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5046 ssl->in_msg, ssl->in_msglen );
5047
Angus Grattond8213d02016-05-25 20:56:48 +10005048 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5051 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005052 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005053 else if( ssl->in_msglen == 0 )
5054 {
5055#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5056 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5057 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5058 {
5059 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5061 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5062 }
5063#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5064
5065 ssl->nb_zero++;
5066
5067 /*
5068 * Three or more empty messages may be a DoS attack
5069 * (excessive CPU consumption).
5070 */
5071 if( ssl->nb_zero > 3 )
5072 {
5073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
5074 "messages, possible DoS attack" ) );
5075 /* Q: Is that the right error code? */
5076 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5077 }
5078 }
5079 else
5080 ssl->nb_zero = 0;
5081
5082#if defined(MBEDTLS_SSL_PROTO_DTLS)
5083 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5084 {
5085 ; /* in_ctr read from peer, not maintained internally */
5086 }
5087 else
5088#endif
5089 {
5090 unsigned i;
5091 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5092 if( ++ssl->in_ctr[i - 1] != 0 )
5093 break;
5094
5095 /* The loop goes to its end iff the counter is wrapping */
5096 if( i == ssl_ep_len( ssl ) )
5097 {
5098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5099 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5100 }
5101 }
5102
Paul Bakker5121ce52009-01-03 21:22:43 +00005103 }
5104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005105#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005106 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005107 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005108 {
5109 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005111 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005112 return( ret );
5113 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005114 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005115#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005117#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005118 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005120 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005121 }
5122#endif
5123
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005124 return( 0 );
5125}
5126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005127static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005128
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005129/*
5130 * Read a record.
5131 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005132 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5133 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5134 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005135 */
Hanno Becker1097b342018-08-15 14:09:41 +01005136
5137/* Helper functions for mbedtls_ssl_read_record(). */
5138static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005139static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5140static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005141
Hanno Becker327c93b2018-08-15 13:56:18 +01005142int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005143 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005144{
5145 int ret;
5146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005148
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005149 if( ssl->keep_current_message == 0 )
5150 {
5151 do {
Simon Butcher99000142016-10-13 17:21:01 +01005152
Hanno Becker26994592018-08-15 14:14:59 +01005153 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005154 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005155 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005156
Hanno Beckere74d5562018-08-15 14:26:08 +01005157 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005158 {
Hanno Becker40f50842018-08-15 14:48:01 +01005159#if defined(MBEDTLS_SSL_PROTO_DTLS)
5160 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005161
Hanno Becker40f50842018-08-15 14:48:01 +01005162 /* We only check for buffered messages if the
5163 * current datagram is fully consumed. */
5164 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005165 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005166 {
Hanno Becker40f50842018-08-15 14:48:01 +01005167 if( ssl_load_buffered_message( ssl ) == 0 )
5168 have_buffered = 1;
5169 }
5170
5171 if( have_buffered == 0 )
5172#endif /* MBEDTLS_SSL_PROTO_DTLS */
5173 {
5174 ret = ssl_get_next_record( ssl );
5175 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5176 continue;
5177
5178 if( ret != 0 )
5179 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005180 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005181 return( ret );
5182 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005183 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005184 }
5185
5186 ret = mbedtls_ssl_handle_message_type( ssl );
5187
Hanno Becker40f50842018-08-15 14:48:01 +01005188#if defined(MBEDTLS_SSL_PROTO_DTLS)
5189 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5190 {
5191 /* Buffer future message */
5192 ret = ssl_buffer_message( ssl );
5193 if( ret != 0 )
5194 return( ret );
5195
5196 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5197 }
5198#endif /* MBEDTLS_SSL_PROTO_DTLS */
5199
Hanno Becker90333da2017-10-10 11:27:13 +01005200 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5201 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005202
5203 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005204 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005205 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005206 return( ret );
5207 }
5208
Hanno Becker327c93b2018-08-15 13:56:18 +01005209 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005210 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005211 {
5212 mbedtls_ssl_update_handshake_status( ssl );
5213 }
Simon Butcher99000142016-10-13 17:21:01 +01005214 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005215 else
Simon Butcher99000142016-10-13 17:21:01 +01005216 {
Hanno Becker02f59072018-08-15 14:00:24 +01005217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005218 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005219 }
5220
5221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5222
5223 return( 0 );
5224}
5225
Hanno Becker40f50842018-08-15 14:48:01 +01005226#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005227static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005228{
Hanno Becker40f50842018-08-15 14:48:01 +01005229 if( ssl->in_left > ssl->next_record_offset )
5230 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005231
Hanno Becker40f50842018-08-15 14:48:01 +01005232 return( 0 );
5233}
5234
5235static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5236{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005237 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005238 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005239 int ret = 0;
5240
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005241 if( hs == NULL )
5242 return( -1 );
5243
Hanno Beckere00ae372018-08-20 09:39:42 +01005244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5245
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005246 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5247 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5248 {
5249 /* Check if we have seen a ChangeCipherSpec before.
5250 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005251 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005252 {
5253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5254 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005255 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005256 }
5257
Hanno Becker39b8bc92018-08-28 17:17:13 +01005258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005259 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5260 ssl->in_msglen = 1;
5261 ssl->in_msg[0] = 1;
5262
5263 /* As long as they are equal, the exact value doesn't matter. */
5264 ssl->in_left = 0;
5265 ssl->next_record_offset = 0;
5266
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005267 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005268 goto exit;
5269 }
Hanno Becker37f95322018-08-16 13:55:32 +01005270
Hanno Beckerb8f50142018-08-28 10:01:34 +01005271#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005272 /* Debug only */
5273 {
5274 unsigned offset;
5275 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5276 {
5277 hs_buf = &hs->buffering.hs[offset];
5278 if( hs_buf->is_valid == 1 )
5279 {
5280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5281 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005282 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005283 }
5284 }
5285 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005286#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005287
5288 /* Check if we have buffered and/or fully reassembled the
5289 * next handshake message. */
5290 hs_buf = &hs->buffering.hs[0];
5291 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5292 {
5293 /* Synthesize a record containing the buffered HS message. */
5294 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5295 ( hs_buf->data[2] << 8 ) |
5296 hs_buf->data[3];
5297
5298 /* Double-check that we haven't accidentally buffered
5299 * a message that doesn't fit into the input buffer. */
5300 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5301 {
5302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5303 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5304 }
5305
5306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5307 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5308 hs_buf->data, msg_len + 12 );
5309
5310 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5311 ssl->in_hslen = msg_len + 12;
5312 ssl->in_msglen = msg_len + 12;
5313 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5314
5315 ret = 0;
5316 goto exit;
5317 }
5318 else
5319 {
5320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5321 hs->in_msg_seq ) );
5322 }
5323
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005324 ret = -1;
5325
5326exit:
5327
5328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5329 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005330}
5331
Hanno Beckera02b0b42018-08-21 17:20:27 +01005332static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5333 size_t desired )
5334{
5335 int offset;
5336 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5338 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005339
Hanno Becker01315ea2018-08-21 17:22:17 +01005340 /* Get rid of future records epoch first, if such exist. */
5341 ssl_free_buffered_record( ssl );
5342
5343 /* Check if we have enough space available now. */
5344 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5345 hs->buffering.total_bytes_buffered ) )
5346 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005348 return( 0 );
5349 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005350
Hanno Becker4f432ad2018-08-28 10:02:32 +01005351 /* We don't have enough space to buffer the next expected handshake
5352 * message. Remove buffers used for future messages to gain space,
5353 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005354 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5355 offset >= 0; offset-- )
5356 {
5357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5358 offset ) );
5359
Hanno Beckerb309b922018-08-23 13:18:05 +01005360 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005361
5362 /* Check if we have enough space available now. */
5363 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5364 hs->buffering.total_bytes_buffered ) )
5365 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005367 return( 0 );
5368 }
5369 }
5370
5371 return( -1 );
5372}
5373
Hanno Becker40f50842018-08-15 14:48:01 +01005374static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5375{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005376 int ret = 0;
5377 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5378
5379 if( hs == NULL )
5380 return( 0 );
5381
5382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5383
5384 switch( ssl->in_msgtype )
5385 {
5386 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005388
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005389 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005390 break;
5391
5392 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005393 {
5394 unsigned recv_msg_seq_offset;
5395 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5396 mbedtls_ssl_hs_buffer *hs_buf;
5397 size_t msg_len = ssl->in_hslen - 12;
5398
5399 /* We should never receive an old handshake
5400 * message - double-check nonetheless. */
5401 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5402 {
5403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5404 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5405 }
5406
5407 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5408 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5409 {
5410 /* Silently ignore -- message too far in the future */
5411 MBEDTLS_SSL_DEBUG_MSG( 2,
5412 ( "Ignore future HS message with sequence number %u, "
5413 "buffering window %u - %u",
5414 recv_msg_seq, ssl->handshake->in_msg_seq,
5415 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5416
5417 goto exit;
5418 }
5419
5420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5421 recv_msg_seq, recv_msg_seq_offset ) );
5422
5423 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5424
5425 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005426 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005427 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005428 size_t reassembly_buf_sz;
5429
Hanno Becker37f95322018-08-16 13:55:32 +01005430 hs_buf->is_fragmented =
5431 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5432
5433 /* We copy the message back into the input buffer
5434 * after reassembly, so check that it's not too large.
5435 * This is an implementation-specific limitation
5436 * and not one from the standard, hence it is not
5437 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005438 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005439 {
5440 /* Ignore message */
5441 goto exit;
5442 }
5443
Hanno Beckere0b150f2018-08-21 15:51:03 +01005444 /* Check if we have enough space to buffer the message. */
5445 if( hs->buffering.total_bytes_buffered >
5446 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5447 {
5448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5449 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5450 }
5451
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005452 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5453 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005454
5455 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5456 hs->buffering.total_bytes_buffered ) )
5457 {
5458 if( recv_msg_seq_offset > 0 )
5459 {
5460 /* If we can't buffer a future message because
5461 * of space limitations -- ignore. */
5462 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",
5463 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5464 (unsigned) hs->buffering.total_bytes_buffered ) );
5465 goto exit;
5466 }
Hanno Beckere1801392018-08-21 16:51:05 +01005467 else
5468 {
5469 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",
5470 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5471 (unsigned) hs->buffering.total_bytes_buffered ) );
5472 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005473
Hanno Beckera02b0b42018-08-21 17:20:27 +01005474 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005475 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005476 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",
5477 (unsigned) msg_len,
5478 (unsigned) reassembly_buf_sz,
5479 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005480 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005481 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5482 goto exit;
5483 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005484 }
5485
5486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5487 msg_len ) );
5488
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005489 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5490 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005491 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005492 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005493 goto exit;
5494 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005495 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005496
5497 /* Prepare final header: copy msg_type, length and message_seq,
5498 * then add standardised fragment_offset and fragment_length */
5499 memcpy( hs_buf->data, ssl->in_msg, 6 );
5500 memset( hs_buf->data + 6, 0, 3 );
5501 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5502
5503 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005504
5505 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005506 }
5507 else
5508 {
5509 /* Make sure msg_type and length are consistent */
5510 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5511 {
5512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5513 /* Ignore */
5514 goto exit;
5515 }
5516 }
5517
Hanno Becker4422bbb2018-08-20 09:40:19 +01005518 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005519 {
5520 size_t frag_len, frag_off;
5521 unsigned char * const msg = hs_buf->data + 12;
5522
5523 /*
5524 * Check and copy current fragment
5525 */
5526
5527 /* Validation of header fields already done in
5528 * mbedtls_ssl_prepare_handshake_record(). */
5529 frag_off = ssl_get_hs_frag_off( ssl );
5530 frag_len = ssl_get_hs_frag_len( ssl );
5531
5532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5533 frag_off, frag_len ) );
5534 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5535
5536 if( hs_buf->is_fragmented )
5537 {
5538 unsigned char * const bitmask = msg + msg_len;
5539 ssl_bitmask_set( bitmask, frag_off, frag_len );
5540 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5541 msg_len ) == 0 );
5542 }
5543 else
5544 {
5545 hs_buf->is_complete = 1;
5546 }
5547
5548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5549 hs_buf->is_complete ? "" : "not yet " ) );
5550 }
5551
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005552 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005553 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005554
5555 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005556 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005557 break;
5558 }
5559
5560exit:
5561
5562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5563 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005564}
5565#endif /* MBEDTLS_SSL_PROTO_DTLS */
5566
Hanno Becker1097b342018-08-15 14:09:41 +01005567static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005568{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005569 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005570 * Consume last content-layer message and potentially
5571 * update in_msglen which keeps track of the contents'
5572 * consumption state.
5573 *
5574 * (1) Handshake messages:
5575 * Remove last handshake message, move content
5576 * and adapt in_msglen.
5577 *
5578 * (2) Alert messages:
5579 * Consume whole record content, in_msglen = 0.
5580 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005581 * (3) Change cipher spec:
5582 * Consume whole record content, in_msglen = 0.
5583 *
5584 * (4) Application data:
5585 * Don't do anything - the record layer provides
5586 * the application data as a stream transport
5587 * and consumes through mbedtls_ssl_read only.
5588 *
5589 */
5590
5591 /* Case (1): Handshake messages */
5592 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005593 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005594 /* Hard assertion to be sure that no application data
5595 * is in flight, as corrupting ssl->in_msglen during
5596 * ssl->in_offt != NULL is fatal. */
5597 if( ssl->in_offt != NULL )
5598 {
5599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5600 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5601 }
5602
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005603 /*
5604 * Get next Handshake message in the current record
5605 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005606
Hanno Becker4a810fb2017-05-24 16:27:30 +01005607 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005608 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005609 * current handshake content: If DTLS handshake
5610 * fragmentation is used, that's the fragment
5611 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005612 * size here is faulty and should be changed at
5613 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005614 * (2) While it doesn't seem to cause problems, one
5615 * has to be very careful not to assume that in_hslen
5616 * is always <= in_msglen in a sensible communication.
5617 * Again, it's wrong for DTLS handshake fragmentation.
5618 * The following check is therefore mandatory, and
5619 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005620 * Additionally, ssl->in_hslen might be arbitrarily out of
5621 * bounds after handling a DTLS message with an unexpected
5622 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005623 */
5624 if( ssl->in_hslen < ssl->in_msglen )
5625 {
5626 ssl->in_msglen -= ssl->in_hslen;
5627 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5628 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005629
Hanno Becker4a810fb2017-05-24 16:27:30 +01005630 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5631 ssl->in_msg, ssl->in_msglen );
5632 }
5633 else
5634 {
5635 ssl->in_msglen = 0;
5636 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005637
Hanno Becker4a810fb2017-05-24 16:27:30 +01005638 ssl->in_hslen = 0;
5639 }
5640 /* Case (4): Application data */
5641 else if( ssl->in_offt != NULL )
5642 {
5643 return( 0 );
5644 }
5645 /* Everything else (CCS & Alerts) */
5646 else
5647 {
5648 ssl->in_msglen = 0;
5649 }
5650
Hanno Becker1097b342018-08-15 14:09:41 +01005651 return( 0 );
5652}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005653
Hanno Beckere74d5562018-08-15 14:26:08 +01005654static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5655{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005656 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005657 return( 1 );
5658
5659 return( 0 );
5660}
5661
Hanno Becker5f066e72018-08-16 14:56:31 +01005662#if defined(MBEDTLS_SSL_PROTO_DTLS)
5663
5664static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5665{
5666 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5667 if( hs == NULL )
5668 return;
5669
Hanno Becker01315ea2018-08-21 17:22:17 +01005670 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005671 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005672 hs->buffering.total_bytes_buffered -=
5673 hs->buffering.future_record.len;
5674
5675 mbedtls_free( hs->buffering.future_record.data );
5676 hs->buffering.future_record.data = NULL;
5677 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005678}
5679
5680static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5681{
5682 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5683 unsigned char * rec;
5684 size_t rec_len;
5685 unsigned rec_epoch;
5686
5687 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5688 return( 0 );
5689
5690 if( hs == NULL )
5691 return( 0 );
5692
Hanno Becker5f066e72018-08-16 14:56:31 +01005693 rec = hs->buffering.future_record.data;
5694 rec_len = hs->buffering.future_record.len;
5695 rec_epoch = hs->buffering.future_record.epoch;
5696
5697 if( rec == NULL )
5698 return( 0 );
5699
Hanno Becker4cb782d2018-08-20 11:19:05 +01005700 /* Only consider loading future records if the
5701 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005702 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005703 return( 0 );
5704
Hanno Becker5f066e72018-08-16 14:56:31 +01005705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5706
5707 if( rec_epoch != ssl->in_epoch )
5708 {
5709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5710 goto exit;
5711 }
5712
5713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5714
5715 /* Double-check that the record is not too large */
5716 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5717 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5718 {
5719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5720 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5721 }
5722
5723 memcpy( ssl->in_hdr, rec, rec_len );
5724 ssl->in_left = rec_len;
5725 ssl->next_record_offset = 0;
5726
5727 ssl_free_buffered_record( ssl );
5728
5729exit:
5730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5731 return( 0 );
5732}
5733
5734static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5735{
5736 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5737 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005738 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005739
5740 /* Don't buffer future records outside handshakes. */
5741 if( hs == NULL )
5742 return( 0 );
5743
5744 /* Only buffer handshake records (we are only interested
5745 * in Finished messages). */
5746 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5747 return( 0 );
5748
5749 /* Don't buffer more than one future epoch record. */
5750 if( hs->buffering.future_record.data != NULL )
5751 return( 0 );
5752
Hanno Becker01315ea2018-08-21 17:22:17 +01005753 /* Don't buffer record if there's not enough buffering space remaining. */
5754 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5755 hs->buffering.total_bytes_buffered ) )
5756 {
5757 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",
5758 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5759 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005760 return( 0 );
5761 }
5762
Hanno Becker5f066e72018-08-16 14:56:31 +01005763 /* Buffer record */
5764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5765 ssl->in_epoch + 1 ) );
5766 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5767 rec_hdr_len + ssl->in_msglen );
5768
5769 /* ssl_parse_record_header() only considers records
5770 * of the next epoch as candidates for buffering. */
5771 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005772 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005773
5774 hs->buffering.future_record.data =
5775 mbedtls_calloc( 1, hs->buffering.future_record.len );
5776 if( hs->buffering.future_record.data == NULL )
5777 {
5778 /* If we run out of RAM trying to buffer a
5779 * record from the next epoch, just ignore. */
5780 return( 0 );
5781 }
5782
Hanno Becker01315ea2018-08-21 17:22:17 +01005783 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005784
Hanno Becker01315ea2018-08-21 17:22:17 +01005785 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005786 return( 0 );
5787}
5788
5789#endif /* MBEDTLS_SSL_PROTO_DTLS */
5790
Hanno Beckere74d5562018-08-15 14:26:08 +01005791static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005792{
5793 int ret;
5794
Hanno Becker5f066e72018-08-16 14:56:31 +01005795#if defined(MBEDTLS_SSL_PROTO_DTLS)
5796 /* We might have buffered a future record; if so,
5797 * and if the epoch matches now, load it.
5798 * On success, this call will set ssl->in_left to
5799 * the length of the buffered record, so that
5800 * the calls to ssl_fetch_input() below will
5801 * essentially be no-ops. */
5802 ret = ssl_load_buffered_record( ssl );
5803 if( ret != 0 )
5804 return( ret );
5805#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005807 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005809 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005810 return( ret );
5811 }
5812
5813 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005815#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005816 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5817 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005818 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005819 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5820 {
5821 ret = ssl_buffer_future_record( ssl );
5822 if( ret != 0 )
5823 return( ret );
5824
5825 /* Fall through to handling of unexpected records */
5826 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5827 }
5828
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005829 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5830 {
5831 /* Skip unexpected record (but not whole datagram) */
5832 ssl->next_record_offset = ssl->in_msglen
5833 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005834
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5836 "(header)" ) );
5837 }
5838 else
5839 {
5840 /* Skip invalid record and the rest of the datagram */
5841 ssl->next_record_offset = 0;
5842 ssl->in_left = 0;
5843
5844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5845 "(header)" ) );
5846 }
5847
5848 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005849 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005850 }
5851#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005852 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005853 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005854
5855 /*
5856 * Read and optionally decrypt the message contents
5857 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005858 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5859 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005861 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005862 return( ret );
5863 }
5864
5865 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005866#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005867 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005870 if( ssl->next_record_offset < ssl->in_left )
5871 {
5872 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5873 }
5874 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005875 else
5876#endif
5877 ssl->in_left = 0;
5878
5879 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005881#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005882 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005883 {
5884 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005885 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5886 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005887 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005888 /* Except when waiting for Finished as a bad mac here
5889 * probably means something went wrong in the handshake
5890 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5891 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5892 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5893 {
5894#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5895 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5896 {
5897 mbedtls_ssl_send_alert_message( ssl,
5898 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5899 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5900 }
5901#endif
5902 return( ret );
5903 }
5904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005905#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005906 if( ssl->conf->badmac_limit != 0 &&
5907 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5910 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005911 }
5912#endif
5913
Hanno Becker4a810fb2017-05-24 16:27:30 +01005914 /* As above, invalid records cause
5915 * dismissal of the whole datagram. */
5916
5917 ssl->next_record_offset = 0;
5918 ssl->in_left = 0;
5919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005921 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005922 }
5923
5924 return( ret );
5925 }
5926 else
5927#endif
5928 {
5929 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005930#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5931 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005933 mbedtls_ssl_send_alert_message( ssl,
5934 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5935 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005936 }
5937#endif
5938 return( ret );
5939 }
5940 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005941
Simon Butcher99000142016-10-13 17:21:01 +01005942 return( 0 );
5943}
5944
5945int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5946{
5947 int ret;
5948
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005949 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005950 * Handle particular types of records
5951 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005952 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005953 {
Simon Butcher99000142016-10-13 17:21:01 +01005954 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5955 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005956 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005957 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005958 }
5959
Hanno Beckere678eaa2018-08-21 14:57:46 +01005960 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005961 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005962 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005963 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5965 ssl->in_msglen ) );
5966 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005967 }
5968
Hanno Beckere678eaa2018-08-21 14:57:46 +01005969 if( ssl->in_msg[0] != 1 )
5970 {
5971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5972 ssl->in_msg[0] ) );
5973 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5974 }
5975
5976#if defined(MBEDTLS_SSL_PROTO_DTLS)
5977 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5978 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5979 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5980 {
5981 if( ssl->handshake == NULL )
5982 {
5983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5984 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5985 }
5986
5987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5988 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5989 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005990#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005991 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005994 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005995 if( ssl->in_msglen != 2 )
5996 {
5997 /* Note: Standard allows for more than one 2 byte alert
5998 to be packed in a single message, but Mbed TLS doesn't
5999 currently support this. */
6000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6001 ssl->in_msglen ) );
6002 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6003 }
6004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006006 ssl->in_msg[0], ssl->in_msg[1] ) );
6007
6008 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006009 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006010 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006011 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006014 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006016 }
6017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006018 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6019 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6022 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006023 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006024
6025#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6026 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6027 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6028 {
Hanno Becker90333da2017-10-10 11:27:13 +01006029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006030 /* Will be handled when trying to parse ServerHello */
6031 return( 0 );
6032 }
6033#endif
6034
6035#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6036 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6037 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6038 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6039 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6040 {
6041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6042 /* Will be handled in mbedtls_ssl_parse_certificate() */
6043 return( 0 );
6044 }
6045#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6046
6047 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006048 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006049 }
6050
Hanno Beckerc76c6192017-06-06 10:03:17 +01006051#if defined(MBEDTLS_SSL_PROTO_DTLS)
6052 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6053 ssl->handshake != NULL &&
6054 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6055 {
6056 ssl_handshake_wrapup_free_hs_transform( ssl );
6057 }
6058#endif
6059
Paul Bakker5121ce52009-01-03 21:22:43 +00006060 return( 0 );
6061}
6062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006064{
6065 int ret;
6066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6068 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6069 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006070 {
6071 return( ret );
6072 }
6073
6074 return( 0 );
6075}
6076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006077int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006078 unsigned char level,
6079 unsigned char message )
6080{
6081 int ret;
6082
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006083 if( ssl == NULL || ssl->conf == NULL )
6084 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006087 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006089 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006090 ssl->out_msglen = 2;
6091 ssl->out_msg[0] = level;
6092 ssl->out_msg[1] = message;
6093
Hanno Becker67bc7c32018-08-06 11:33:50 +01006094 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006096 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006097 return( ret );
6098 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006100
6101 return( 0 );
6102}
6103
Hanno Beckerb9d44792019-02-08 07:19:04 +00006104#if defined(MBEDTLS_X509_CRT_PARSE_C)
6105static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6106{
6107#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6108 if( session->peer_cert != NULL )
6109 {
6110 mbedtls_x509_crt_free( session->peer_cert );
6111 mbedtls_free( session->peer_cert );
6112 session->peer_cert = NULL;
6113 }
6114#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6115 if( session->peer_cert_digest != NULL )
6116 {
6117 /* Zeroization is not necessary. */
6118 mbedtls_free( session->peer_cert_digest );
6119 session->peer_cert_digest = NULL;
6120 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6121 session->peer_cert_digest_len = 0;
6122 }
6123#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6124}
6125#endif /* MBEDTLS_X509_CRT_PARSE_C */
6126
Paul Bakker5121ce52009-01-03 21:22:43 +00006127/*
6128 * Handshake functions
6129 */
Hanno Becker21489932019-02-05 13:20:55 +00006130#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006131/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006132int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006133{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006134 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6135 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006138
Hanno Becker7177a882019-02-05 13:36:46 +00006139 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006142 ssl->state++;
6143 return( 0 );
6144 }
6145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6147 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006148}
6149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006150int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006151{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006152 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6153 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006156
Hanno Becker7177a882019-02-05 13:36:46 +00006157 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006160 ssl->state++;
6161 return( 0 );
6162 }
6163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6165 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006166}
Gilles Peskinef9828522017-05-03 12:28:43 +02006167
Hanno Becker21489932019-02-05 13:20:55 +00006168#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006169/* Some certificate support -> implement write and parse */
6170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006171int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006172{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006173 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006174 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006175 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006176 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6177 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006180
Hanno Becker7177a882019-02-05 13:36:46 +00006181 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006183 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006184 ssl->state++;
6185 return( 0 );
6186 }
6187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006188#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006189 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006190 {
6191 if( ssl->client_auth == 0 )
6192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006194 ssl->state++;
6195 return( 0 );
6196 }
6197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006198#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006199 /*
6200 * If using SSLv3 and got no cert, send an Alert message
6201 * (otherwise an empty Certificate message will be sent).
6202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006203 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6204 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006205 {
6206 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006207 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6208 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6209 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006212 goto write_msg;
6213 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006214#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006215 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006216#endif /* MBEDTLS_SSL_CLI_C */
6217#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006218 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006220 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6223 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006224 }
6225 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006226#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006228 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006229
6230 /*
6231 * 0 . 0 handshake type
6232 * 1 . 3 handshake length
6233 * 4 . 6 length of all certs
6234 * 7 . 9 length of cert. 1
6235 * 10 . n-1 peer certificate
6236 * n . n+2 length of cert. 2
6237 * n+3 . ... upper level cert, etc.
6238 */
6239 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006240 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006241
Paul Bakker29087132010-03-21 21:03:34 +00006242 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006243 {
6244 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006245 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006248 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006249 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006250 }
6251
6252 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6253 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6254 ssl->out_msg[i + 2] = (unsigned char)( n );
6255
6256 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6257 i += n; crt = crt->next;
6258 }
6259
6260 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6261 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6262 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6263
6264 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006265 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6266 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006267
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006268#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006269write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006270#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006271
6272 ssl->state++;
6273
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006274 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006275 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006276 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006277 return( ret );
6278 }
6279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006281
Paul Bakkered27a042013-04-18 22:46:23 +02006282 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006283}
6284
Hanno Becker84879e32019-01-31 07:44:03 +00006285#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006286
6287#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006288static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6289 unsigned char *crt_buf,
6290 size_t crt_buf_len )
6291{
6292 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6293
6294 if( peer_crt == NULL )
6295 return( -1 );
6296
6297 if( peer_crt->raw.len != crt_buf_len )
6298 return( -1 );
6299
Hanno Becker46f34d02019-02-08 14:00:04 +00006300 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006301}
Hanno Becker177475a2019-02-05 17:02:46 +00006302#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6303static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6304 unsigned char *crt_buf,
6305 size_t crt_buf_len )
6306{
6307 int ret;
6308 unsigned char const * const peer_cert_digest =
6309 ssl->session->peer_cert_digest;
6310 mbedtls_md_type_t const peer_cert_digest_type =
6311 ssl->session->peer_cert_digest_type;
6312 mbedtls_md_info_t const * const digest_info =
6313 mbedtls_md_info_from_type( peer_cert_digest_type );
6314 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6315 size_t digest_len;
6316
6317 if( peer_cert_digest == NULL || digest_info == NULL )
6318 return( -1 );
6319
6320 digest_len = mbedtls_md_get_size( digest_info );
6321 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6322 return( -1 );
6323
6324 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6325 if( ret != 0 )
6326 return( -1 );
6327
6328 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6329}
6330#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006331#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006332
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006333/*
6334 * Once the certificate message is read, parse it into a cert chain and
6335 * perform basic checks, but leave actual verification to the caller
6336 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006337static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6338 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006339{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006340 int ret;
6341#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6342 int crt_cnt=0;
6343#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006344 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006345 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006347 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006350 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6351 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006352 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006353 }
6354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6356 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006359 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6360 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006361 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006362 }
6363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006365
Paul Bakker5121ce52009-01-03 21:22:43 +00006366 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006367 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006368 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006369 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006370
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006371 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006372 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006375 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6376 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006377 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006378 }
6379
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006380 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6381 i += 3;
6382
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006383 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006384 while( i < ssl->in_hslen )
6385 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006386 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006387 if ( i + 3 > ssl->in_hslen ) {
6388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006389 mbedtls_ssl_send_alert_message( ssl,
6390 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6391 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006392 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6393 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006394 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6395 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006396 if( ssl->in_msg[i] != 0 )
6397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006399 mbedtls_ssl_send_alert_message( ssl,
6400 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6401 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006402 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006403 }
6404
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006405 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006406 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6407 | (unsigned int) ssl->in_msg[i + 2];
6408 i += 3;
6409
6410 if( n < 128 || i + n > ssl->in_hslen )
6411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006413 mbedtls_ssl_send_alert_message( ssl,
6414 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6415 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006417 }
6418
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006419 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006420#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6421 if( crt_cnt++ == 0 &&
6422 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6423 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006424 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006425 /* During client-side renegotiation, check that the server's
6426 * end-CRTs hasn't changed compared to the initial handshake,
6427 * mitigating the triple handshake attack. On success, reuse
6428 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006429 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6430 if( ssl_check_peer_crt_unchanged( ssl,
6431 &ssl->in_msg[i],
6432 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006433 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6435 mbedtls_ssl_send_alert_message( ssl,
6436 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6437 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6438 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006439 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006440
6441 /* Now we can safely free the original chain. */
6442 ssl_clear_peer_cert( ssl->session );
6443 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006444#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6445
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006446 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006447#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006448 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006449#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006450 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006451 * it in-place from the input buffer instead of making a copy. */
6452 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6453#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006454 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006455 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006456 case 0: /*ok*/
6457 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6458 /* Ignore certificate with an unknown algorithm: maybe a
6459 prior certificate was already trusted. */
6460 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006461
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006462 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6463 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6464 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006465
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006466 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6467 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6468 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006469
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006470 default:
6471 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6472 crt_parse_der_failed:
6473 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6474 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6475 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006476 }
6477
6478 i += n;
6479 }
6480
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006481 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006482 return( 0 );
6483}
6484
Hanno Becker4a55f632019-02-05 12:49:06 +00006485#if defined(MBEDTLS_SSL_SRV_C)
6486static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6487{
6488 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6489 return( -1 );
6490
6491#if defined(MBEDTLS_SSL_PROTO_SSL3)
6492 /*
6493 * Check if the client sent an empty certificate
6494 */
6495 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6496 {
6497 if( ssl->in_msglen == 2 &&
6498 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6499 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6500 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6501 {
6502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6503 return( 0 );
6504 }
6505
6506 return( -1 );
6507 }
6508#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6509
6510#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6511 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6512 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6513 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6514 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6515 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6516 {
6517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6518 return( 0 );
6519 }
6520
6521 return( -1 );
6522#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6523 MBEDTLS_SSL_PROTO_TLS1_2 */
6524}
6525#endif /* MBEDTLS_SSL_SRV_C */
6526
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006527/* Check if a certificate message is expected.
6528 * Return either
6529 * - SSL_CERTIFICATE_EXPECTED, or
6530 * - SSL_CERTIFICATE_SKIP
6531 * indicating whether a Certificate message is expected or not.
6532 */
6533#define SSL_CERTIFICATE_EXPECTED 0
6534#define SSL_CERTIFICATE_SKIP 1
6535static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6536 int authmode )
6537{
6538 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006539 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006540
6541 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6542 return( SSL_CERTIFICATE_SKIP );
6543
6544#if defined(MBEDTLS_SSL_SRV_C)
6545 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6546 {
6547 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6548 return( SSL_CERTIFICATE_SKIP );
6549
6550 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6551 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006552 ssl->session_negotiate->verify_result =
6553 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6554 return( SSL_CERTIFICATE_SKIP );
6555 }
6556 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006557#else
6558 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006559#endif /* MBEDTLS_SSL_SRV_C */
6560
6561 return( SSL_CERTIFICATE_EXPECTED );
6562}
6563
Hanno Becker68636192019-02-05 14:36:34 +00006564static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6565 int authmode,
6566 mbedtls_x509_crt *chain,
6567 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006568{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006569 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006570 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006571 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006572 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006573
Hanno Becker8927c832019-04-03 12:52:50 +01006574 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6575 void *p_vrfy;
6576
Hanno Becker68636192019-02-05 14:36:34 +00006577 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6578 return( 0 );
6579
Hanno Becker8927c832019-04-03 12:52:50 +01006580 if( ssl->f_vrfy != NULL )
6581 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006582 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006583 f_vrfy = ssl->f_vrfy;
6584 p_vrfy = ssl->p_vrfy;
6585 }
6586 else
6587 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006588 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006589 f_vrfy = ssl->conf->f_vrfy;
6590 p_vrfy = ssl->conf->p_vrfy;
6591 }
6592
Hanno Becker68636192019-02-05 14:36:34 +00006593 /*
6594 * Main check: verify certificate
6595 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006596#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6597 if( ssl->conf->f_ca_cb != NULL )
6598 {
6599 ((void) rs_ctx);
6600 have_ca_chain = 1;
6601
6602 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006603 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006604 chain,
6605 ssl->conf->f_ca_cb,
6606 ssl->conf->p_ca_cb,
6607 ssl->conf->cert_profile,
6608 ssl->hostname,
6609 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006610 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006611 }
6612 else
6613#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6614 {
6615 mbedtls_x509_crt *ca_chain;
6616 mbedtls_x509_crl *ca_crl;
6617
6618#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6619 if( ssl->handshake->sni_ca_chain != NULL )
6620 {
6621 ca_chain = ssl->handshake->sni_ca_chain;
6622 ca_crl = ssl->handshake->sni_ca_crl;
6623 }
6624 else
6625#endif
6626 {
6627 ca_chain = ssl->conf->ca_chain;
6628 ca_crl = ssl->conf->ca_crl;
6629 }
6630
6631 if( ca_chain != NULL )
6632 have_ca_chain = 1;
6633
6634 ret = mbedtls_x509_crt_verify_restartable(
6635 chain,
6636 ca_chain, ca_crl,
6637 ssl->conf->cert_profile,
6638 ssl->hostname,
6639 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006640 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006641 }
Hanno Becker68636192019-02-05 14:36:34 +00006642
6643 if( ret != 0 )
6644 {
6645 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6646 }
6647
6648#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6649 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6650 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6651#endif
6652
6653 /*
6654 * Secondary checks: always done, but change 'ret' only if it was 0
6655 */
6656
6657#if defined(MBEDTLS_ECP_C)
6658 {
6659 const mbedtls_pk_context *pk = &chain->pk;
6660
6661 /* If certificate uses an EC key, make sure the curve is OK */
6662 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6663 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6664 {
6665 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6666
6667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6668 if( ret == 0 )
6669 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6670 }
6671 }
6672#endif /* MBEDTLS_ECP_C */
6673
6674 if( mbedtls_ssl_check_cert_usage( chain,
6675 ciphersuite_info,
6676 ! ssl->conf->endpoint,
6677 &ssl->session_negotiate->verify_result ) != 0 )
6678 {
6679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6680 if( ret == 0 )
6681 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6682 }
6683
6684 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6685 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6686 * with details encoded in the verification flags. All other kinds
6687 * of error codes, including those from the user provided f_vrfy
6688 * functions, are treated as fatal and lead to a failure of
6689 * ssl_parse_certificate even if verification was optional. */
6690 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6691 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6692 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6693 {
6694 ret = 0;
6695 }
6696
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006697 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006698 {
6699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6700 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6701 }
6702
6703 if( ret != 0 )
6704 {
6705 uint8_t alert;
6706
6707 /* The certificate may have been rejected for several reasons.
6708 Pick one and send the corresponding alert. Which alert to send
6709 may be a subject of debate in some cases. */
6710 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6711 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6712 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6713 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6714 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6715 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6716 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6717 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6718 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6719 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6720 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6721 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6722 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6723 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6724 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6725 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6726 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6727 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6728 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6729 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6730 else
6731 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6732 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6733 alert );
6734 }
6735
6736#if defined(MBEDTLS_DEBUG_C)
6737 if( ssl->session_negotiate->verify_result != 0 )
6738 {
6739 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6740 ssl->session_negotiate->verify_result ) );
6741 }
6742 else
6743 {
6744 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6745 }
6746#endif /* MBEDTLS_DEBUG_C */
6747
6748 return( ret );
6749}
6750
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006751#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6752static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6753 unsigned char *start, size_t len )
6754{
6755 int ret;
6756 /* Remember digest of the peer's end-CRT. */
6757 ssl->session_negotiate->peer_cert_digest =
6758 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6759 if( ssl->session_negotiate->peer_cert_digest == NULL )
6760 {
6761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6762 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6763 mbedtls_ssl_send_alert_message( ssl,
6764 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6765 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6766
6767 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6768 }
6769
6770 ret = mbedtls_md( mbedtls_md_info_from_type(
6771 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6772 start, len,
6773 ssl->session_negotiate->peer_cert_digest );
6774
6775 ssl->session_negotiate->peer_cert_digest_type =
6776 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6777 ssl->session_negotiate->peer_cert_digest_len =
6778 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6779
6780 return( ret );
6781}
6782
6783static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6784 unsigned char *start, size_t len )
6785{
6786 unsigned char *end = start + len;
6787 int ret;
6788
6789 /* Make a copy of the peer's raw public key. */
6790 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6791 ret = mbedtls_pk_parse_subpubkey( &start, end,
6792 &ssl->handshake->peer_pubkey );
6793 if( ret != 0 )
6794 {
6795 /* We should have parsed the public key before. */
6796 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6797 }
6798
6799 return( 0 );
6800}
6801#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6802
Hanno Becker68636192019-02-05 14:36:34 +00006803int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6804{
6805 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006806 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006807#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6808 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6809 ? ssl->handshake->sni_authmode
6810 : ssl->conf->authmode;
6811#else
6812 const int authmode = ssl->conf->authmode;
6813#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006814 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006815 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006816
6817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6818
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006819 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6820 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006821 {
6822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006823 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006824 }
6825
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006826#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6827 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006828 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006829 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006830 chain = ssl->handshake->ecrs_peer_cert;
6831 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006832 goto crt_verify;
6833 }
6834#endif
6835
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006836 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006837 {
6838 /* mbedtls_ssl_read_record may have sent an alert already. We
6839 let it decide whether to alert. */
6840 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006841 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006842 }
6843
Hanno Becker4a55f632019-02-05 12:49:06 +00006844#if defined(MBEDTLS_SSL_SRV_C)
6845 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6846 {
6847 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006848
6849 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006850 ret = 0;
6851 else
6852 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006853
Hanno Becker6bdfab22019-02-05 13:11:17 +00006854 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006855 }
6856#endif /* MBEDTLS_SSL_SRV_C */
6857
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006858 /* Clear existing peer CRT structure in case we tried to
6859 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006860 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006861
6862 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6863 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006864 {
6865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6866 sizeof( mbedtls_x509_crt ) ) );
6867 mbedtls_ssl_send_alert_message( ssl,
6868 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6869 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006870
Hanno Becker3dad3112019-02-05 17:19:52 +00006871 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6872 goto exit;
6873 }
6874 mbedtls_x509_crt_init( chain );
6875
6876 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006877 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006878 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006879
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006880#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6881 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006882 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006883
6884crt_verify:
6885 if( ssl->handshake->ecrs_enabled)
6886 rs_ctx = &ssl->handshake->ecrs_ctx;
6887#endif
6888
Hanno Becker68636192019-02-05 14:36:34 +00006889 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006890 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006891 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006892 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006893
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006894#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006895 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006896 unsigned char *crt_start, *pk_start;
6897 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006898
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006899 /* We parse the CRT chain without copying, so
6900 * these pointers point into the input buffer,
6901 * and are hence still valid after freeing the
6902 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006903
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006904 crt_start = chain->raw.p;
6905 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006906
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006907 pk_start = chain->pk_raw.p;
6908 pk_len = chain->pk_raw.len;
6909
6910 /* Free the CRT structures before computing
6911 * digest and copying the peer's public key. */
6912 mbedtls_x509_crt_free( chain );
6913 mbedtls_free( chain );
6914 chain = NULL;
6915
6916 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006917 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006918 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006919
6920 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6921 if( ret != 0 )
6922 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006923 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006924#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6925 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006926 ssl->session_negotiate->peer_cert = chain;
6927 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006928#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006931
Hanno Becker6bdfab22019-02-05 13:11:17 +00006932exit:
6933
Hanno Becker3dad3112019-02-05 17:19:52 +00006934 if( ret == 0 )
6935 ssl->state++;
6936
6937#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6938 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6939 {
6940 ssl->handshake->ecrs_peer_cert = chain;
6941 chain = NULL;
6942 }
6943#endif
6944
6945 if( chain != NULL )
6946 {
6947 mbedtls_x509_crt_free( chain );
6948 mbedtls_free( chain );
6949 }
6950
Paul Bakker5121ce52009-01-03 21:22:43 +00006951 return( ret );
6952}
Hanno Becker21489932019-02-05 13:20:55 +00006953#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006956{
6957 int ret;
6958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006962 ssl->out_msglen = 1;
6963 ssl->out_msg[0] = 1;
6964
Paul Bakker5121ce52009-01-03 21:22:43 +00006965 ssl->state++;
6966
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006967 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006968 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006969 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006970 return( ret );
6971 }
6972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006974
6975 return( 0 );
6976}
6977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006979{
6980 int ret;
6981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006983
Hanno Becker327c93b2018-08-15 13:56:18 +01006984 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006987 return( ret );
6988 }
6989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006990 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006993 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6994 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006996 }
6997
Hanno Beckere678eaa2018-08-21 14:57:46 +01006998 /* CCS records are only accepted if they have length 1 and content '1',
6999 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007000
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007001 /*
7002 * Switch to our negotiated transform and session parameters for inbound
7003 * data.
7004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007005 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007006 ssl->transform_in = ssl->transform_negotiate;
7007 ssl->session_in = ssl->session_negotiate;
7008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007009#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007010 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007012#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007013 ssl_dtls_replay_reset( ssl );
7014#endif
7015
7016 /* Increment epoch */
7017 if( ++ssl->in_epoch == 0 )
7018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007020 /* This is highly unlikely to happen for legitimate reasons, so
7021 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007022 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007023 }
7024 }
7025 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007026#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007027 memset( ssl->in_ctr, 0, 8 );
7028
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007029 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7032 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007034 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007037 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7038 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007039 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007040 }
7041 }
7042#endif
7043
Paul Bakker5121ce52009-01-03 21:22:43 +00007044 ssl->state++;
7045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007047
7048 return( 0 );
7049}
7050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007051void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7052 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007053{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007054 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007056#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7057 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7058 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007059 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007060 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007061#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7063#if defined(MBEDTLS_SHA512_C)
7064 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007065 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7066 else
7067#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068#if defined(MBEDTLS_SHA256_C)
7069 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007070 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007071 else
7072#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007073#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007076 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007077 }
Paul Bakker380da532012-04-18 16:10:25 +00007078}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007080void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007081{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7083 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007084 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7085 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007086#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007087#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7088#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007089#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007090 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007091 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7092#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007093 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007094#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007095#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007097#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007098 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007099 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007100#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007101 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007102#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007103#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007105}
7106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007108 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007109{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007110#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7111 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007112 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7113 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007114#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7116#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007117#if defined(MBEDTLS_USE_PSA_CRYPTO)
7118 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7119#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007120 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007121#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007122#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007123#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007124#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007125 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007126#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007127 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007128#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007129#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007131}
7132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7134 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7135static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007136 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007137{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007138 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7139 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007140}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007141#endif
Paul Bakker380da532012-04-18 16:10:25 +00007142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007143#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7144#if defined(MBEDTLS_SHA256_C)
7145static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007146 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007147{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007148#if defined(MBEDTLS_USE_PSA_CRYPTO)
7149 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7150#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007151 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007152#endif
Paul Bakker380da532012-04-18 16:10:25 +00007153}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007154#endif
Paul Bakker380da532012-04-18 16:10:25 +00007155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007156#if defined(MBEDTLS_SHA512_C)
7157static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007158 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007159{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007160#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007161 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007162#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007163 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007164#endif
Paul Bakker380da532012-04-18 16:10:25 +00007165}
Paul Bakker769075d2012-11-24 11:26:46 +01007166#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007167#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007170static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007171 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007172{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007173 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007174 mbedtls_md5_context md5;
7175 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007176
Paul Bakker5121ce52009-01-03 21:22:43 +00007177 unsigned char padbuf[48];
7178 unsigned char md5sum[16];
7179 unsigned char sha1sum[20];
7180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007181 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007182 if( !session )
7183 session = ssl->session;
7184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007186
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007187 mbedtls_md5_init( &md5 );
7188 mbedtls_sha1_init( &sha1 );
7189
7190 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7191 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007192
7193 /*
7194 * SSLv3:
7195 * hash =
7196 * MD5( master + pad2 +
7197 * MD5( handshake + sender + master + pad1 ) )
7198 * + SHA1( master + pad2 +
7199 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007200 */
7201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007203 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7204 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007205#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007208 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7209 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007210#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007213 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007214
Paul Bakker1ef83d62012-04-11 12:09:53 +00007215 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007216
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007217 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7218 mbedtls_md5_update_ret( &md5, session->master, 48 );
7219 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7220 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007221
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007222 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7223 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7224 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7225 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007226
Paul Bakker1ef83d62012-04-11 12:09:53 +00007227 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007228
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007229 mbedtls_md5_starts_ret( &md5 );
7230 mbedtls_md5_update_ret( &md5, session->master, 48 );
7231 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7232 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7233 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007234
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007235 mbedtls_sha1_starts_ret( &sha1 );
7236 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7237 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7238 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7239 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007242
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007243 mbedtls_md5_free( &md5 );
7244 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007245
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007246 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7247 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7248 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007251}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007254#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007255static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007256 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007257{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007258 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007259 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007260 mbedtls_md5_context md5;
7261 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007262 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007265 if( !session )
7266 session = ssl->session;
7267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007269
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007270 mbedtls_md5_init( &md5 );
7271 mbedtls_sha1_init( &sha1 );
7272
7273 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7274 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007275
Paul Bakker1ef83d62012-04-11 12:09:53 +00007276 /*
7277 * TLSv1:
7278 * hash = PRF( master, finished_label,
7279 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7280 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007283 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7284 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007285#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007288 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7289 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007290#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007292 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007293 ? "client finished"
7294 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007295
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007296 mbedtls_md5_finish_ret( &md5, padbuf );
7297 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007298
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007299 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007300 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007303
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007304 mbedtls_md5_free( &md5 );
7305 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007306
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007307 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007310}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7314#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007315static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007317{
7318 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007319 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007320 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007321#if defined(MBEDTLS_USE_PSA_CRYPTO)
7322 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007323 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007324 psa_status_t status;
7325#else
7326 mbedtls_sha256_context sha256;
7327#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007329 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007330 if( !session )
7331 session = ssl->session;
7332
Andrzej Kurekeb342242019-01-29 09:14:33 -05007333 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7334 ? "client finished"
7335 : "server finished";
7336
7337#if defined(MBEDTLS_USE_PSA_CRYPTO)
7338 sha256_psa = psa_hash_operation_init();
7339
7340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7341
7342 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7343 if( status != PSA_SUCCESS )
7344 {
7345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7346 return;
7347 }
7348
7349 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7350 if( status != PSA_SUCCESS )
7351 {
7352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7353 return;
7354 }
7355 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7356#else
7357
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007358 mbedtls_sha256_init( &sha256 );
7359
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007361
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007362 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007363
7364 /*
7365 * TLSv1.2:
7366 * hash = PRF( master, finished_label,
7367 * Hash( handshake ) )[0.11]
7368 */
7369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370#if !defined(MBEDTLS_SHA256_ALT)
7371 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007372 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007373#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007374
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007375 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007376 mbedtls_sha256_free( &sha256 );
7377#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007378
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007379 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007380 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007383
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007384 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007387}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007391static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007393{
7394 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007395 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007396 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007397#if defined(MBEDTLS_USE_PSA_CRYPTO)
7398 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007399 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007400 psa_status_t status;
7401#else
7402 mbedtls_sha512_context sha512;
7403#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007406 if( !session )
7407 session = ssl->session;
7408
Andrzej Kurekeb342242019-01-29 09:14:33 -05007409 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7410 ? "client finished"
7411 : "server finished";
7412
7413#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007414 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007415
7416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7417
Andrzej Kurek972fba52019-01-30 03:29:12 -05007418 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007419 if( status != PSA_SUCCESS )
7420 {
7421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7422 return;
7423 }
7424
Andrzej Kurek972fba52019-01-30 03:29:12 -05007425 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007426 if( status != PSA_SUCCESS )
7427 {
7428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7429 return;
7430 }
7431 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7432#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007433 mbedtls_sha512_init( &sha512 );
7434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007436
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007437 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007438
7439 /*
7440 * TLSv1.2:
7441 * hash = PRF( master, finished_label,
7442 * Hash( handshake ) )[0.11]
7443 */
7444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007446 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7447 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007448#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007449
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007450 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007451 mbedtls_sha512_free( &sha512 );
7452#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007453
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007454 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007455 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007458
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007459 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007462}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463#endif /* MBEDTLS_SHA512_C */
7464#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007467{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007469
7470 /*
7471 * Free our handshake params
7472 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007473 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007475 ssl->handshake = NULL;
7476
7477 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007478 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007479 */
7480 if( ssl->transform )
7481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482 mbedtls_ssl_transform_free( ssl->transform );
7483 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007484 }
7485 ssl->transform = ssl->transform_negotiate;
7486 ssl->transform_negotiate = NULL;
7487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007488 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007489}
7490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007492{
7493 int resume = ssl->handshake->resume;
7494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007497#if defined(MBEDTLS_SSL_RENEGOTIATION)
7498 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007500 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007501 ssl->renego_records_seen = 0;
7502 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007503#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007504
7505 /*
7506 * Free the previous session and switch in the current one
7507 */
Paul Bakker0a597072012-09-25 21:55:46 +00007508 if( ssl->session )
7509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007511 /* RFC 7366 3.1: keep the EtM state */
7512 ssl->session_negotiate->encrypt_then_mac =
7513 ssl->session->encrypt_then_mac;
7514#endif
7515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007516 mbedtls_ssl_session_free( ssl->session );
7517 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007518 }
7519 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007520 ssl->session_negotiate = NULL;
7521
Paul Bakker0a597072012-09-25 21:55:46 +00007522 /*
7523 * Add cache entry
7524 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007525 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007526 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007527 resume == 0 )
7528 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007529 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007531 }
Paul Bakker0a597072012-09-25 21:55:46 +00007532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007534 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007535 ssl->handshake->flight != NULL )
7536 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007537 /* Cancel handshake timer */
7538 ssl_set_timer( ssl, 0 );
7539
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007540 /* Keep last flight around in case we need to resend it:
7541 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007542 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007543 }
7544 else
7545#endif
7546 ssl_handshake_wrapup_free_hs_transform( ssl );
7547
Paul Bakker48916f92012-09-16 19:57:18 +00007548 ssl->state++;
7549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007551}
7552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007554{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007555 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007558
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007559 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007560
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007561 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007562
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007563 /*
7564 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7565 * may define some other value. Currently (early 2016), no defined
7566 * ciphersuite does this (and this is unlikely to change as activity has
7567 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007571#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007572 ssl->verify_data_len = hash_len;
7573 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007574#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007575
Paul Bakker5121ce52009-01-03 21:22:43 +00007576 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7578 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007579
7580 /*
7581 * In case of session resuming, invert the client and server
7582 * ChangeCipherSpec messages order.
7583 */
Paul Bakker0a597072012-09-25 21:55:46 +00007584 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007587 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007588 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007589#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007591 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007593#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007594 }
7595 else
7596 ssl->state++;
7597
Paul Bakker48916f92012-09-16 19:57:18 +00007598 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007599 * Switch to our negotiated transform and session parameters for outbound
7600 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007605 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007606 {
7607 unsigned char i;
7608
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007609 /* Remember current epoch settings for resending */
7610 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007611 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007612
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007613 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007614 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007615
7616 /* Increment epoch */
7617 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007618 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007619 break;
7620
7621 /* The loop goes to its end iff the counter is wrapping */
7622 if( i == 0 )
7623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7625 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007626 }
7627 }
7628 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007630 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007631
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007632 ssl->transform_out = ssl->transform_negotiate;
7633 ssl->session_out = ssl->session_negotiate;
7634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007635#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7636 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7641 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007642 }
7643 }
7644#endif
7645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007646#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007647 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007649#endif
7650
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007651 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007652 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007653 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007654 return( ret );
7655 }
7656
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007657#if defined(MBEDTLS_SSL_PROTO_DTLS)
7658 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7659 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7660 {
7661 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7662 return( ret );
7663 }
7664#endif
7665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007667
7668 return( 0 );
7669}
7670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007671#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007672#define SSL_MAX_HASH_LEN 36
7673#else
7674#define SSL_MAX_HASH_LEN 12
7675#endif
7676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007677int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007678{
Paul Bakker23986e52011-04-24 08:57:21 +00007679 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007680 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007681 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007684
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007685 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007686
Hanno Becker327c93b2018-08-15 13:56:18 +01007687 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007690 return( ret );
7691 }
7692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007696 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7697 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007698 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007699 }
7700
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007701 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702#if defined(MBEDTLS_SSL_PROTO_SSL3)
7703 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007704 hash_len = 36;
7705 else
7706#endif
7707 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7710 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007713 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7714 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007715 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007716 }
7717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007718 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007719 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007722 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7723 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007725 }
7726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007728 ssl->verify_data_len = hash_len;
7729 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007730#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007731
Paul Bakker0a597072012-09-25 21:55:46 +00007732 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007735 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007737#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007739 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007741#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007742 }
7743 else
7744 ssl->state++;
7745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007746#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007747 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007748 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007749#endif
7750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007752
7753 return( 0 );
7754}
7755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007756static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007757{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7761 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7762 mbedtls_md5_init( &handshake->fin_md5 );
7763 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007764 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7765 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007766#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7768#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007769#if defined(MBEDTLS_USE_PSA_CRYPTO)
7770 handshake->fin_sha256_psa = psa_hash_operation_init();
7771 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7772#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007773 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007774 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007775#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007776#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007778#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007779 handshake->fin_sha384_psa = psa_hash_operation_init();
7780 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007781#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007782 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007783 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007784#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007785#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007786#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007787
7788 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007789
7790#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7791 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7792 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7793#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007795#if defined(MBEDTLS_DHM_C)
7796 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007797#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007798#if defined(MBEDTLS_ECDH_C)
7799 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007800#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007801#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007802 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007803#if defined(MBEDTLS_SSL_CLI_C)
7804 handshake->ecjpake_cache = NULL;
7805 handshake->ecjpake_cache_len = 0;
7806#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007807#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007808
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007809#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007810 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007811#endif
7812
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007813#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7814 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7815#endif
Hanno Becker75173122019-02-06 16:18:31 +00007816
7817#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7818 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7819 mbedtls_pk_init( &handshake->peer_pubkey );
7820#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007821}
7822
Hanno Beckera18d1322018-01-03 14:27:32 +00007823void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007824{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007825 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007827 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7828 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007829
Hanno Beckerd56ed242018-01-03 15:32:51 +00007830#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831 mbedtls_md_init( &transform->md_ctx_enc );
7832 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007833#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007834}
7835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007836void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007837{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007839}
7840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007842{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007843 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007844 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007846 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007848 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007849 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007850
7851 /*
7852 * Either the pointers are now NULL or cleared properly and can be freed.
7853 * Now allocate missing structures.
7854 */
7855 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007856 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007857 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007858 }
Paul Bakker48916f92012-09-16 19:57:18 +00007859
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007860 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007861 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007862 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007863 }
Paul Bakker48916f92012-09-16 19:57:18 +00007864
Paul Bakker82788fb2014-10-20 13:59:19 +02007865 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007866 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007867 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007868 }
Paul Bakker48916f92012-09-16 19:57:18 +00007869
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007870 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007871 if( ssl->handshake == NULL ||
7872 ssl->transform_negotiate == NULL ||
7873 ssl->session_negotiate == NULL )
7874 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877 mbedtls_free( ssl->handshake );
7878 mbedtls_free( ssl->transform_negotiate );
7879 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007880
7881 ssl->handshake = NULL;
7882 ssl->transform_negotiate = NULL;
7883 ssl->session_negotiate = NULL;
7884
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007885 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007886 }
7887
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007888 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007890 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007891 ssl_handshake_params_init( ssl->handshake );
7892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007894 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7895 {
7896 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007897
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007898 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7899 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7900 else
7901 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007902
7903 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007904 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007905#endif
7906
Paul Bakker48916f92012-09-16 19:57:18 +00007907 return( 0 );
7908}
7909
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007910#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007911/* Dummy cookie callbacks for defaults */
7912static int ssl_cookie_write_dummy( void *ctx,
7913 unsigned char **p, unsigned char *end,
7914 const unsigned char *cli_id, size_t cli_id_len )
7915{
7916 ((void) ctx);
7917 ((void) p);
7918 ((void) end);
7919 ((void) cli_id);
7920 ((void) cli_id_len);
7921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007923}
7924
7925static int ssl_cookie_check_dummy( void *ctx,
7926 const unsigned char *cookie, size_t cookie_len,
7927 const unsigned char *cli_id, size_t cli_id_len )
7928{
7929 ((void) ctx);
7930 ((void) cookie);
7931 ((void) cookie_len);
7932 ((void) cli_id);
7933 ((void) cli_id_len);
7934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007936}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007937#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007938
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007939/* Once ssl->out_hdr as the address of the beginning of the
7940 * next outgoing record is set, deduce the other pointers.
7941 *
7942 * Note: For TLS, we save the implicit record sequence number
7943 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7944 * and the caller has to make sure there's space for this.
7945 */
7946
7947static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7948 mbedtls_ssl_transform *transform )
7949{
7950#if defined(MBEDTLS_SSL_PROTO_DTLS)
7951 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7952 {
7953 ssl->out_ctr = ssl->out_hdr + 3;
7954 ssl->out_len = ssl->out_hdr + 11;
7955 ssl->out_iv = ssl->out_hdr + 13;
7956 }
7957 else
7958#endif
7959 {
7960 ssl->out_ctr = ssl->out_hdr - 8;
7961 ssl->out_len = ssl->out_hdr + 3;
7962 ssl->out_iv = ssl->out_hdr + 5;
7963 }
7964
7965 /* Adjust out_msg to make space for explicit IV, if used. */
7966 if( transform != NULL &&
7967 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7968 {
7969 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7970 }
7971 else
7972 ssl->out_msg = ssl->out_iv;
7973}
7974
7975/* Once ssl->in_hdr as the address of the beginning of the
7976 * next incoming record is set, deduce the other pointers.
7977 *
7978 * Note: For TLS, we save the implicit record sequence number
7979 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7980 * and the caller has to make sure there's space for this.
7981 */
7982
7983static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7984 mbedtls_ssl_transform *transform )
7985{
7986#if defined(MBEDTLS_SSL_PROTO_DTLS)
7987 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7988 {
7989 ssl->in_ctr = ssl->in_hdr + 3;
7990 ssl->in_len = ssl->in_hdr + 11;
7991 ssl->in_iv = ssl->in_hdr + 13;
7992 }
7993 else
7994#endif
7995 {
7996 ssl->in_ctr = ssl->in_hdr - 8;
7997 ssl->in_len = ssl->in_hdr + 3;
7998 ssl->in_iv = ssl->in_hdr + 5;
7999 }
8000
8001 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
8002 if( transform != NULL &&
8003 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8004 {
8005 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
8006 }
8007 else
8008 ssl->in_msg = ssl->in_iv;
8009}
8010
Paul Bakker5121ce52009-01-03 21:22:43 +00008011/*
8012 * Initialize an SSL context
8013 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008014void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8015{
8016 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8017}
8018
8019/*
8020 * Setup an SSL context
8021 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008022
8023static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8024{
8025 /* Set the incoming and outgoing record pointers. */
8026#if defined(MBEDTLS_SSL_PROTO_DTLS)
8027 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8028 {
8029 ssl->out_hdr = ssl->out_buf;
8030 ssl->in_hdr = ssl->in_buf;
8031 }
8032 else
8033#endif /* MBEDTLS_SSL_PROTO_DTLS */
8034 {
8035 ssl->out_hdr = ssl->out_buf + 8;
8036 ssl->in_hdr = ssl->in_buf + 8;
8037 }
8038
8039 /* Derive other internal pointers. */
8040 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
8041 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
8042}
8043
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008044int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008045 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008046{
Paul Bakker48916f92012-09-16 19:57:18 +00008047 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008048
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008049 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008050
8051 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008052 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008053 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008054
8055 /* Set to NULL in case of an error condition */
8056 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008057
Angus Grattond8213d02016-05-25 20:56:48 +10008058 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8059 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008060 {
Angus Grattond8213d02016-05-25 20:56:48 +10008061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008062 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008063 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008064 }
8065
8066 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8067 if( ssl->out_buf == NULL )
8068 {
8069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008070 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008071 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008072 }
8073
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008074 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008075
Paul Bakker48916f92012-09-16 19:57:18 +00008076 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008077 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008078
8079 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008080
8081error:
8082 mbedtls_free( ssl->in_buf );
8083 mbedtls_free( ssl->out_buf );
8084
8085 ssl->conf = NULL;
8086
8087 ssl->in_buf = NULL;
8088 ssl->out_buf = NULL;
8089
8090 ssl->in_hdr = NULL;
8091 ssl->in_ctr = NULL;
8092 ssl->in_len = NULL;
8093 ssl->in_iv = NULL;
8094 ssl->in_msg = NULL;
8095
8096 ssl->out_hdr = NULL;
8097 ssl->out_ctr = NULL;
8098 ssl->out_len = NULL;
8099 ssl->out_iv = NULL;
8100 ssl->out_msg = NULL;
8101
k-stachowiak9f7798e2018-07-31 16:52:32 +02008102 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008103}
8104
8105/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008106 * Reset an initialized and used SSL context for re-use while retaining
8107 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008108 *
8109 * If partial is non-zero, keep data in the input buffer and client ID.
8110 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008111 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008112static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008113{
Paul Bakker48916f92012-09-16 19:57:18 +00008114 int ret;
8115
Hanno Becker7e772132018-08-10 12:38:21 +01008116#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8117 !defined(MBEDTLS_SSL_SRV_C)
8118 ((void) partial);
8119#endif
8120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008121 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008122
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008123 /* Cancel any possibly running timer */
8124 ssl_set_timer( ssl, 0 );
8125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008126#if defined(MBEDTLS_SSL_RENEGOTIATION)
8127 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008128 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008129
8130 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008131 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8132 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008133#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008134 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008135
Paul Bakker7eb013f2011-10-06 12:37:39 +00008136 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008137 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008138
8139 ssl->in_msgtype = 0;
8140 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008142 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008143 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008144#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008146 ssl_dtls_replay_reset( ssl );
8147#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008148
8149 ssl->in_hslen = 0;
8150 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008151
8152 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008153
8154 ssl->out_msgtype = 0;
8155 ssl->out_msglen = 0;
8156 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008157#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8158 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008159 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008160#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008161
Hanno Becker19859472018-08-06 09:40:20 +01008162 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8163
Paul Bakker48916f92012-09-16 19:57:18 +00008164 ssl->transform_in = NULL;
8165 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008166
Hanno Becker78640902018-08-13 16:35:15 +01008167 ssl->session_in = NULL;
8168 ssl->session_out = NULL;
8169
Angus Grattond8213d02016-05-25 20:56:48 +10008170 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008171
8172#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008173 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008174#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8175 {
8176 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008177 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008178 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008180#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8181 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8184 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8187 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008188 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008189 }
8190#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008191
Paul Bakker48916f92012-09-16 19:57:18 +00008192 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194 mbedtls_ssl_transform_free( ssl->transform );
8195 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008196 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008197 }
Paul Bakker48916f92012-09-16 19:57:18 +00008198
Paul Bakkerc0463502013-02-14 11:19:38 +01008199 if( ssl->session )
8200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201 mbedtls_ssl_session_free( ssl->session );
8202 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008203 ssl->session = NULL;
8204 }
8205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008206#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008207 ssl->alpn_chosen = NULL;
8208#endif
8209
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008210#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008211#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008212 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008213#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008214 {
8215 mbedtls_free( ssl->cli_id );
8216 ssl->cli_id = NULL;
8217 ssl->cli_id_len = 0;
8218 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008219#endif
8220
Paul Bakker48916f92012-09-16 19:57:18 +00008221 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8222 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008223
8224 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008225}
8226
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008227/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008228 * Reset an initialized and used SSL context for re-use while retaining
8229 * all application-set variables, function pointers and data.
8230 */
8231int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8232{
8233 return( ssl_session_reset_int( ssl, 0 ) );
8234}
8235
8236/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008237 * SSL set accessors
8238 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008239void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008240{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008241 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008242}
8243
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008244void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008245{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008246 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008247}
8248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008249#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008250void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008251{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008252 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008253}
8254#endif
8255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008256#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008257void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008258{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008259 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008260}
8261#endif
8262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008263#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008264
Hanno Becker1841b0a2018-08-24 11:13:57 +01008265void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8266 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008267{
8268 ssl->disable_datagram_packing = !allow_packing;
8269}
8270
8271void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8272 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008273{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008274 conf->hs_timeout_min = min;
8275 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008276}
8277#endif
8278
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008279void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008280{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008281 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008282}
8283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008284#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008285void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008286 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008287 void *p_vrfy )
8288{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008289 conf->f_vrfy = f_vrfy;
8290 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008291}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008292#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008293
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008294void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008295 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008296 void *p_rng )
8297{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008298 conf->f_rng = f_rng;
8299 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008300}
8301
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008302void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008303 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008304 void *p_dbg )
8305{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008306 conf->f_dbg = f_dbg;
8307 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008308}
8309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008311 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008312 mbedtls_ssl_send_t *f_send,
8313 mbedtls_ssl_recv_t *f_recv,
8314 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008315{
8316 ssl->p_bio = p_bio;
8317 ssl->f_send = f_send;
8318 ssl->f_recv = f_recv;
8319 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008320}
8321
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008322#if defined(MBEDTLS_SSL_PROTO_DTLS)
8323void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8324{
8325 ssl->mtu = mtu;
8326}
8327#endif
8328
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008329void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008330{
8331 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008332}
8333
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008334void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8335 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008336 mbedtls_ssl_set_timer_t *f_set_timer,
8337 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008338{
8339 ssl->p_timer = p_timer;
8340 ssl->f_set_timer = f_set_timer;
8341 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008342
8343 /* Make sure we start with no timer running */
8344 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008345}
8346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008347#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008348void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008349 void *p_cache,
8350 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8351 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008352{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008353 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008354 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008355 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008356}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008357#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359#if defined(MBEDTLS_SSL_CLI_C)
8360int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008361{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008362 int ret;
8363
8364 if( ssl == NULL ||
8365 session == NULL ||
8366 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008367 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008369 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008370 }
8371
Hanno Becker52055ae2019-02-06 14:30:46 +00008372 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8373 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008374 return( ret );
8375
Paul Bakker0a597072012-09-25 21:55:46 +00008376 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008377
8378 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008379}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008380#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008381
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008382void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008383 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008384{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008385 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8386 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8387 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8388 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008389}
8390
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008391void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008392 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008393 int major, int minor )
8394{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008395 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008396 return;
8397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008398 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008399 return;
8400
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008401 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008402}
8403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008404#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008405void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008406 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008407{
8408 conf->cert_profile = profile;
8409}
8410
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008411/* Append a new keycert entry to a (possibly empty) list */
8412static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8413 mbedtls_x509_crt *cert,
8414 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008415{
niisato8ee24222018-06-25 19:05:48 +09008416 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008417
niisato8ee24222018-06-25 19:05:48 +09008418 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8419 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008420 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008421
niisato8ee24222018-06-25 19:05:48 +09008422 new_cert->cert = cert;
8423 new_cert->key = key;
8424 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008425
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008426 /* Update head is the list was null, else add to the end */
8427 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008428 {
niisato8ee24222018-06-25 19:05:48 +09008429 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008430 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008431 else
8432 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008433 mbedtls_ssl_key_cert *cur = *head;
8434 while( cur->next != NULL )
8435 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008436 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008437 }
8438
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008439 return( 0 );
8440}
8441
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008442int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008443 mbedtls_x509_crt *own_cert,
8444 mbedtls_pk_context *pk_key )
8445{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008446 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008447}
8448
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008449void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008450 mbedtls_x509_crt *ca_chain,
8451 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008452{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008453 conf->ca_chain = ca_chain;
8454 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008455
8456#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8457 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8458 * cannot be used together. */
8459 conf->f_ca_cb = NULL;
8460 conf->p_ca_cb = NULL;
8461#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008462}
Hanno Becker5adaad92019-03-27 16:54:37 +00008463
8464#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8465void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008466 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008467 void *p_ca_cb )
8468{
8469 conf->f_ca_cb = f_ca_cb;
8470 conf->p_ca_cb = p_ca_cb;
8471
8472 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8473 * cannot be used together. */
8474 conf->ca_chain = NULL;
8475 conf->ca_crl = NULL;
8476}
8477#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008478#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008479
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008480#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8481int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8482 mbedtls_x509_crt *own_cert,
8483 mbedtls_pk_context *pk_key )
8484{
8485 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8486 own_cert, pk_key ) );
8487}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008488
8489void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8490 mbedtls_x509_crt *ca_chain,
8491 mbedtls_x509_crl *ca_crl )
8492{
8493 ssl->handshake->sni_ca_chain = ca_chain;
8494 ssl->handshake->sni_ca_crl = ca_crl;
8495}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008496
8497void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8498 int authmode )
8499{
8500 ssl->handshake->sni_authmode = authmode;
8501}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008502#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8503
Hanno Becker8927c832019-04-03 12:52:50 +01008504#if defined(MBEDTLS_X509_CRT_PARSE_C)
8505void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8506 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8507 void *p_vrfy )
8508{
8509 ssl->f_vrfy = f_vrfy;
8510 ssl->p_vrfy = p_vrfy;
8511}
8512#endif
8513
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008514#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008515/*
8516 * Set EC J-PAKE password for current handshake
8517 */
8518int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8519 const unsigned char *pw,
8520 size_t pw_len )
8521{
8522 mbedtls_ecjpake_role role;
8523
Janos Follath8eb64132016-06-03 15:40:57 +01008524 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008525 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8526
8527 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8528 role = MBEDTLS_ECJPAKE_SERVER;
8529 else
8530 role = MBEDTLS_ECJPAKE_CLIENT;
8531
8532 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8533 role,
8534 MBEDTLS_MD_SHA256,
8535 MBEDTLS_ECP_DP_SECP256R1,
8536 pw, pw_len ) );
8537}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008538#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008540#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008541
8542static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8543{
8544 /* Remove reference to existing PSK, if any. */
8545#if defined(MBEDTLS_USE_PSA_CRYPTO)
8546 if( conf->psk_opaque != 0 )
8547 {
8548 /* The maintenance of the PSK key slot is the
8549 * user's responsibility. */
8550 conf->psk_opaque = 0;
8551 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008552 /* This and the following branch should never
8553 * be taken simultaenously as we maintain the
8554 * invariant that raw and opaque PSKs are never
8555 * configured simultaneously. As a safeguard,
8556 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008557#endif /* MBEDTLS_USE_PSA_CRYPTO */
8558 if( conf->psk != NULL )
8559 {
8560 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8561
8562 mbedtls_free( conf->psk );
8563 conf->psk = NULL;
8564 conf->psk_len = 0;
8565 }
8566
8567 /* Remove reference to PSK identity, if any. */
8568 if( conf->psk_identity != NULL )
8569 {
8570 mbedtls_free( conf->psk_identity );
8571 conf->psk_identity = NULL;
8572 conf->psk_identity_len = 0;
8573 }
8574}
8575
Hanno Becker7390c712018-11-15 13:33:04 +00008576/* This function assumes that PSK identity in the SSL config is unset.
8577 * It checks that the provided identity is well-formed and attempts
8578 * to make a copy of it in the SSL config.
8579 * On failure, the PSK identity in the config remains unset. */
8580static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8581 unsigned char const *psk_identity,
8582 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008583{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008584 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008585 if( psk_identity == NULL ||
8586 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008587 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008588 {
8589 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8590 }
8591
Hanno Becker7390c712018-11-15 13:33:04 +00008592 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8593 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008594 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008595
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008596 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008597 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008598
8599 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008600}
8601
Hanno Becker7390c712018-11-15 13:33:04 +00008602int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8603 const unsigned char *psk, size_t psk_len,
8604 const unsigned char *psk_identity, size_t psk_identity_len )
8605{
8606 int ret;
8607 /* Remove opaque/raw PSK + PSK Identity */
8608 ssl_conf_remove_psk( conf );
8609
8610 /* Check and set raw PSK */
8611 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8612 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8613 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8614 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8615 conf->psk_len = psk_len;
8616 memcpy( conf->psk, psk, conf->psk_len );
8617
8618 /* Check and set PSK Identity */
8619 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8620 if( ret != 0 )
8621 ssl_conf_remove_psk( conf );
8622
8623 return( ret );
8624}
8625
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008626static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8627{
8628#if defined(MBEDTLS_USE_PSA_CRYPTO)
8629 if( ssl->handshake->psk_opaque != 0 )
8630 {
8631 ssl->handshake->psk_opaque = 0;
8632 }
8633 else
8634#endif /* MBEDTLS_USE_PSA_CRYPTO */
8635 if( ssl->handshake->psk != NULL )
8636 {
8637 mbedtls_platform_zeroize( ssl->handshake->psk,
8638 ssl->handshake->psk_len );
8639 mbedtls_free( ssl->handshake->psk );
8640 ssl->handshake->psk_len = 0;
8641 }
8642}
8643
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008644int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8645 const unsigned char *psk, size_t psk_len )
8646{
8647 if( psk == NULL || ssl->handshake == NULL )
8648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8649
8650 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8652
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008653 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008654
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008655 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008656 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008657
8658 ssl->handshake->psk_len = psk_len;
8659 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8660
8661 return( 0 );
8662}
8663
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008664#if defined(MBEDTLS_USE_PSA_CRYPTO)
8665int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008666 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008667 const unsigned char *psk_identity,
8668 size_t psk_identity_len )
8669{
Hanno Becker7390c712018-11-15 13:33:04 +00008670 int ret;
8671 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008672 ssl_conf_remove_psk( conf );
8673
Hanno Becker7390c712018-11-15 13:33:04 +00008674 /* Check and set opaque PSK */
8675 if( psk_slot == 0 )
8676 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008677 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008678
8679 /* Check and set PSK Identity */
8680 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8681 psk_identity_len );
8682 if( ret != 0 )
8683 ssl_conf_remove_psk( conf );
8684
8685 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008686}
8687
8688int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008689 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008690{
8691 if( psk_slot == 0 || ssl->handshake == NULL )
8692 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8693
8694 ssl_remove_psk( ssl );
8695 ssl->handshake->psk_opaque = psk_slot;
8696 return( 0 );
8697}
8698#endif /* MBEDTLS_USE_PSA_CRYPTO */
8699
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008700void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008701 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008702 size_t),
8703 void *p_psk )
8704{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008705 conf->f_psk = f_psk;
8706 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008707}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008708#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008709
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008710#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008711
8712#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008713int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008714{
8715 int ret;
8716
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008717 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8718 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8719 {
8720 mbedtls_mpi_free( &conf->dhm_P );
8721 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008722 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008723 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008724
8725 return( 0 );
8726}
Hanno Becker470a8c42017-10-04 15:28:46 +01008727#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008728
Hanno Beckera90658f2017-10-04 15:29:08 +01008729int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8730 const unsigned char *dhm_P, size_t P_len,
8731 const unsigned char *dhm_G, size_t G_len )
8732{
8733 int ret;
8734
8735 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8736 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8737 {
8738 mbedtls_mpi_free( &conf->dhm_P );
8739 mbedtls_mpi_free( &conf->dhm_G );
8740 return( ret );
8741 }
8742
8743 return( 0 );
8744}
Paul Bakker5121ce52009-01-03 21:22:43 +00008745
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008746int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008747{
8748 int ret;
8749
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008750 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8751 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8752 {
8753 mbedtls_mpi_free( &conf->dhm_P );
8754 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008755 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008756 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008757
8758 return( 0 );
8759}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008760#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008761
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008762#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8763/*
8764 * Set the minimum length for Diffie-Hellman parameters
8765 */
8766void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8767 unsigned int bitlen )
8768{
8769 conf->dhm_min_bitlen = bitlen;
8770}
8771#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8772
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008773#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008774/*
8775 * Set allowed/preferred hashes for handshake signatures
8776 */
8777void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8778 const int *hashes )
8779{
8780 conf->sig_hashes = hashes;
8781}
Hanno Becker947194e2017-04-07 13:25:49 +01008782#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008783
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008784#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008785/*
8786 * Set the allowed elliptic curves
8787 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008788void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008789 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008790{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008791 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008792}
Hanno Becker947194e2017-04-07 13:25:49 +01008793#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008794
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008795#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008796int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008797{
Hanno Becker947194e2017-04-07 13:25:49 +01008798 /* Initialize to suppress unnecessary compiler warning */
8799 size_t hostname_len = 0;
8800
8801 /* Check if new hostname is valid before
8802 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008803 if( hostname != NULL )
8804 {
8805 hostname_len = strlen( hostname );
8806
8807 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8808 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8809 }
8810
8811 /* Now it's clear that we will overwrite the old hostname,
8812 * so we can free it safely */
8813
8814 if( ssl->hostname != NULL )
8815 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008816 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008817 mbedtls_free( ssl->hostname );
8818 }
8819
8820 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008821
Paul Bakker5121ce52009-01-03 21:22:43 +00008822 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008823 {
8824 ssl->hostname = NULL;
8825 }
8826 else
8827 {
8828 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008829 if( ssl->hostname == NULL )
8830 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008831
Hanno Becker947194e2017-04-07 13:25:49 +01008832 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008833
Hanno Becker947194e2017-04-07 13:25:49 +01008834 ssl->hostname[hostname_len] = '\0';
8835 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008836
8837 return( 0 );
8838}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008839#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008840
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008841#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008842void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008843 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008844 const unsigned char *, size_t),
8845 void *p_sni )
8846{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008847 conf->f_sni = f_sni;
8848 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008849}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008850#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008852#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008853int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008854{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008855 size_t cur_len, tot_len;
8856 const char **p;
8857
8858 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008859 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8860 * MUST NOT be truncated."
8861 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008862 */
8863 tot_len = 0;
8864 for( p = protos; *p != NULL; p++ )
8865 {
8866 cur_len = strlen( *p );
8867 tot_len += cur_len;
8868
8869 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008870 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008871 }
8872
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008873 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008874
8875 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008876}
8877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008878const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008879{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008880 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008881}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008882#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008883
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008884void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008885{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008886 conf->max_major_ver = major;
8887 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008888}
8889
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008890void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008891{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008892 conf->min_major_ver = major;
8893 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008894}
8895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008896#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008897void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008898{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008899 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008900}
8901#endif
8902
Janos Follath088ce432017-04-10 12:42:31 +01008903#if defined(MBEDTLS_SSL_SRV_C)
8904void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8905 char cert_req_ca_list )
8906{
8907 conf->cert_req_ca_list = cert_req_ca_list;
8908}
8909#endif
8910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008911#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008912void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008913{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008914 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008915}
8916#endif
8917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008918#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008919void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008920{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008921 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008922}
8923#endif
8924
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008925#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008926void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008927{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008928 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008929}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008930#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008932#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008933int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008934{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008935 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008936 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008938 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008939 }
8940
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008941 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008942
8943 return( 0 );
8944}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008945#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008947#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008948void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008949{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008950 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008951}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008952#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008954#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008955void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008956{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008957 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008958}
8959#endif
8960
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008961void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008962{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008963 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008964}
8965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008966#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008967void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008968{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008969 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008970}
8971
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008972void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008973{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008974 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008975}
8976
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008977void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008978 const unsigned char period[8] )
8979{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008980 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008981}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008982#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008984#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008985#if defined(MBEDTLS_SSL_CLI_C)
8986void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008987{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008988 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008989}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008990#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008991
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008992#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008993void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8994 mbedtls_ssl_ticket_write_t *f_ticket_write,
8995 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8996 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008997{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008998 conf->f_ticket_write = f_ticket_write;
8999 conf->f_ticket_parse = f_ticket_parse;
9000 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009001}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009002#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009003#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009004
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009005#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9006void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9007 mbedtls_ssl_export_keys_t *f_export_keys,
9008 void *p_export_keys )
9009{
9010 conf->f_export_keys = f_export_keys;
9011 conf->p_export_keys = p_export_keys;
9012}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009013
9014void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9015 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9016 void *p_export_keys )
9017{
9018 conf->f_export_keys_ext = f_export_keys_ext;
9019 conf->p_export_keys = p_export_keys;
9020}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009021#endif
9022
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009023#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009024void mbedtls_ssl_conf_async_private_cb(
9025 mbedtls_ssl_config *conf,
9026 mbedtls_ssl_async_sign_t *f_async_sign,
9027 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9028 mbedtls_ssl_async_resume_t *f_async_resume,
9029 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009030 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009031{
9032 conf->f_async_sign_start = f_async_sign;
9033 conf->f_async_decrypt_start = f_async_decrypt;
9034 conf->f_async_resume = f_async_resume;
9035 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009036 conf->p_async_config_data = async_config_data;
9037}
9038
Gilles Peskine8f97af72018-04-26 11:46:10 +02009039void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9040{
9041 return( conf->p_async_config_data );
9042}
9043
Gilles Peskine1febfef2018-04-30 11:54:39 +02009044void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009045{
9046 if( ssl->handshake == NULL )
9047 return( NULL );
9048 else
9049 return( ssl->handshake->user_async_ctx );
9050}
9051
Gilles Peskine1febfef2018-04-30 11:54:39 +02009052void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009053 void *ctx )
9054{
9055 if( ssl->handshake != NULL )
9056 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009057}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009058#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009059
Paul Bakker5121ce52009-01-03 21:22:43 +00009060/*
9061 * SSL get accessors
9062 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009063size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009064{
9065 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9066}
9067
Hanno Becker8b170a02017-10-10 11:51:19 +01009068int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9069{
9070 /*
9071 * Case A: We're currently holding back
9072 * a message for further processing.
9073 */
9074
9075 if( ssl->keep_current_message == 1 )
9076 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009077 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009078 return( 1 );
9079 }
9080
9081 /*
9082 * Case B: Further records are pending in the current datagram.
9083 */
9084
9085#if defined(MBEDTLS_SSL_PROTO_DTLS)
9086 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9087 ssl->in_left > ssl->next_record_offset )
9088 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009089 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009090 return( 1 );
9091 }
9092#endif /* MBEDTLS_SSL_PROTO_DTLS */
9093
9094 /*
9095 * Case C: A handshake message is being processed.
9096 */
9097
Hanno Becker8b170a02017-10-10 11:51:19 +01009098 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9099 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009101 return( 1 );
9102 }
9103
9104 /*
9105 * Case D: An application data message is being processed
9106 */
9107 if( ssl->in_offt != NULL )
9108 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009109 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009110 return( 1 );
9111 }
9112
9113 /*
9114 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009115 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009116 * we implement support for multiple alerts in single records.
9117 */
9118
9119 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9120 return( 0 );
9121}
9122
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009123uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009124{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009125 if( ssl->session != NULL )
9126 return( ssl->session->verify_result );
9127
9128 if( ssl->session_negotiate != NULL )
9129 return( ssl->session_negotiate->verify_result );
9130
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009131 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009132}
9133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009134const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009135{
Paul Bakker926c8e42013-03-06 10:23:34 +01009136 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009137 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009139 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009140}
9141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009142const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009143{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009144#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009145 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009146 {
9147 switch( ssl->minor_ver )
9148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009149 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009150 return( "DTLSv1.0" );
9151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009152 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009153 return( "DTLSv1.2" );
9154
9155 default:
9156 return( "unknown (DTLS)" );
9157 }
9158 }
9159#endif
9160
Paul Bakker43ca69c2011-01-15 17:35:19 +00009161 switch( ssl->minor_ver )
9162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009163 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009164 return( "SSLv3.0" );
9165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009166 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009167 return( "TLSv1.0" );
9168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009169 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009170 return( "TLSv1.1" );
9171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009172 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009173 return( "TLSv1.2" );
9174
Paul Bakker43ca69c2011-01-15 17:35:19 +00009175 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009176 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009177 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009178}
9179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009180int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009181{
Hanno Becker3136ede2018-08-17 15:28:19 +01009182 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009183 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009184 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009185
Hanno Becker78640902018-08-13 16:35:15 +01009186 if( transform == NULL )
9187 return( (int) mbedtls_ssl_hdr_len( ssl ) );
9188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009189#if defined(MBEDTLS_ZLIB_SUPPORT)
9190 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9191 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009192#endif
9193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009194 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009196 case MBEDTLS_MODE_GCM:
9197 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009198 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009199 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009200 transform_expansion = transform->minlen;
9201 break;
9202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009203 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009204
9205 block_size = mbedtls_cipher_get_block_size(
9206 &transform->cipher_ctx_enc );
9207
Hanno Becker3136ede2018-08-17 15:28:19 +01009208 /* Expansion due to the addition of the MAC. */
9209 transform_expansion += transform->maclen;
9210
9211 /* Expansion due to the addition of CBC padding;
9212 * Theoretically up to 256 bytes, but we never use
9213 * more than the block size of the underlying cipher. */
9214 transform_expansion += block_size;
9215
9216 /* For TLS 1.1 or higher, an explicit IV is added
9217 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009218#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9219 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009220 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009221#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009222
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009223 break;
9224
9225 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009227 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009228 }
9229
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02009230 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009231}
9232
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009233#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9234size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9235{
9236 size_t max_len;
9237
9238 /*
9239 * Assume mfl_code is correct since it was checked when set
9240 */
Angus Grattond8213d02016-05-25 20:56:48 +10009241 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009242
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009243 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009244 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009245 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009246 {
Angus Grattond8213d02016-05-25 20:56:48 +10009247 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009248 }
9249
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009250 /* During a handshake, use the value being negotiated */
9251 if( ssl->session_negotiate != NULL &&
9252 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9253 {
9254 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9255 }
9256
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009257 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009258}
9259#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9260
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009261#if defined(MBEDTLS_SSL_PROTO_DTLS)
9262static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9263{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009264 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9265 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9266 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9267 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9268 return ( 0 );
9269
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009270 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9271 return( ssl->mtu );
9272
9273 if( ssl->mtu == 0 )
9274 return( ssl->handshake->mtu );
9275
9276 return( ssl->mtu < ssl->handshake->mtu ?
9277 ssl->mtu : ssl->handshake->mtu );
9278}
9279#endif /* MBEDTLS_SSL_PROTO_DTLS */
9280
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009281int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9282{
9283 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9284
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009285#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9286 !defined(MBEDTLS_SSL_PROTO_DTLS)
9287 (void) ssl;
9288#endif
9289
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009290#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9291 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9292
9293 if( max_len > mfl )
9294 max_len = mfl;
9295#endif
9296
9297#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009298 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009299 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009300 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009301 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9302 const size_t overhead = (size_t) ret;
9303
9304 if( ret < 0 )
9305 return( ret );
9306
9307 if( mtu <= overhead )
9308 {
9309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9310 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9311 }
9312
9313 if( max_len > mtu - overhead )
9314 max_len = mtu - overhead;
9315 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009316#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009317
Hanno Becker0defedb2018-08-10 12:35:02 +01009318#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9319 !defined(MBEDTLS_SSL_PROTO_DTLS)
9320 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009321#endif
9322
9323 return( (int) max_len );
9324}
9325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009326#if defined(MBEDTLS_X509_CRT_PARSE_C)
9327const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009328{
9329 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009330 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009331
Hanno Beckere6824572019-02-07 13:18:46 +00009332#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009333 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009334#else
9335 return( NULL );
9336#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009337}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009338#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009340#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009341int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9342 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009343{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009344 if( ssl == NULL ||
9345 dst == NULL ||
9346 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009347 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009349 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009350 }
9351
Hanno Becker52055ae2019-02-06 14:30:46 +00009352 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009353}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009354#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009355
Paul Bakker5121ce52009-01-03 21:22:43 +00009356/*
Paul Bakker1961b702013-01-25 14:49:24 +01009357 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009360{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009361 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009362
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009363 if( ssl == NULL || ssl->conf == NULL )
9364 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009366#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009367 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009368 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009369#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009370#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009371 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009372 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009373#endif
9374
Paul Bakker1961b702013-01-25 14:49:24 +01009375 return( ret );
9376}
9377
9378/*
9379 * Perform the SSL handshake
9380 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009381int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009382{
9383 int ret = 0;
9384
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009385 if( ssl == NULL || ssl->conf == NULL )
9386 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009392 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009393
9394 if( ret != 0 )
9395 break;
9396 }
9397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009399
9400 return( ret );
9401}
9402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009403#if defined(MBEDTLS_SSL_RENEGOTIATION)
9404#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009405/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009406 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009408static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009409{
9410 int ret;
9411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009413
9414 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009415 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9416 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009417
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009418 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009419 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009420 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009421 return( ret );
9422 }
9423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009425
9426 return( 0 );
9427}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009429
9430/*
9431 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009432 * - any side: calling mbedtls_ssl_renegotiate(),
9433 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9434 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009435 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009436 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009437 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009439static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009440{
9441 int ret;
9442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009444
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009445 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9446 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009447
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009448 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9449 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009450#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009451 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009452 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009453 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009454 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009455 ssl->handshake->out_msg_seq = 1;
9456 else
9457 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009458 }
9459#endif
9460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009461 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9462 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009464 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009466 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009467 return( ret );
9468 }
9469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009471
9472 return( 0 );
9473}
9474
9475/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009476 * Renegotiate current connection on client,
9477 * or request renegotiation on server
9478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009479int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009480{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009481 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009482
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009483 if( ssl == NULL || ssl->conf == NULL )
9484 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009487 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009488 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9491 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009493 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009494
9495 /* Did we already try/start sending HelloRequest? */
9496 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009497 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009498
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009499 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009500 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009503#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009504 /*
9505 * On client, either start the renegotiation process or,
9506 * if already in progress, continue the handshake
9507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009508 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009510 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9511 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009512
9513 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009515 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009516 return( ret );
9517 }
9518 }
9519 else
9520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009521 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009523 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009524 return( ret );
9525 }
9526 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009527#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009528
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009529 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009530}
9531
9532/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009533 * Check record counters and renegotiate if they're above the limit.
9534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009536{
Andres AG2196c7f2016-12-15 17:01:16 +00009537 size_t ep_len = ssl_ep_len( ssl );
9538 int in_ctr_cmp;
9539 int out_ctr_cmp;
9540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9542 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009543 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009544 {
9545 return( 0 );
9546 }
9547
Andres AG2196c7f2016-12-15 17:01:16 +00009548 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9549 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009550 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009551 ssl->conf->renego_period + ep_len, 8 - ep_len );
9552
9553 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009554 {
9555 return( 0 );
9556 }
9557
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009559 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009560}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009562
9563/*
9564 * Receive application data decrypted from the SSL layer
9565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009567{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009568 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009569 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009570
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009571 if( ssl == NULL || ssl->conf == NULL )
9572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009577 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009579 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009580 return( ret );
9581
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009582 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009583 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009584 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009585 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009586 return( ret );
9587 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009588 }
9589#endif
9590
Hanno Becker4a810fb2017-05-24 16:27:30 +01009591 /*
9592 * Check if renegotiation is necessary and/or handshake is
9593 * in process. If yes, perform/continue, and fall through
9594 * if an unexpected packet is received while the client
9595 * is waiting for the ServerHello.
9596 *
9597 * (There is no equivalent to the last condition on
9598 * the server-side as it is not treated as within
9599 * a handshake while waiting for the ClientHello
9600 * after a renegotiation request.)
9601 */
9602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009603#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009604 ret = ssl_check_ctr_renegotiate( ssl );
9605 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9606 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009609 return( ret );
9610 }
9611#endif
9612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009613 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009615 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009616 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9617 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009619 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009620 return( ret );
9621 }
9622 }
9623
Hanno Beckere41158b2017-10-23 13:30:32 +01009624 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009625 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009626 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009627 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009628 if( ssl->f_get_timer != NULL &&
9629 ssl->f_get_timer( ssl->p_timer ) == -1 )
9630 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009631 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009632 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009633
Hanno Becker327c93b2018-08-15 13:56:18 +01009634 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009635 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009636 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9637 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009638
Hanno Becker4a810fb2017-05-24 16:27:30 +01009639 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9640 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009641 }
9642
9643 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009644 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009645 {
9646 /*
9647 * OpenSSL sends empty messages to randomize the IV
9648 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009649 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009651 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009652 return( 0 );
9653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009655 return( ret );
9656 }
9657 }
9658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009659 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009662
Hanno Becker4a810fb2017-05-24 16:27:30 +01009663 /*
9664 * - For client-side, expect SERVER_HELLO_REQUEST.
9665 * - For server-side, expect CLIENT_HELLO.
9666 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9667 */
9668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009669#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009670 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009671 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009672 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009675
9676 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009677#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009678 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009679 {
9680 continue;
9681 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009682#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009683 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009684 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009685#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009686
Hanno Becker4a810fb2017-05-24 16:27:30 +01009687#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009688 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009689 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009692
9693 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009694#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009695 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009696 {
9697 continue;
9698 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009699#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009700 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009701 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009702#endif /* MBEDTLS_SSL_SRV_C */
9703
Hanno Becker21df7f92017-10-17 11:03:26 +01009704#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009705 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009706 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9707 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9708 ssl->conf->allow_legacy_renegotiation ==
9709 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9710 {
9711 /*
9712 * Accept renegotiation request
9713 */
Paul Bakker48916f92012-09-16 19:57:18 +00009714
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009715 /* DTLS clients need to know renego is server-initiated */
9716#if defined(MBEDTLS_SSL_PROTO_DTLS)
9717 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9718 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9719 {
9720 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9721 }
9722#endif
9723 ret = ssl_start_renegotiation( ssl );
9724 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9725 ret != 0 )
9726 {
9727 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9728 return( ret );
9729 }
9730 }
9731 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009732#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009733 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009734 /*
9735 * Refuse renegotiation
9736 */
9737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009738 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009740#if defined(MBEDTLS_SSL_PROTO_SSL3)
9741 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009742 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009743 /* SSLv3 does not have a "no_renegotiation" warning, so
9744 we send a fatal alert and abort the connection. */
9745 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9746 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9747 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009748 }
9749 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009750#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9751#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9752 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9753 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009755 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9756 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9757 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009758 {
9759 return( ret );
9760 }
Paul Bakker48916f92012-09-16 19:57:18 +00009761 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009762 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009763#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9764 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9767 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009768 }
Paul Bakker48916f92012-09-16 19:57:18 +00009769 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009770
Hanno Becker90333da2017-10-10 11:27:13 +01009771 /* At this point, we don't know whether the renegotiation has been
9772 * completed or not. The cases to consider are the following:
9773 * 1) The renegotiation is complete. In this case, no new record
9774 * has been read yet.
9775 * 2) The renegotiation is incomplete because the client received
9776 * an application data record while awaiting the ServerHello.
9777 * 3) The renegotiation is incomplete because the client received
9778 * a non-handshake, non-application data message while awaiting
9779 * the ServerHello.
9780 * In each of these case, looping will be the proper action:
9781 * - For 1), the next iteration will read a new record and check
9782 * if it's application data.
9783 * - For 2), the loop condition isn't satisfied as application data
9784 * is present, hence continue is the same as break
9785 * - For 3), the loop condition is satisfied and read_record
9786 * will re-deliver the message that was held back by the client
9787 * when expecting the ServerHello.
9788 */
9789 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009790 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009791#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009792 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009793 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009794 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009795 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009796 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009799 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009800 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009801 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009802 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009803 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009804#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009806 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9807 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009810 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009811 }
9812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009813 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9816 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009817 }
9818
9819 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009820
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009821 /* We're going to return something now, cancel timer,
9822 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009823 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009824 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009825
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009826#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009827 /* If we requested renego but received AppData, resend HelloRequest.
9828 * Do it now, after setting in_offt, to avoid taking this branch
9829 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009830#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009831 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009832 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009833 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009834 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009836 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009837 return( ret );
9838 }
9839 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009840#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009841#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009842 }
9843
9844 n = ( len < ssl->in_msglen )
9845 ? len : ssl->in_msglen;
9846
9847 memcpy( buf, ssl->in_offt, n );
9848 ssl->in_msglen -= n;
9849
9850 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009851 {
9852 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009853 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009854 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009855 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009856 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009857 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009858 /* more data available */
9859 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009860 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009863
Paul Bakker23986e52011-04-24 08:57:21 +00009864 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009865}
9866
9867/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009868 * Send application data to be encrypted by the SSL layer, taking care of max
9869 * fragment length and buffer size.
9870 *
9871 * According to RFC 5246 Section 6.2.1:
9872 *
9873 * Zero-length fragments of Application data MAY be sent as they are
9874 * potentially useful as a traffic analysis countermeasure.
9875 *
9876 * Therefore, it is possible that the input message length is 0 and the
9877 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009878 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009879static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009880 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009881{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009882 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9883 const size_t max_len = (size_t) ret;
9884
9885 if( ret < 0 )
9886 {
9887 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9888 return( ret );
9889 }
9890
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009891 if( len > max_len )
9892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009893#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009894 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009897 "maximum fragment length: %d > %d",
9898 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009899 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009900 }
9901 else
9902#endif
9903 len = max_len;
9904 }
Paul Bakker887bd502011-06-08 13:10:54 +00009905
Paul Bakker5121ce52009-01-03 21:22:43 +00009906 if( ssl->out_left != 0 )
9907 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009908 /*
9909 * The user has previously tried to send the data and
9910 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9911 * written. In this case, we expect the high-level write function
9912 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9913 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009914 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009917 return( ret );
9918 }
9919 }
Paul Bakker887bd502011-06-08 13:10:54 +00009920 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009921 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009922 /*
9923 * The user is trying to send a message the first time, so we need to
9924 * copy the data into the internal buffers and setup the data structure
9925 * to keep track of partial writes
9926 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009927 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009928 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009929 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009930
Hanno Becker67bc7c32018-08-06 11:33:50 +01009931 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009933 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009934 return( ret );
9935 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009936 }
9937
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009938 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009939}
9940
9941/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009942 * Write application data, doing 1/n-1 splitting if necessary.
9943 *
9944 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009945 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009946 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009947 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009948#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009949static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009950 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009951{
9952 int ret;
9953
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009954 if( ssl->conf->cbc_record_splitting ==
9955 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009956 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009957 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9958 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9959 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009960 {
9961 return( ssl_write_real( ssl, buf, len ) );
9962 }
9963
9964 if( ssl->split_done == 0 )
9965 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009966 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009967 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009968 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009969 }
9970
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009971 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9972 return( ret );
9973 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009974
9975 return( ret + 1 );
9976}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009977#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009978
9979/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009980 * Write application data (public-facing wrapper)
9981 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009982int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009983{
9984 int ret;
9985
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009987
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009988 if( ssl == NULL || ssl->conf == NULL )
9989 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9990
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009991#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009992 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9993 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009994 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009995 return( ret );
9996 }
9997#endif
9998
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009999 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010000 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010001 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010002 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010003 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010004 return( ret );
10005 }
10006 }
10007
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010008#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010009 ret = ssl_write_split( ssl, buf, len );
10010#else
10011 ret = ssl_write_real( ssl, buf, len );
10012#endif
10013
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010015
10016 return( ret );
10017}
10018
10019/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010020 * Notify the peer that the connection is being closed
10021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010022int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010023{
10024 int ret;
10025
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010026 if( ssl == NULL || ssl->conf == NULL )
10027 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010030
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010031 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010032 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010034 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010036 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10037 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10038 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010040 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010041 return( ret );
10042 }
10043 }
10044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010046
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010047 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010048}
10049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010050void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010051{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010052 if( transform == NULL )
10053 return;
10054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010055#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010056 deflateEnd( &transform->ctx_deflate );
10057 inflateEnd( &transform->ctx_inflate );
10058#endif
10059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010060 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10061 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010062
Hanno Beckerd56ed242018-01-03 15:32:51 +000010063#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010064 mbedtls_md_free( &transform->md_ctx_enc );
10065 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010066#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010067
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010068 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010069}
10070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010071#if defined(MBEDTLS_X509_CRT_PARSE_C)
10072static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010073{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010074 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010075
10076 while( cur != NULL )
10077 {
10078 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010079 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010080 cur = next;
10081 }
10082}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010083#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010084
Hanno Becker0271f962018-08-16 13:23:47 +010010085#if defined(MBEDTLS_SSL_PROTO_DTLS)
10086
10087static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10088{
10089 unsigned offset;
10090 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10091
10092 if( hs == NULL )
10093 return;
10094
Hanno Becker283f5ef2018-08-24 09:34:47 +010010095 ssl_free_buffered_record( ssl );
10096
Hanno Becker0271f962018-08-16 13:23:47 +010010097 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010098 ssl_buffering_free_slot( ssl, offset );
10099}
10100
10101static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10102 uint8_t slot )
10103{
10104 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10105 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010106
10107 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10108 return;
10109
Hanno Beckere605b192018-08-21 15:59:07 +010010110 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010111 {
Hanno Beckere605b192018-08-21 15:59:07 +010010112 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010113 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010114 mbedtls_free( hs_buf->data );
10115 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010116 }
10117}
10118
10119#endif /* MBEDTLS_SSL_PROTO_DTLS */
10120
Gilles Peskine9b562d52018-04-25 20:32:43 +020010121void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010122{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010123 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10124
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010125 if( handshake == NULL )
10126 return;
10127
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010128#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10129 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10130 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010131 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010132 handshake->async_in_progress = 0;
10133 }
10134#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10135
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010136#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10137 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10138 mbedtls_md5_free( &handshake->fin_md5 );
10139 mbedtls_sha1_free( &handshake->fin_sha1 );
10140#endif
10141#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10142#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010143#if defined(MBEDTLS_USE_PSA_CRYPTO)
10144 psa_hash_abort( &handshake->fin_sha256_psa );
10145#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010146 mbedtls_sha256_free( &handshake->fin_sha256 );
10147#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010148#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010149#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010150#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010151 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010152#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010153 mbedtls_sha512_free( &handshake->fin_sha512 );
10154#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010155#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010156#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010158#if defined(MBEDTLS_DHM_C)
10159 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010160#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010161#if defined(MBEDTLS_ECDH_C)
10162 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010163#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010164#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010165 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010166#if defined(MBEDTLS_SSL_CLI_C)
10167 mbedtls_free( handshake->ecjpake_cache );
10168 handshake->ecjpake_cache = NULL;
10169 handshake->ecjpake_cache_len = 0;
10170#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010171#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010172
Janos Follath4ae5c292016-02-10 11:27:43 +000010173#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10174 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010175 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010176 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010177#endif
10178
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010179#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10180 if( handshake->psk != NULL )
10181 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010182 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010183 mbedtls_free( handshake->psk );
10184 }
10185#endif
10186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010187#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10188 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010189 /*
10190 * Free only the linked list wrapper, not the keys themselves
10191 * since the belong to the SNI callback
10192 */
10193 if( handshake->sni_key_cert != NULL )
10194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010195 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010196
10197 while( cur != NULL )
10198 {
10199 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010200 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010201 cur = next;
10202 }
10203 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010204#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010205
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010206#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010207 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010208 if( handshake->ecrs_peer_cert != NULL )
10209 {
10210 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10211 mbedtls_free( handshake->ecrs_peer_cert );
10212 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010213#endif
10214
Hanno Becker75173122019-02-06 16:18:31 +000010215#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10216 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10217 mbedtls_pk_free( &handshake->peer_pubkey );
10218#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010220#if defined(MBEDTLS_SSL_PROTO_DTLS)
10221 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010222 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010223 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010224#endif
10225
Hanno Becker4a63ed42019-01-08 11:39:35 +000010226#if defined(MBEDTLS_ECDH_C) && \
10227 defined(MBEDTLS_USE_PSA_CRYPTO)
10228 psa_destroy_key( handshake->ecdh_psa_privkey );
10229#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10230
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010231 mbedtls_platform_zeroize( handshake,
10232 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010233}
10234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010235void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010236{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010237 if( session == NULL )
10238 return;
10239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010240#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010241 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010242#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010243
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010244#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010245 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010246#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010247
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010248 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010249}
10250
Paul Bakker5121ce52009-01-03 21:22:43 +000010251/*
10252 * Free an SSL context
10253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010254void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010255{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010256 if( ssl == NULL )
10257 return;
10258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010260
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010261 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010262 {
Angus Grattond8213d02016-05-25 20:56:48 +100010263 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010264 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010265 }
10266
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010267 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010268 {
Angus Grattond8213d02016-05-25 20:56:48 +100010269 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010270 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010271 }
10272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010273#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010274 if( ssl->compress_buf != NULL )
10275 {
Angus Grattond8213d02016-05-25 20:56:48 +100010276 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010277 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010278 }
10279#endif
10280
Paul Bakker48916f92012-09-16 19:57:18 +000010281 if( ssl->transform )
10282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010283 mbedtls_ssl_transform_free( ssl->transform );
10284 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010285 }
10286
10287 if( ssl->handshake )
10288 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010289 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010290 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10291 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010293 mbedtls_free( ssl->handshake );
10294 mbedtls_free( ssl->transform_negotiate );
10295 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010296 }
10297
Paul Bakkerc0463502013-02-14 11:19:38 +010010298 if( ssl->session )
10299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010300 mbedtls_ssl_session_free( ssl->session );
10301 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010302 }
10303
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010304#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010305 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010306 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010307 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010308 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010309 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010310#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010312#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10313 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10316 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010317 }
10318#endif
10319
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010320#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010321 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010322#endif
10323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010324 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010325
Paul Bakker86f04f42013-02-14 11:20:09 +010010326 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010327 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010328}
10329
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010330/*
10331 * Initialze mbedtls_ssl_config
10332 */
10333void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10334{
10335 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10336}
10337
Simon Butcherc97b6972015-12-27 23:48:17 +000010338#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010339static int ssl_preset_default_hashes[] = {
10340#if defined(MBEDTLS_SHA512_C)
10341 MBEDTLS_MD_SHA512,
10342 MBEDTLS_MD_SHA384,
10343#endif
10344#if defined(MBEDTLS_SHA256_C)
10345 MBEDTLS_MD_SHA256,
10346 MBEDTLS_MD_SHA224,
10347#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010348#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010349 MBEDTLS_MD_SHA1,
10350#endif
10351 MBEDTLS_MD_NONE
10352};
Simon Butcherc97b6972015-12-27 23:48:17 +000010353#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010354
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010355static int ssl_preset_suiteb_ciphersuites[] = {
10356 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10357 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10358 0
10359};
10360
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010361#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010362static int ssl_preset_suiteb_hashes[] = {
10363 MBEDTLS_MD_SHA256,
10364 MBEDTLS_MD_SHA384,
10365 MBEDTLS_MD_NONE
10366};
10367#endif
10368
10369#if defined(MBEDTLS_ECP_C)
10370static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10371 MBEDTLS_ECP_DP_SECP256R1,
10372 MBEDTLS_ECP_DP_SECP384R1,
10373 MBEDTLS_ECP_DP_NONE
10374};
10375#endif
10376
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010377/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010378 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010379 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010380int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010381 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010382{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010383#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010384 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010385#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010386
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010387 /* Use the functions here so that they are covered in tests,
10388 * but otherwise access member directly for efficiency */
10389 mbedtls_ssl_conf_endpoint( conf, endpoint );
10390 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010391
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010392 /*
10393 * Things that are common to all presets
10394 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010395#if defined(MBEDTLS_SSL_CLI_C)
10396 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10397 {
10398 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10399#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10400 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10401#endif
10402 }
10403#endif
10404
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010405#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010406 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010407#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010408
10409#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10410 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10411#endif
10412
10413#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10414 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10415#endif
10416
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010417#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10418 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10419#endif
10420
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010421#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010422 conf->f_cookie_write = ssl_cookie_write_dummy;
10423 conf->f_cookie_check = ssl_cookie_check_dummy;
10424#endif
10425
10426#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10427 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10428#endif
10429
Janos Follath088ce432017-04-10 12:42:31 +010010430#if defined(MBEDTLS_SSL_SRV_C)
10431 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10432#endif
10433
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010434#if defined(MBEDTLS_SSL_PROTO_DTLS)
10435 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10436 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10437#endif
10438
10439#if defined(MBEDTLS_SSL_RENEGOTIATION)
10440 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010441 memset( conf->renego_period, 0x00, 2 );
10442 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010443#endif
10444
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010445#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10446 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10447 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010448 const unsigned char dhm_p[] =
10449 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10450 const unsigned char dhm_g[] =
10451 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10452
Hanno Beckera90658f2017-10-04 15:29:08 +010010453 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10454 dhm_p, sizeof( dhm_p ),
10455 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010456 {
10457 return( ret );
10458 }
10459 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010460#endif
10461
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010462 /*
10463 * Preset-specific defaults
10464 */
10465 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010466 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010467 /*
10468 * NSA Suite B
10469 */
10470 case MBEDTLS_SSL_PRESET_SUITEB:
10471 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10472 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10473 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10474 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10475
10476 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10477 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10478 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10479 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10480 ssl_preset_suiteb_ciphersuites;
10481
10482#if defined(MBEDTLS_X509_CRT_PARSE_C)
10483 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010484#endif
10485
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010486#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010487 conf->sig_hashes = ssl_preset_suiteb_hashes;
10488#endif
10489
10490#if defined(MBEDTLS_ECP_C)
10491 conf->curve_list = ssl_preset_suiteb_curves;
10492#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010493 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010494
10495 /*
10496 * Default
10497 */
10498 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010499 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10500 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10501 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10502 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10503 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10504 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10505 MBEDTLS_SSL_MIN_MINOR_VERSION :
10506 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010507 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10508 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10509
10510#if defined(MBEDTLS_SSL_PROTO_DTLS)
10511 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10512 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10513#endif
10514
10515 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10516 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10517 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10518 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10519 mbedtls_ssl_list_ciphersuites();
10520
10521#if defined(MBEDTLS_X509_CRT_PARSE_C)
10522 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10523#endif
10524
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010525#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010526 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010527#endif
10528
10529#if defined(MBEDTLS_ECP_C)
10530 conf->curve_list = mbedtls_ecp_grp_id_list();
10531#endif
10532
10533#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10534 conf->dhm_min_bitlen = 1024;
10535#endif
10536 }
10537
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010538 return( 0 );
10539}
10540
10541/*
10542 * Free mbedtls_ssl_config
10543 */
10544void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10545{
10546#if defined(MBEDTLS_DHM_C)
10547 mbedtls_mpi_free( &conf->dhm_P );
10548 mbedtls_mpi_free( &conf->dhm_G );
10549#endif
10550
10551#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10552 if( conf->psk != NULL )
10553 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010554 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010555 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010556 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010557 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010558 }
10559
10560 if( conf->psk_identity != NULL )
10561 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010562 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010563 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010564 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010565 conf->psk_identity_len = 0;
10566 }
10567#endif
10568
10569#if defined(MBEDTLS_X509_CRT_PARSE_C)
10570 ssl_key_cert_free( conf->key_cert );
10571#endif
10572
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010573 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010574}
10575
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010576#if defined(MBEDTLS_PK_C) && \
10577 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010578/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010579 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010581unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010582{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010583#if defined(MBEDTLS_RSA_C)
10584 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10585 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010586#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010587#if defined(MBEDTLS_ECDSA_C)
10588 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10589 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010590#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010591 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010592}
10593
Hanno Becker7e5437a2017-04-28 17:15:26 +010010594unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10595{
10596 switch( type ) {
10597 case MBEDTLS_PK_RSA:
10598 return( MBEDTLS_SSL_SIG_RSA );
10599 case MBEDTLS_PK_ECDSA:
10600 case MBEDTLS_PK_ECKEY:
10601 return( MBEDTLS_SSL_SIG_ECDSA );
10602 default:
10603 return( MBEDTLS_SSL_SIG_ANON );
10604 }
10605}
10606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010607mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010608{
10609 switch( sig )
10610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010611#if defined(MBEDTLS_RSA_C)
10612 case MBEDTLS_SSL_SIG_RSA:
10613 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010614#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010615#if defined(MBEDTLS_ECDSA_C)
10616 case MBEDTLS_SSL_SIG_ECDSA:
10617 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010618#endif
10619 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010620 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010621 }
10622}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010623#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010624
Hanno Becker7e5437a2017-04-28 17:15:26 +010010625#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10626 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10627
10628/* Find an entry in a signature-hash set matching a given hash algorithm. */
10629mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10630 mbedtls_pk_type_t sig_alg )
10631{
10632 switch( sig_alg )
10633 {
10634 case MBEDTLS_PK_RSA:
10635 return( set->rsa );
10636 case MBEDTLS_PK_ECDSA:
10637 return( set->ecdsa );
10638 default:
10639 return( MBEDTLS_MD_NONE );
10640 }
10641}
10642
10643/* Add a signature-hash-pair to a signature-hash set */
10644void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10645 mbedtls_pk_type_t sig_alg,
10646 mbedtls_md_type_t md_alg )
10647{
10648 switch( sig_alg )
10649 {
10650 case MBEDTLS_PK_RSA:
10651 if( set->rsa == MBEDTLS_MD_NONE )
10652 set->rsa = md_alg;
10653 break;
10654
10655 case MBEDTLS_PK_ECDSA:
10656 if( set->ecdsa == MBEDTLS_MD_NONE )
10657 set->ecdsa = md_alg;
10658 break;
10659
10660 default:
10661 break;
10662 }
10663}
10664
10665/* Allow exactly one hash algorithm for each signature. */
10666void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10667 mbedtls_md_type_t md_alg )
10668{
10669 set->rsa = md_alg;
10670 set->ecdsa = md_alg;
10671}
10672
10673#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10674 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10675
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010676/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010677 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010679mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010680{
10681 switch( hash )
10682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010683#if defined(MBEDTLS_MD5_C)
10684 case MBEDTLS_SSL_HASH_MD5:
10685 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010686#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010687#if defined(MBEDTLS_SHA1_C)
10688 case MBEDTLS_SSL_HASH_SHA1:
10689 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010690#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010691#if defined(MBEDTLS_SHA256_C)
10692 case MBEDTLS_SSL_HASH_SHA224:
10693 return( MBEDTLS_MD_SHA224 );
10694 case MBEDTLS_SSL_HASH_SHA256:
10695 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010696#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010697#if defined(MBEDTLS_SHA512_C)
10698 case MBEDTLS_SSL_HASH_SHA384:
10699 return( MBEDTLS_MD_SHA384 );
10700 case MBEDTLS_SSL_HASH_SHA512:
10701 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010702#endif
10703 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010704 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010705 }
10706}
10707
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010708/*
10709 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10710 */
10711unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10712{
10713 switch( md )
10714 {
10715#if defined(MBEDTLS_MD5_C)
10716 case MBEDTLS_MD_MD5:
10717 return( MBEDTLS_SSL_HASH_MD5 );
10718#endif
10719#if defined(MBEDTLS_SHA1_C)
10720 case MBEDTLS_MD_SHA1:
10721 return( MBEDTLS_SSL_HASH_SHA1 );
10722#endif
10723#if defined(MBEDTLS_SHA256_C)
10724 case MBEDTLS_MD_SHA224:
10725 return( MBEDTLS_SSL_HASH_SHA224 );
10726 case MBEDTLS_MD_SHA256:
10727 return( MBEDTLS_SSL_HASH_SHA256 );
10728#endif
10729#if defined(MBEDTLS_SHA512_C)
10730 case MBEDTLS_MD_SHA384:
10731 return( MBEDTLS_SSL_HASH_SHA384 );
10732 case MBEDTLS_MD_SHA512:
10733 return( MBEDTLS_SSL_HASH_SHA512 );
10734#endif
10735 default:
10736 return( MBEDTLS_SSL_HASH_NONE );
10737 }
10738}
10739
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010740#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010741/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010742 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010743 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010744 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010745int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010746{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010747 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010748
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010749 if( ssl->conf->curve_list == NULL )
10750 return( -1 );
10751
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010752 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010753 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010754 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010755
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010756 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010757}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010758#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010759
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010760#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010761/*
10762 * Check if a hash proposed by the peer is in our list.
10763 * Return 0 if we're willing to use it, -1 otherwise.
10764 */
10765int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10766 mbedtls_md_type_t md )
10767{
10768 const int *cur;
10769
10770 if( ssl->conf->sig_hashes == NULL )
10771 return( -1 );
10772
10773 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10774 if( *cur == (int) md )
10775 return( 0 );
10776
10777 return( -1 );
10778}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010779#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010781#if defined(MBEDTLS_X509_CRT_PARSE_C)
10782int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10783 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010784 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010785 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010786{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010787 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010788#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010789 int usage = 0;
10790#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010791#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010792 const char *ext_oid;
10793 size_t ext_len;
10794#endif
10795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010796#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10797 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010798 ((void) cert);
10799 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010800 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010801#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010803#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10804 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010805 {
10806 /* Server part of the key exchange */
10807 switch( ciphersuite->key_exchange )
10808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010809 case MBEDTLS_KEY_EXCHANGE_RSA:
10810 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010811 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010812 break;
10813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010814 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10815 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10816 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10817 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010818 break;
10819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010820 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10821 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010822 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010823 break;
10824
10825 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010826 case MBEDTLS_KEY_EXCHANGE_NONE:
10827 case MBEDTLS_KEY_EXCHANGE_PSK:
10828 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10829 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010830 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010831 usage = 0;
10832 }
10833 }
10834 else
10835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010836 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10837 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010838 }
10839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010840 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010841 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010842 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010843 ret = -1;
10844 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010845#else
10846 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010847#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010849#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10850 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010852 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10853 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010854 }
10855 else
10856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010857 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10858 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010859 }
10860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010861 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010862 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010863 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010864 ret = -1;
10865 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010866#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010867
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010868 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010869}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010870#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010871
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010872/*
10873 * Convert version numbers to/from wire format
10874 * and, for DTLS, to/from TLS equivalent.
10875 *
10876 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010877 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010878 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10879 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10880 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010881void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010882 unsigned char ver[2] )
10883{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010884#if defined(MBEDTLS_SSL_PROTO_DTLS)
10885 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010887 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010888 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10889
10890 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10891 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10892 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010893 else
10894#else
10895 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010896#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010897 {
10898 ver[0] = (unsigned char) major;
10899 ver[1] = (unsigned char) minor;
10900 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010901}
10902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010903void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010904 const unsigned char ver[2] )
10905{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010906#if defined(MBEDTLS_SSL_PROTO_DTLS)
10907 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010908 {
10909 *major = 255 - ver[0] + 2;
10910 *minor = 255 - ver[1] + 1;
10911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010912 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010913 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10914 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010915 else
10916#else
10917 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010918#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010919 {
10920 *major = ver[0];
10921 *minor = ver[1];
10922 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010923}
10924
Simon Butcher99000142016-10-13 17:21:01 +010010925int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10926{
10927#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10928 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10929 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10930
10931 switch( md )
10932 {
10933#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10934#if defined(MBEDTLS_MD5_C)
10935 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010936 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010937#endif
10938#if defined(MBEDTLS_SHA1_C)
10939 case MBEDTLS_SSL_HASH_SHA1:
10940 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10941 break;
10942#endif
10943#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10944#if defined(MBEDTLS_SHA512_C)
10945 case MBEDTLS_SSL_HASH_SHA384:
10946 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10947 break;
10948#endif
10949#if defined(MBEDTLS_SHA256_C)
10950 case MBEDTLS_SSL_HASH_SHA256:
10951 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10952 break;
10953#endif
10954 default:
10955 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10956 }
10957
10958 return 0;
10959#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10960 (void) ssl;
10961 (void) md;
10962
10963 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10964#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10965}
10966
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010967#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10968 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10969int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10970 unsigned char *output,
10971 unsigned char *data, size_t data_len )
10972{
10973 int ret = 0;
10974 mbedtls_md5_context mbedtls_md5;
10975 mbedtls_sha1_context mbedtls_sha1;
10976
10977 mbedtls_md5_init( &mbedtls_md5 );
10978 mbedtls_sha1_init( &mbedtls_sha1 );
10979
10980 /*
10981 * digitally-signed struct {
10982 * opaque md5_hash[16];
10983 * opaque sha_hash[20];
10984 * };
10985 *
10986 * md5_hash
10987 * MD5(ClientHello.random + ServerHello.random
10988 * + ServerParams);
10989 * sha_hash
10990 * SHA(ClientHello.random + ServerHello.random
10991 * + ServerParams);
10992 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010993 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010994 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010995 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010996 goto exit;
10997 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010998 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010999 ssl->handshake->randbytes, 64 ) ) != 0 )
11000 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011001 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011002 goto exit;
11003 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011004 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011005 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011006 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011007 goto exit;
11008 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011009 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011010 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011011 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011012 goto exit;
11013 }
11014
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011015 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011016 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011017 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011018 goto exit;
11019 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011020 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011021 ssl->handshake->randbytes, 64 ) ) != 0 )
11022 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011023 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011024 goto exit;
11025 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011026 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011027 data_len ) ) != 0 )
11028 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011029 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011030 goto exit;
11031 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011032 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011033 output + 16 ) ) != 0 )
11034 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011035 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011036 goto exit;
11037 }
11038
11039exit:
11040 mbedtls_md5_free( &mbedtls_md5 );
11041 mbedtls_sha1_free( &mbedtls_sha1 );
11042
11043 if( ret != 0 )
11044 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11045 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11046
11047 return( ret );
11048
11049}
11050#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11051 MBEDTLS_SSL_PROTO_TLS1_1 */
11052
11053#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11054 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011055
11056#if defined(MBEDTLS_USE_PSA_CRYPTO)
11057int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11058 unsigned char *hash, size_t *hashlen,
11059 unsigned char *data, size_t data_len,
11060 mbedtls_md_type_t md_alg )
11061{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011062 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011063 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011064 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11065
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011066 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011067
11068 if( ( status = psa_hash_setup( &hash_operation,
11069 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011070 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011071 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011072 goto exit;
11073 }
11074
Andrzej Kurek814feff2019-01-14 04:35:19 -050011075 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11076 64 ) ) != 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_update", 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,
11083 data, data_len ) ) != 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_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11090 hashlen ) ) != 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_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011093 goto exit;
11094 }
11095
11096exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011097 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011098 {
11099 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11100 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011101 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011102 {
11103 case PSA_ERROR_NOT_SUPPORTED:
11104 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011105 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011106 case PSA_ERROR_BUFFER_TOO_SMALL:
11107 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11108 case PSA_ERROR_INSUFFICIENT_MEMORY:
11109 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11110 default:
11111 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11112 }
11113 }
11114 return( 0 );
11115}
11116
11117#else
11118
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011119int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011120 unsigned char *hash, size_t *hashlen,
11121 unsigned char *data, size_t data_len,
11122 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011123{
11124 int ret = 0;
11125 mbedtls_md_context_t ctx;
11126 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011127 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011128
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011130
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011131 mbedtls_md_init( &ctx );
11132
11133 /*
11134 * digitally-signed struct {
11135 * opaque client_random[32];
11136 * opaque server_random[32];
11137 * ServerDHParams params;
11138 * };
11139 */
11140 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11141 {
11142 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11143 goto exit;
11144 }
11145 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11146 {
11147 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11148 goto exit;
11149 }
11150 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11151 {
11152 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11153 goto exit;
11154 }
11155 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11156 {
11157 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11158 goto exit;
11159 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011160 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011161 {
11162 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11163 goto exit;
11164 }
11165
11166exit:
11167 mbedtls_md_free( &ctx );
11168
11169 if( ret != 0 )
11170 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11171 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11172
11173 return( ret );
11174}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011175#endif /* MBEDTLS_USE_PSA_CRYPTO */
11176
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011177#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11178 MBEDTLS_SSL_PROTO_TLS1_2 */
11179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011180#endif /* MBEDTLS_SSL_TLS_C */