blob: 46b6679a5613d3fe895ae7aa74824cd65b01a37e [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"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Jarno Lamsaaf60cd72019-12-19 16:45:23 +020051#include "mbedtls/platform.h"
52
Paul Bakker0be444a2013-08-27 21:55:01 +020053
Rich Evans00ab4702015-02-06 13:43:58 +000054#include <string.h>
55
Janos Follath23bdca02016-10-07 14:47:14 +010056#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020058#endif
59
Hanno Beckeref982d52019-07-23 15:56:18 +010060#if defined(MBEDTLS_USE_TINYCRYPT)
61static int uecc_rng_wrapper( uint8_t *dest, unsigned int size )
62{
Hanno Beckerd089fad2019-07-24 09:05:05 +010063 int ret;
64 ret = mbedtls_ssl_conf_rng_func( NULL, dest, size );
65 if( ret == 0 )
66 return( (int) size );
67
68 return( 0 );
Hanno Beckeref982d52019-07-23 15:56:18 +010069}
Hanno Becker75f12d12019-07-23 16:16:15 +010070
71int mbedtls_ssl_ecdh_read_peerkey( mbedtls_ssl_context *ssl,
72 unsigned char **p, unsigned char *end )
73{
74 size_t const secp256r1_uncompressed_point_length =
75 1 /* length */ + 1 /* length */ + 2 * NUM_ECC_BYTES /* data */;
76
77 if( (size_t)( end - *p ) < secp256r1_uncompressed_point_length )
78 {
79 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Bad ECDH peer pubkey (too short)" ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010081 }
82
83 if( (*p)[0] != 2 * NUM_ECC_BYTES + 1 ||
84 (*p)[1] != 0x04 )
85 {
86 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Unexpected ECDH peer pubkey header - expected { %#02x, %#02x }, got { %#02x, %#02x }",
87 2 * NUM_ECC_BYTES + 1,
88 0x04,
89 (unsigned) (*p)[0],
90 (unsigned) (*p)[1] ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010092 }
93
Teppo Järvelin91d79382019-10-02 09:09:31 +030094 mbedtls_platform_memcpy( ssl->handshake->ecdh_peerkey, *p + 2, 2 * NUM_ECC_BYTES );
Hanno Becker75f12d12019-07-23 16:16:15 +010095
96 *p += secp256r1_uncompressed_point_length;
97 return( 0 );
98}
Hanno Beckeref982d52019-07-23 15:56:18 +010099#endif /* MBEDTLS_USE_TINYCRYPT */
100
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100101static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +0100102static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100103
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100104/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100106{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200107#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +0100108 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100109#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200110
111#if defined(MBEDTLS_SSL_PROTO_DTLS)
112 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
113 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200114 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200115#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200116#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100117 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200118#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100119}
120
Hanno Beckerb82350b2019-07-26 07:24:05 +0100121static void ssl_send_pending_fatal_alert( mbedtls_ssl_context *ssl )
122{
123 if( ssl->pending_fatal_alert_msg == MBEDTLS_SSL_ALERT_MSG_NONE )
124 return;
125
126 mbedtls_ssl_send_alert_message( ssl,
127 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
128 ssl->pending_fatal_alert_msg );
129 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
130}
131
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200132/*
133 * Start a timer.
134 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200137{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100138 if( mbedtls_ssl_get_set_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200139 return;
140
141 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
Hanno Becker0ae6b242019-06-13 16:45:36 +0100142 mbedtls_ssl_get_set_timer( ssl )( ssl->p_timer,
143 millisecs / 4,
144 millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200145}
146
147/*
148 * Return -1 is timer is expired, 0 if it isn't.
149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200151{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100152 if( mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200153 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200154
Hanno Becker0ae6b242019-06-13 16:45:36 +0100155 if( mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200156 {
157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200158 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200159 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200160
161 return( 0 );
162}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200163
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100164static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
165 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100166static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100167
Hanno Becker02f26092019-07-03 16:13:00 +0100168#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker03e2db62019-07-12 14:40:00 +0100169static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
170 unsigned char *buf,
171 size_t len,
172 mbedtls_record *rec );
173
Hanno Becker02f26092019-07-03 16:13:00 +0100174int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
175 unsigned char *buf,
176 size_t buflen )
177{
Hanno Becker03e2db62019-07-12 14:40:00 +0100178 int ret = 0;
179 mbedtls_record rec;
180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
181 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
182
183 /* We don't support record checking in TLS because
184 * (a) there doesn't seem to be a usecase for it, and
185 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
186 * and we'd need to backup the transform here.
187 */
188#if defined(MBEDTLS_SSL_PROTO_TLS)
189 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
190 {
191 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
192 goto exit;
193 }
194 MBEDTLS_SSL_TRANSPORT_ELSE
195#endif /* MBEDTLS_SSL_PROTO_TLS */
196#if defined(MBEDTLS_SSL_PROTO_DTLS)
197 {
198 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
199 if( ret != 0 )
200 {
201 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
202 goto exit;
203 }
204
205 if( ssl->transform_in != NULL )
206 {
207 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
208 if( ret != 0 )
209 {
210 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
211 goto exit;
212 }
213 }
214 }
215#endif /* MBEDTLS_SSL_PROTO_DTLS */
216
217exit:
218 /* On success, we have decrypted the buffer in-place, so make
219 * sure we don't leak any plaintext data. */
220 mbedtls_platform_zeroize( buf, buflen );
221
222 /* For the purpose of this API, treat messages with unexpected CID
223 * as well as such from future epochs as unexpected. */
224 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
225 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
226 {
227 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
228 }
229
230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
231 return( ret );
Hanno Becker02f26092019-07-03 16:13:00 +0100232}
233#endif /* MBEDTLS_SSL_RECORD_CHECKING */
234
Hanno Becker67bc7c32018-08-06 11:33:50 +0100235#define SSL_DONT_FORCE_FLUSH 0
236#define SSL_FORCE_FLUSH 1
237
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200238#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100239
Hanno Beckera5a2b082019-05-15 14:03:01 +0100240#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100241/* Top-level Connection ID API */
242
Hanno Beckere0200da2019-06-13 09:23:43 +0100243#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
244 !defined(MBEDTLS_SSL_CONF_CID_LEN) && \
245 !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100246int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
247 size_t len,
248 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100249{
250 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
251 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
252
Hanno Becker791ec6b2019-05-14 11:45:26 +0100253 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
254 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
255 {
256 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
257 }
258
259 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100260 conf->cid_len = len;
261 return( 0 );
262}
Hanno Beckere0200da2019-06-13 09:23:43 +0100263#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
264 !MBEDTLS_SSL_CONF_CID_LEN &&
265 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
266
267#if MBEDTLS_SSL_CONF_CID_LEN > MBEDTLS_SSL_CID_IN_LEN_MAX
268#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_CID_LEN"
269#endif
270#if MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE && \
271 MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_FAIL
272#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID"
273#endif
274
275#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
276 !MBEDTLS_SSL_CONF_CID_LEN &&
277 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
Hanno Beckereec2be92019-05-03 13:06:44 +0100278
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100279int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
280 int enable,
281 unsigned char const *own_cid,
282 size_t own_cid_len )
283{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200284 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100285 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
286
Hanno Becker07489862019-04-25 16:01:49 +0100287 ssl->negotiate_cid = enable;
288 if( enable == MBEDTLS_SSL_CID_DISABLED )
289 {
290 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
291 return( 0 );
292 }
293 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100294 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100295
Hanno Beckere0200da2019-06-13 09:23:43 +0100296 if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) )
Hanno Becker07489862019-04-25 16:01:49 +0100297 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100298 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
299 (unsigned) own_cid_len,
Hanno Beckere0200da2019-06-13 09:23:43 +0100300 (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) );
Hanno Becker07489862019-04-25 16:01:49 +0100301 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
302 }
303
Teppo Järvelin6f4e0302019-10-04 13:53:53 +0300304 /* Not using more secure mbedtls_platform_memcpy as cid is public */
305 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100306 /* Truncation is not an issue here because
307 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
308 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100309
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100310 return( 0 );
311}
312
313int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
314 int *enabled,
315 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
316 size_t *peer_cid_len )
317{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100318 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100319
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200320 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100321 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
322 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100323 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100324 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100325
Hanno Beckercb063f52019-05-03 12:54:52 +0100326 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
327 * were used, but client and server requested the empty CID.
328 * This is indistinguishable from not using the CID extension
329 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100330 if( ssl->transform_in->in_cid_len == 0 &&
331 ssl->transform_in->out_cid_len == 0 )
332 {
333 return( 0 );
334 }
335
Hanno Becker633d6042019-05-22 16:50:35 +0100336 if( peer_cid_len != NULL )
337 {
338 *peer_cid_len = ssl->transform_in->out_cid_len;
339 if( peer_cid != NULL )
340 {
Teppo Järvelin6f4e0302019-10-04 13:53:53 +0300341 /* Not using more secure mbedtls_platform_memcpy as cid is public */
342 memcpy( peer_cid, ssl->transform_in->out_cid,
Hanno Becker633d6042019-05-22 16:50:35 +0100343 ssl->transform_in->out_cid_len );
344 }
345 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100346
347 *enabled = MBEDTLS_SSL_CID_ENABLED;
348
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100349 return( 0 );
350}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100351#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100352
Hanno Beckerd5847772018-08-28 10:09:23 +0100353/* Forward declarations for functions related to message buffering. */
354static void ssl_buffering_free( mbedtls_ssl_context *ssl );
355static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
356 uint8_t slot );
357static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
358static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
359static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
360static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +0100361static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
362 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100363static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100364
Hanno Beckera67dee22018-08-22 10:05:20 +0100365static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100366static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100367{
Hanno Becker11682cc2018-08-22 14:41:02 +0100368 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100369
370 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100371 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100372
373 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
374}
375
Hanno Becker67bc7c32018-08-06 11:33:50 +0100376static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
377{
Hanno Becker11682cc2018-08-22 14:41:02 +0100378 size_t const bytes_written = ssl->out_left;
379 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100380
381 /* Double-check that the write-index hasn't gone
382 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100383 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100384 {
385 /* Should never happen... */
386 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
387 }
388
389 return( (int) ( mtu - bytes_written ) );
390}
391
392static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
393{
394 int ret;
395 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400396 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100397
398#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
399 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
400
401 if( max_len > mfl )
402 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100403
404 /* By the standard (RFC 6066 Sect. 4), the MFL extension
405 * only limits the maximum record payload size, so in theory
406 * we would be allowed to pack multiple records of payload size
407 * MFL into a single datagram. However, this would mean that there's
408 * no way to explicitly communicate MTU restrictions to the peer.
409 *
410 * The following reduction of max_len makes sure that we never
411 * write datagrams larger than MFL + Record Expansion Overhead.
412 */
413 if( max_len <= ssl->out_left )
414 return( 0 );
415
416 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100417#endif
418
419 ret = ssl_get_remaining_space_in_datagram( ssl );
420 if( ret < 0 )
421 return( ret );
422 remaining = (size_t) ret;
423
424 ret = mbedtls_ssl_get_record_expansion( ssl );
425 if( ret < 0 )
426 return( ret );
427 expansion = (size_t) ret;
428
429 if( remaining <= expansion )
430 return( 0 );
431
432 remaining -= expansion;
433 if( remaining >= max_len )
434 remaining = max_len;
435
436 return( (int) remaining );
437}
438
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200439/*
440 * Double the retransmit timeout value, within the allowed range,
441 * returning -1 if the maximum value has already been reached.
442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200444{
445 uint32_t new_timeout;
446
Hanno Becker1f835fa2019-06-13 10:14:59 +0100447 if( ssl->handshake->retransmit_timeout >=
448 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
449 {
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200450 return( -1 );
Hanno Becker1f835fa2019-06-13 10:14:59 +0100451 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200452
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200453 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
454 * in the following way: after the initial transmission and a first
455 * retransmission, back off to a temporary estimated MTU of 508 bytes.
456 * This value is guaranteed to be deliverable (if not guaranteed to be
457 * delivered) of any compliant IPv4 (and IPv6) network, and should work
458 * on most non-IP stacks too. */
Hanno Becker1f835fa2019-06-13 10:14:59 +0100459 if( ssl->handshake->retransmit_timeout !=
460 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400461 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200462 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
464 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200465
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200466 new_timeout = 2 * ssl->handshake->retransmit_timeout;
467
468 /* Avoid arithmetic overflow and range overflow */
469 if( new_timeout < ssl->handshake->retransmit_timeout ||
Hanno Becker1f835fa2019-06-13 10:14:59 +0100470 new_timeout > mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200471 {
Hanno Becker1f835fa2019-06-13 10:14:59 +0100472 new_timeout = mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200473 }
474
475 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200477 ssl->handshake->retransmit_timeout ) );
478
479 return( 0 );
480}
481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200483{
Hanno Becker1f835fa2019-06-13 10:14:59 +0100484 ssl->handshake->retransmit_timeout = mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200486 ssl->handshake->retransmit_timeout ) );
487}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200491/*
492 * Convert max_fragment_length codes to length.
493 * RFC 6066 says:
494 * enum{
495 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
496 * } MaxFragmentLength;
497 * and we add 0 -> extension unused
498 */
Angus Grattond8213d02016-05-25 20:56:48 +1000499static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200500{
Angus Grattond8213d02016-05-25 20:56:48 +1000501 switch( mfl )
502 {
503 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300504 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000505 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
506 return 512;
507 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
508 return 1024;
509 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
510 return 2048;
511 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
512 return 4096;
513 default:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300514 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000515 }
516}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200518
Hanno Becker58fccf22019-02-06 14:30:46 +0000519int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
520 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200521{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_ssl_session_free( dst );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300523 mbedtls_platform_memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000526
527#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200528 if( src->peer_cert != NULL )
529 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200530 int ret;
531
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200532 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200533 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200534 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200539 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200542 dst->peer_cert = NULL;
543 return( ret );
544 }
545 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100546#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000547 if( src->peer_cert_digest != NULL )
548 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000549 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000550 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000551 if( dst->peer_cert_digest == NULL )
552 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
553
Teppo Järvelin91d79382019-10-02 09:09:31 +0300554 mbedtls_platform_memcpy( dst->peer_cert_digest, src->peer_cert_digest,
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000555 src->peer_cert_digest_len );
556 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000557 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000558 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100559#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200562
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200563#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200564 if( src->ticket != NULL )
565 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200566 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200567 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200568 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200569
Teppo Järvelin91d79382019-10-02 09:09:31 +0300570 mbedtls_platform_memcpy( dst->ticket, src->ticket, src->ticket_len );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200571 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200572#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200573
574 return( 0 );
575}
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
578int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200579 const unsigned char *key_enc, const unsigned char *key_dec,
580 size_t keylen,
581 const unsigned char *iv_enc, const unsigned char *iv_dec,
582 size_t ivlen,
583 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200584 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
586int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
587int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
588int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
589int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
590#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000591
Paul Bakker5121ce52009-01-03 21:22:43 +0000592/*
593 * Key material generation
594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100596MBEDTLS_NO_INLINE static int ssl3_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200597 const char *label,
598 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000599 unsigned char *dstbuf, size_t dlen )
600{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100601 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000602 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200603 mbedtls_md5_context md5;
604 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000605 unsigned char padding[16];
606 unsigned char sha1sum[20];
607 ((void)label);
608
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200609 mbedtls_md5_init( &md5 );
610 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200611
Paul Bakker5f70b252012-09-13 14:23:06 +0000612 /*
613 * SSLv3:
614 * block =
615 * MD5( secret + SHA1( 'A' + secret + random ) ) +
616 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
617 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
618 * ...
619 */
620 for( i = 0; i < dlen / 16; i++ )
621 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200622 mbedtls_platform_memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000623
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100624 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100625 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100626 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100627 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100628 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100629 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100630 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100631 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100632 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100633 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000634
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100635 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100636 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100637 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100638 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100639 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100640 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100641 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100642 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000643 }
644
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100645exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200646 mbedtls_md5_free( &md5 );
647 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000648
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500649 mbedtls_platform_zeroize( padding, sizeof( padding ) );
650 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000651
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100652 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000653}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100657MBEDTLS_NO_INLINE static int tls1_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200658 const char *label,
659 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000660 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000661{
Paul Bakker23986e52011-04-24 08:57:21 +0000662 size_t nb, hs;
663 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200664 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 unsigned char tmp[128];
666 unsigned char h_i[20];
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100667 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100669 int ret;
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
673 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
676 hs = ( slen + 1 ) / 2;
677 S1 = secret;
678 S2 = secret + slen - hs;
679
680 nb = strlen( label );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300681 mbedtls_platform_memcpy( tmp + 20, label, nb );
682 mbedtls_platform_memcpy( tmp + 20 + nb, random, rlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 nb += rlen;
684
685 /*
686 * First compute P_md5(secret,label+random)[0..dlen]
687 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100688 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) ==
689 MBEDTLS_MD_INVALID_HANDLE )
690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100692 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100695 return( ret );
696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
698 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
699 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000700
701 for( i = 0; i < dlen; i += 16 )
702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_md_hmac_reset ( &md_ctx );
704 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
705 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_md_hmac_reset ( &md_ctx );
708 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
709 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
711 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
712
713 for( j = 0; j < k; j++ )
714 dstbuf[i + j] = h_i[j];
715 }
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100718
Paul Bakker5121ce52009-01-03 21:22:43 +0000719 /*
720 * XOR out with P_sha1(secret,label+random)[0..dlen]
721 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100722 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) ==
723 MBEDTLS_MD_INVALID_HANDLE )
724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100726 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100729 return( ret );
730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
732 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
733 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
735 for( i = 0; i < dlen; i += 20 )
736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_md_hmac_reset ( &md_ctx );
738 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
739 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_md_hmac_reset ( &md_ctx );
742 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
743 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000744
745 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
746
747 for( j = 0; j < k; j++ )
748 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
749 }
750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100752
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500753 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
754 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
756 return( 0 );
757}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerf6cc7422019-08-16 14:34:52 +0100761#if !( defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_SHA512_C) )
762MBEDTLS_ALWAYS_INLINE static inline
763#else
764static
765#endif
766int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100767 const unsigned char *secret, size_t slen,
768 const char *label,
769 const unsigned char *random, size_t rlen,
770 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000771{
772 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100773 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000774 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100776 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100778 int ret;
779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000781
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100782 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) ==
783 MBEDTLS_MD_INVALID_HANDLE )
784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100786 }
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100789
790 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000792
793 nb = strlen( label );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300794 mbedtls_platform_memcpy( tmp + md_len, label, nb );
795 mbedtls_platform_memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000796 nb += rlen;
797
798 /*
799 * Compute P_<hash>(secret, label + random)[0..dlen]
800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100802 return( ret );
803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
805 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
806 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100807
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100808 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 mbedtls_md_hmac_reset ( &md_ctx );
811 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
812 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 mbedtls_md_hmac_reset ( &md_ctx );
815 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
816 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000817
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100818 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000819
820 for( j = 0; j < k; j++ )
821 dstbuf[i + j] = h_i[j];
822 }
823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100825
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500826 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
827 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000828
829 return( 0 );
830}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100833MBEDTLS_NO_INLINE static int tls_prf_sha256(
834 const unsigned char *secret, size_t slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100835 const char *label,
836 const unsigned char *random, size_t rlen,
837 unsigned char *dstbuf, size_t dlen )
838{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100840 label, random, rlen, dstbuf, dlen ) );
841}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100845MBEDTLS_NO_INLINE static int tls_prf_sha384(
846 const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200847 const char *label,
848 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000849 unsigned char *dstbuf, size_t dlen )
850{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100852 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000853}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854#endif /* MBEDTLS_SHA512_C */
855#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000856
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100857/*
858 * Call the appropriate PRF function
859 */
Hanno Becker2793f742019-08-16 14:28:43 +0100860MBEDTLS_ALWAYS_INLINE static inline int ssl_prf( int minor_ver,
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100861 mbedtls_md_type_t hash,
862 const unsigned char *secret, size_t slen,
863 const char *label,
864 const unsigned char *random, size_t rlen,
865 unsigned char *dstbuf, size_t dlen )
866{
867#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
868 (void) hash;
869#endif
870
871#if defined(MBEDTLS_SSL_PROTO_SSL3)
872 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
873 return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
874 else
875#endif
876#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +0100877 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100878 return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
879 else
880#endif
881#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
882#if defined(MBEDTLS_SHA512_C)
883 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
884 hash == MBEDTLS_MD_SHA384 )
885 {
886 return( tls_prf_sha384( secret, slen, label, random, rlen,
887 dstbuf, dlen ) );
888 }
889 else
890#endif
891#if defined(MBEDTLS_SHA256_C)
892 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
893 {
894 return( tls_prf_sha256( secret, slen, label, random, rlen,
895 dstbuf, dlen ) );
896 }
897#endif
898#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
899
900 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
901}
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200902
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100903#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100904MBEDTLS_NO_INLINE static void ssl_calc_finished_ssl(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100905 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
906{
907 const char *sender;
908 mbedtls_md5_context md5;
909 mbedtls_sha1_context sha1;
910
911 unsigned char padbuf[48];
912 unsigned char md5sum[16];
913 unsigned char sha1sum[20];
914
915 mbedtls_ssl_session *session = ssl->session_negotiate;
916 if( !session )
917 session = ssl->session;
918
919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
920
921 mbedtls_md5_init( &md5 );
922 mbedtls_sha1_init( &sha1 );
923
924 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
925 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
926
927 /*
928 * SSLv3:
929 * hash =
930 * MD5( master + pad2 +
931 * MD5( handshake + sender + master + pad1 ) )
932 * + SHA1( master + pad2 +
933 * SHA1( handshake + sender + master + pad1 ) )
934 */
935
936#if !defined(MBEDTLS_MD5_ALT)
937 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
938 md5.state, sizeof( md5.state ) );
939#endif
940
941#if !defined(MBEDTLS_SHA1_ALT)
942 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
943 sha1.state, sizeof( sha1.state ) );
944#endif
945
946 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
947 : "SRVR";
948
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200949 mbedtls_platform_memset( padbuf, 0x36, 48 );
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100950
951 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
952 mbedtls_md5_update_ret( &md5, session->master, 48 );
953 mbedtls_md5_update_ret( &md5, padbuf, 48 );
954 mbedtls_md5_finish_ret( &md5, md5sum );
955
956 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
957 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
958 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
959 mbedtls_sha1_finish_ret( &sha1, sha1sum );
960
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200961 mbedtls_platform_memset( padbuf, 0x5C, 48 );
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100962
963 mbedtls_md5_starts_ret( &md5 );
964 mbedtls_md5_update_ret( &md5, session->master, 48 );
965 mbedtls_md5_update_ret( &md5, padbuf, 48 );
966 mbedtls_md5_update_ret( &md5, md5sum, 16 );
967 mbedtls_md5_finish_ret( &md5, buf );
968
969 mbedtls_sha1_starts_ret( &sha1 );
970 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
971 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
972 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
973 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
974
975 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
976
977 mbedtls_md5_free( &md5 );
978 mbedtls_sha1_free( &sha1 );
979
980 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
981 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
982 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
983
984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
985}
986#endif /* MBEDTLS_SSL_PROTO_SSL3 */
987
988#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100989MBEDTLS_NO_INLINE static void ssl_calc_finished_tls(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100990 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
991{
992 int len = 12;
993 const char *sender;
994 mbedtls_md5_context md5;
995 mbedtls_sha1_context sha1;
996 unsigned char padbuf[36];
997
998 mbedtls_ssl_session *session = ssl->session_negotiate;
999 if( !session )
1000 session = ssl->session;
1001
1002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
1003
1004 mbedtls_md5_init( &md5 );
1005 mbedtls_sha1_init( &sha1 );
1006
1007 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1008 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1009
1010 /*
1011 * TLSv1:
1012 * hash = PRF( master, finished_label,
1013 * MD5( handshake ) + SHA1( handshake ) )[0..11]
1014 */
1015
1016#if !defined(MBEDTLS_MD5_ALT)
1017 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1018 md5.state, sizeof( md5.state ) );
1019#endif
1020
1021#if !defined(MBEDTLS_SHA1_ALT)
1022 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1023 sha1.state, sizeof( sha1.state ) );
1024#endif
1025
1026 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1027 ? "client finished"
1028 : "server finished";
1029
1030 mbedtls_md5_finish_ret( &md5, padbuf );
1031 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
1032
1033 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1034 mbedtls_ssl_suite_get_mac(
1035 mbedtls_ssl_ciphersuite_from_id(
1036 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1037 session->master, 48, sender,
1038 padbuf, 36, buf, len );
1039
1040 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1041
1042 mbedtls_md5_free( &md5 );
1043 mbedtls_sha1_free( &sha1 );
1044
1045 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1046
1047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1048}
1049#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1050
1051#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1052#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001053MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha256(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001054 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1055{
1056 int len = 12;
1057 const char *sender;
1058 mbedtls_sha256_context sha256;
1059 unsigned char padbuf[32];
1060
1061 mbedtls_ssl_session *session = ssl->session_negotiate;
1062 if( !session )
1063 session = ssl->session;
1064
1065 mbedtls_sha256_init( &sha256 );
1066
1067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
1068
1069 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1070
1071 /*
1072 * TLSv1.2:
1073 * hash = PRF( master, finished_label,
1074 * Hash( handshake ) )[0.11]
1075 */
1076
1077#if !defined(MBEDTLS_SHA256_ALT)
1078 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
1079 sha256.state, sizeof( sha256.state ) );
1080#endif
1081
1082 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1083 ? "client finished"
1084 : "server finished";
1085
1086 mbedtls_sha256_finish_ret( &sha256, padbuf );
1087
1088 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1089 mbedtls_ssl_suite_get_mac(
1090 mbedtls_ssl_ciphersuite_from_id(
1091 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1092 session->master, 48, sender,
1093 padbuf, 32, buf, len );
1094
1095 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1096
1097 mbedtls_sha256_free( &sha256 );
1098
1099 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1100
1101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1102}
1103#endif /* MBEDTLS_SHA256_C */
1104
1105#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001106MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha384(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001107 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1108{
1109 int len = 12;
1110 const char *sender;
1111 mbedtls_sha512_context sha512;
1112 unsigned char padbuf[48];
1113
1114 mbedtls_ssl_session *session = ssl->session_negotiate;
1115 if( !session )
1116 session = ssl->session;
1117
1118 mbedtls_sha512_init( &sha512 );
1119
1120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
1121
1122 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1123
1124 /*
1125 * TLSv1.2:
1126 * hash = PRF( master, finished_label,
1127 * Hash( handshake ) )[0.11]
1128 */
1129
1130#if !defined(MBEDTLS_SHA512_ALT)
1131 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
1132 sha512.state, sizeof( sha512.state ) );
1133#endif
1134
1135 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1136 ? "client finished"
1137 : "server finished";
1138
1139 mbedtls_sha512_finish_ret( &sha512, padbuf );
1140
1141 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1142 mbedtls_ssl_suite_get_mac(
1143 mbedtls_ssl_ciphersuite_from_id(
1144 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1145 session->master, 48, sender,
1146 padbuf, 48, buf, len );
1147
1148 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1149
1150 mbedtls_sha512_free( &sha512 );
1151
1152 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1153
1154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1155}
1156#endif /* MBEDTLS_SHA512_C */
1157#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1158
Hanno Becker2793f742019-08-16 14:28:43 +01001159MBEDTLS_ALWAYS_INLINE static inline int ssl_calc_finished(
1160 int minor_ver,
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001161 mbedtls_md_type_t hash,
1162 mbedtls_ssl_context *ssl,
1163 unsigned char *buf,
1164 int from )
1165{
1166#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1167 (void) hash;
1168#endif
1169
1170#if defined(MBEDTLS_SSL_PROTO_SSL3)
1171 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1172 ssl_calc_finished_ssl( ssl, buf, from );
1173 else
1174#endif
1175#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001176 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001177 ssl_calc_finished_tls( ssl, buf, from );
1178 else
1179#endif
1180#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1181#if defined(MBEDTLS_SHA512_C)
1182 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1183 hash == MBEDTLS_MD_SHA384 )
1184 {
1185 ssl_calc_finished_tls_sha384( ssl, buf, from );
1186 }
1187 else
1188#endif
1189#if defined(MBEDTLS_SHA256_C)
1190 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1191 ssl_calc_finished_tls_sha256( ssl, buf, from );
1192 else
1193#endif
1194#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1195 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1196
1197 return( 0 );
1198}
1199
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001200/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001201 * Populate a transform structure with session keys and all the other
1202 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001203 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001204 * Parameters:
1205 * - [in/out]: transform: structure to populate
1206 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001207 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001208 * - [in] ciphersuite
1209 * - [in] master
1210 * - [in] encrypt_then_mac
1211 * - [in] trunc_hmac
1212 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001213 * - [in] tls_prf: pointer to PRF to use for key derivation
1214 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001215 * - [in] minor_ver: SSL/TLS minor version
1216 * - [in] endpoint: client or server
1217 * - [in] ssl: optionally used for:
1218 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1219 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1220 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001221 */
Hanno Becker298a4702019-08-16 10:21:32 +01001222/* Force compilers to inline this function if it's used only
1223 * from one place, because at least ARMC5 doesn't do that
1224 * automatically. */
1225#if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1226MBEDTLS_ALWAYS_INLINE static inline
1227#else
1228static
1229#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1230int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001231 int ciphersuite,
1232 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001233#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001234#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1235 int encrypt_then_mac,
1236#endif
1237#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1238 int trunc_hmac,
1239#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001240#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001241#if defined(MBEDTLS_ZLIB_SUPPORT)
1242 int compression,
1243#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001244 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001245 int minor_ver,
1246 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001247 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001248{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001249 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 unsigned char keyblk[256];
1251 unsigned char *key1;
1252 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001253 unsigned char *mac_enc;
1254 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001255 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001256 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001257 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001258 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 const mbedtls_cipher_info_t *cipher_info;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001260 mbedtls_md_handle_t md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001261
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001262#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1263 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1264 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001265 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001266 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001267#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001268
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001269 /*
1270 * Some data just needs copying into the structure
1271 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001272#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1273 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001274 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001275#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001276
1277#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001278 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001279#else
1280 ((void) minor_ver);
1281#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001282
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001283#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Teppo Järvelin91d79382019-10-02 09:09:31 +03001284 mbedtls_platform_memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001285#endif
1286
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001287 /*
1288 * Get various info structures
1289 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001290 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001291 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001292 {
1293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001294 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001295 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1296 }
1297
Hanno Becker473f98f2019-06-26 10:27:32 +01001298 cipher_info = mbedtls_cipher_info_from_type(
1299 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001300 if( cipher_info == NULL )
1301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001303 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001305 }
1306
Hanno Becker473f98f2019-06-26 10:27:32 +01001307 md_info = mbedtls_md_info_from_type(
1308 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001309 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Paul Bakker68884e32013-01-07 18:20:04 +01001310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001312 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001314 }
1315
Hanno Beckera5a2b082019-05-15 14:03:01 +01001316#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001317 /* Copy own and peer's CID if the use of the CID
1318 * extension has been negotiated. */
1319 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1320 {
1321 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001322
Hanno Becker4932f9f2019-05-03 15:23:51 +01001323 transform->in_cid_len = ssl->own_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001324 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1325 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001326 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001327 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001328
1329 transform->out_cid_len = ssl->handshake->peer_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001330 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1331 memcpy( transform->out_cid, ssl->handshake->peer_cid,
Hanno Beckere582d122019-05-15 10:21:55 +01001332 ssl->handshake->peer_cid_len );
1333 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1334 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001335 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001336#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001337
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001339 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001340 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001341 ret = ssl_prf( minor_ver,
1342 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1343 master, 48, "key expansion", randbytes, 64,
1344 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001345 if( ret != 0 )
1346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001348 return( ret );
1349 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001352 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001353 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001354 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001356
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 /*
1358 * Determine the appropriate key, IV and MAC length.
1359 */
Paul Bakker68884e32013-01-07 18:20:04 +01001360
Hanno Beckere7f2df02017-12-27 08:17:40 +00001361 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001362
Hanno Beckerf1229442018-01-03 15:32:31 +00001363#if defined(MBEDTLS_GCM_C) || \
1364 defined(MBEDTLS_CCM_C) || \
1365 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001367 cipher_info->mode == MBEDTLS_MODE_CCM ||
1368 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001370 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001371 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001372 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1373 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001374
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001375 /* All modes haves 96-bit IVs;
1376 * GCM and CCM has 4 implicit and 8 explicit bytes
1377 * ChachaPoly has all 12 bytes implicit
1378 */
Paul Bakker68884e32013-01-07 18:20:04 +01001379 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001380 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1381 transform->fixed_ivlen = 12;
1382 else
1383 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001384 }
1385 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001386#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1387#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1388 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1389 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001390 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001391 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1393 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001396 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001397 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001399 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001400 mac_key_len = mbedtls_md_get_size( md_info );
1401 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001404 /*
1405 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1406 * (rfc 6066 page 13 or rfc 2104 section 4),
1407 * so we only need to adjust the length here.
1408 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001409 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001412
1413#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1414 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001415 * HMAC implementation which also truncates the key
1416 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001417 mac_key_len = transform->maclen;
1418#endif
1419 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001421
1422 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001423 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001425 else
1426#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1427 {
1428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1429 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1430 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001431
Hanno Beckera9d5c452019-07-25 16:47:12 +01001432 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001433 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001434 (unsigned) transform->ivlen,
1435 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001436
1437 /*
1438 * Finally setup the cipher contexts, IVs and MAC secrets.
1439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001441 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001443 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001444 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001445
Paul Bakker68884e32013-01-07 18:20:04 +01001446 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001447 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001448
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001449 /*
1450 * This is not used in TLS v1.1.
1451 */
Paul Bakker48916f92012-09-16 19:57:18 +00001452 iv_copy_len = ( transform->fixed_ivlen ) ?
1453 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001454 mbedtls_platform_memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1455 mbedtls_platform_memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001456 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001457 }
1458 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#endif /* MBEDTLS_SSL_CLI_C */
1460#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001461 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001463 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001464 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001465
Hanno Becker81c7b182017-11-09 18:39:33 +00001466 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001467 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001468
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001469 /*
1470 * This is not used in TLS v1.1.
1471 */
Paul Bakker48916f92012-09-16 19:57:18 +00001472 iv_copy_len = ( transform->fixed_ivlen ) ?
1473 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001474 mbedtls_platform_memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1475 mbedtls_platform_memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001476 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001477 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001478 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1482 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001483 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001484
Hanno Becker92231322018-01-03 15:32:51 +00001485#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001487 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001488 {
Hanno Becker92231322018-01-03 15:32:51 +00001489 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1492 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001493 }
1494
Teppo Järvelin91d79382019-10-02 09:09:31 +03001495 mbedtls_platform_memcpy( transform->mac_enc, mac_enc, mac_key_len );
1496 mbedtls_platform_memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001497 }
1498 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1500#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1501 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001502 if( mbedtls_ssl_ver_geq( minor_ver, MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakker68884e32013-01-07 18:20:04 +01001503 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001504 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1505 For AEAD-based ciphersuites, there is nothing to do here. */
1506 if( mac_key_len != 0 )
1507 {
1508 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1509 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1510 }
Paul Bakker68884e32013-01-07 18:20:04 +01001511 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001512 else
1513#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1516 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001517 }
Hanno Becker92231322018-01-03 15:32:51 +00001518#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1521 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001522 {
1523 int ret = 0;
1524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001526
Hanno Beckere7f2df02017-12-27 08:17:40 +00001527 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001528 transform->iv_enc, transform->iv_dec,
1529 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001530 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001531 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1534 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001535 }
1536 }
Hanno Becker92231322018-01-03 15:32:51 +00001537#else
1538 ((void) mac_dec);
1539 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001541
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001542#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1543 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001544 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001545 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001546 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001547 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001548 iv_copy_len );
1549 }
1550#endif
1551
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001552 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001553 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001554 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001556 return( ret );
1557 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001558
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001559 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001560 cipher_info ) ) != 0 )
1561 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001562 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001563 return( ret );
1564 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001567 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001571 return( ret );
1572 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001575 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001579 return( ret );
1580 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#if defined(MBEDTLS_CIPHER_MODE_CBC)
1583 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1586 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001589 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001590 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1593 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001596 return( ret );
1597 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001598 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001600
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001601 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001602
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001603 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001605 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001608
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001609 mbedtls_platform_memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1610 mbedtls_platform_memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001611
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001612 if( deflateInit( &transform->ctx_deflate,
1613 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001614 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1617 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001618 }
1619 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001621
Paul Bakker5121ce52009-01-03 21:22:43 +00001622 return( 0 );
1623}
1624
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001625#if defined(MBEDTLS_SSL_PROTO_SSL3)
1626static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1627 unsigned char hash[36],
1628 size_t *hlen )
1629{
1630 mbedtls_md5_context md5;
1631 mbedtls_sha1_context sha1;
1632 unsigned char pad_1[48];
1633 unsigned char pad_2[48];
1634
1635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1636
1637 mbedtls_md5_init( &md5 );
1638 mbedtls_sha1_init( &sha1 );
1639
1640 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1641 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1642
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001643 mbedtls_platform_memset( pad_1, 0x36, 48 );
1644 mbedtls_platform_memset( pad_2, 0x5C, 48 );
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001645
1646 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1647 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1648 mbedtls_md5_finish_ret( &md5, hash );
1649
1650 mbedtls_md5_starts_ret( &md5 );
1651 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1652 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1653 mbedtls_md5_update_ret( &md5, hash, 16 );
1654 mbedtls_md5_finish_ret( &md5, hash );
1655
1656 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1657 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1658 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1659
1660 mbedtls_sha1_starts_ret( &sha1 );
1661 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1662 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1663 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1664 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1665
1666 *hlen = 36;
1667
1668 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1670
1671 mbedtls_md5_free( &md5 );
1672 mbedtls_sha1_free( &sha1 );
1673
1674 return;
1675}
1676#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1677
1678#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1679static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1680 unsigned char hash[36],
1681 size_t *hlen )
1682{
1683 mbedtls_md5_context md5;
1684 mbedtls_sha1_context sha1;
1685
1686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1687
1688 mbedtls_md5_init( &md5 );
1689 mbedtls_sha1_init( &sha1 );
1690
1691 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1692 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1693
1694 mbedtls_md5_finish_ret( &md5, hash );
1695 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1696
1697 *hlen = 36;
1698
1699 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1701
1702 mbedtls_md5_free( &md5 );
1703 mbedtls_sha1_free( &sha1 );
1704
1705 return;
1706}
1707#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1708
1709#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1710#if defined(MBEDTLS_SHA256_C)
1711static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1712 unsigned char hash[32],
1713 size_t *hlen )
1714{
1715 mbedtls_sha256_context sha256;
1716
1717 mbedtls_sha256_init( &sha256 );
1718
1719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1720
1721 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1722 mbedtls_sha256_finish_ret( &sha256, hash );
1723
1724 *hlen = 32;
1725
1726 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1728
1729 mbedtls_sha256_free( &sha256 );
1730
1731 return;
1732}
1733#endif /* MBEDTLS_SHA256_C */
1734
1735#if defined(MBEDTLS_SHA512_C)
1736static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1737 unsigned char hash[48],
1738 size_t *hlen )
1739{
1740 mbedtls_sha512_context sha512;
1741
1742 mbedtls_sha512_init( &sha512 );
1743
1744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1745
1746 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1747 mbedtls_sha512_finish_ret( &sha512, hash );
1748
1749 *hlen = 48;
1750
1751 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1753
1754 mbedtls_sha512_free( &sha512 );
1755
1756 return;
1757}
1758#endif /* MBEDTLS_SHA512_C */
1759#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1760
Hanno Becker2f41b242019-08-15 17:29:43 +01001761int mbedtls_ssl_calc_verify( int minor_ver,
1762 mbedtls_md_type_t hash,
1763 mbedtls_ssl_context const *ssl,
1764 unsigned char *dst,
1765 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001766{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001767#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1768 (void) hash;
1769#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001770
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001771#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001772 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001773 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001774 else
1775#endif
1776#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001777 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker2f41b242019-08-15 17:29:43 +01001778 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001779 else
1780#endif
1781#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1782#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001783 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1784 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001785 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001786 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001787 }
1788 else
1789#endif
1790#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001791 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001792 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001793 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001794 }
1795 else
1796#endif
1797#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1798 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001799 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1800 }
1801
1802 return( 0 );
1803}
1804
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001805/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001806 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001807 *
1808 * Parameters:
1809 * [in/out] handshake
1810 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1811 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001812 * [out] master
1813 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001814 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001815static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001816 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001817 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001818{
1819 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001820
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001821/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1822/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1823/* (void) ssl; */
1824/* #endif */
1825
1826 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1827 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001828
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001829#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02001830 if( handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001831 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001832 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1833 return( 0 );
1834 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001835#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001836
1837 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1838 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001839
1840#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001841 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1842 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001843 {
1844 unsigned char session_hash[48];
1845 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001846
Hanno Becker2f41b242019-08-15 17:29:43 +01001847 mbedtls_ssl_calc_verify(
1848 mbedtls_ssl_get_minor_ver( ssl ),
1849 mbedtls_ssl_suite_get_mac( ciphersuite ),
1850 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001851
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001852 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1853 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001854
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001855 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1856 mbedtls_ssl_suite_get_mac( ciphersuite ),
1857 handshake->premaster, handshake->pmslen,
1858 "extended master secret",
1859 session_hash, hash_len,
1860 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001861 }
1862 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001863#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001864 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001865 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1866 mbedtls_ssl_suite_get_mac( ciphersuite ),
1867 handshake->premaster, handshake->pmslen,
1868 "master secret",
1869 handshake->randbytes, 64,
1870 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001871 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001872 if( ret != 0 )
1873 {
1874 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1875 return( ret );
1876 }
1877
1878 mbedtls_platform_zeroize( handshake->premaster,
1879 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001880
1881 return( 0 );
1882}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001883
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001884int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1885{
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02001886 volatile int ret;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001887
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001889 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_UNSET;
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001890 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001891 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001892 ssl->session_negotiate->master,
1893 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001894 if( ret != 0 )
1895 {
1896 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1897 return( ret );
1898 }
1899
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001900 /* Swap the client and server random values:
1901 * - MS derivation wanted client+server (RFC 5246 8.1)
1902 * - key derivation wants server+client (RFC 5246 6.3) */
1903 {
1904 unsigned char tmp[64];
Teppo Järvelin91d79382019-10-02 09:09:31 +03001905 mbedtls_platform_memcpy( tmp, ssl->handshake->randbytes, 64 );
1906 mbedtls_platform_memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1907 mbedtls_platform_memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001908 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1909 }
1910
1911 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001912 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001913 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1914 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001915#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001916#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001917 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001918#endif
1919#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001920 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001921#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001922#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001923#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001924 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001925#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001926 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001927 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001928 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1929 ssl );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001930 if( ret == 0 )
1931 {
1932 mbedtls_platform_enforce_volatile_reads();
1933 if( ret == 0 )
1934 {
1935 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_SET;
1936 }
1937 else
1938 {
1939 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
1940 }
1941 }
1942 else
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001943 {
1944 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1945 return( ret );
1946 }
1947
1948 /* We no longer need Server/ClientHello.random values */
1949 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1950 sizeof( ssl->handshake->randbytes ) );
1951
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001952 /* Allocate compression buffer */
1953#if defined(MBEDTLS_ZLIB_SUPPORT)
1954 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1955 ssl->compress_buf == NULL )
1956 {
1957 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1958 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1959 if( ssl->compress_buf == NULL )
1960 {
1961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001962 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001963 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1964 }
1965 }
1966#endif
1967
Paul Bakker5121ce52009-01-03 21:22:43 +00001968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1969
1970 return( 0 );
1971}
1972
Hanno Becker09d23642019-07-22 17:18:18 +01001973int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1974{
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02001975 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker09d23642019-07-22 17:18:18 +01001976
1977 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1978 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Beckera3c2c172019-07-23 16:51:57 +01001979#if defined(MBEDTLS_USE_TINYCRYPT)
1980 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001981 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Hanno Beckera3c2c172019-07-23 16:51:57 +01001982 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001983 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1984 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1985 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1986 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1987 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001988 {
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01001989 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1990 ssl->handshake->ecdh_privkey,
1991 ssl->handshake->premaster );
1992 if( ret == UECC_FAULT_DETECTED )
1993 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
1994 if( ret != UECC_SUCCESS )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001995 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02001996 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Hanno Beckera3c2c172019-07-23 16:51:57 +01001997
1998 ssl->handshake->pmslen = NUM_ECC_BYTES;
1999 }
2000 else
2001#endif
Hanno Becker09d23642019-07-22 17:18:18 +01002002#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2003 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2004 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
2005 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002006 ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002007 ssl->handshake->premaster,
2008 MBEDTLS_PREMASTER_SIZE,
2009 &ssl->handshake->pmslen,
2010 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002011 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2012 if( ret == 0 )
2013 {
2014 mbedtls_platform_enforce_volatile_reads();
2015 if( ret == 0 )
2016 {
2017 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2018 }
2019 else
2020 {
2021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2022 return( ret );
2023 }
2024 }
2025 else
Hanno Becker09d23642019-07-22 17:18:18 +01002026 {
2027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2028 return( ret );
2029 }
2030
2031 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2032 }
2033 else
2034#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01002035#if defined(MBEDTLS_ECDH_C) && \
2036 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2037 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2038 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2039 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002040 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2041 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2042 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2043 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2044 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2045 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2046 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2047 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2048 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002049 ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002050 &ssl->handshake->pmslen,
2051 ssl->handshake->premaster,
2052 MBEDTLS_MPI_MAX_SIZE,
2053 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002054 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2055 if( ret == 0 )
2056 {
2057 mbedtls_platform_enforce_volatile_reads();
2058 if( ret == 0 )
2059 {
2060 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2061 }
2062 else
2063 {
2064 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2065 return( ret );
2066 }
2067 }
2068 else
Hanno Becker09d23642019-07-22 17:18:18 +01002069 {
2070 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2071 return( ret );
2072 }
2073
2074 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2075 }
2076 else
2077#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2078 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2079 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2080 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2081#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2082 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2083 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002084 ret = mbedtls_ssl_psk_derive_premaster( ssl,
2085 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) );
2086 if( ret == 0 )
2087 {
2088 mbedtls_platform_enforce_volatile_reads();
2089 if( ret == 0 )
2090 {
2091 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2092 }
2093 else
2094 {
2095 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2096 return( ret );
2097 }
2098 }
2099 else
Hanno Becker09d23642019-07-22 17:18:18 +01002100 {
2101 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2102 return( ret );
2103 }
2104 }
2105 else
2106#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2107#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2108 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2109 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2110 {
2111 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2112 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2113 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002114 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002115 if( ret == 0 )
2116 {
2117 mbedtls_platform_enforce_volatile_reads();
2118 if( ret == 0 )
2119 {
2120 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2121 }
2122 else
2123 {
2124 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2125 return( ret );
2126 }
2127 }
2128 else
Hanno Becker09d23642019-07-22 17:18:18 +01002129 {
2130 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2131 return( ret );
2132 }
2133 }
2134 else
2135#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2136#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2137 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2138 == MBEDTLS_KEY_EXCHANGE_RSA )
2139 {
2140 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002141 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002142 * ssl_rsa_generate_partial_pms(). Only the
2143 * PMS length needs to be set. */
2144 ssl->handshake->pmslen = 48;
2145 }
2146 else
2147#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2148 {
2149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2150 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2151 }
2152
2153 return( 0 );
2154}
2155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2157int 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 +02002158{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002159 unsigned char *p = ssl->handshake->premaster;
2160 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002161 const unsigned char *psk = ssl->conf->psk;
2162 size_t psk_len = ssl->conf->psk_len;
2163
2164 /* If the psk callback was called, use its result */
2165 if( ssl->handshake->psk != NULL )
2166 {
2167 psk = ssl->handshake->psk;
2168 psk_len = ssl->handshake->psk_len;
2169 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002170
2171 /*
2172 * PMS = struct {
2173 * opaque other_secret<0..2^16-1>;
2174 * opaque psk<0..2^16-1>;
2175 * };
2176 * with "other_secret" depending on the particular key exchange
2177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2179 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002180 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002181 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002183
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002184 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002185
2186 if( end < p || (size_t)( end - p ) < psk_len )
2187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2188
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002189 mbedtls_platform_memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002190 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002191 }
2192 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2194#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2195 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002196 {
2197 /*
2198 * other_secret already set by the ClientKeyExchange message,
2199 * and is 48 bytes long
2200 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002201 if( end - p < 2 )
2202 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2203
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002204 *p++ = 0;
2205 *p++ = 48;
2206 p += 48;
2207 }
2208 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2210#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2211 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002212 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002213 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002214 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002215
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002216 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002218 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002219 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002220 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002223 return( ret );
2224 }
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002225 p = mbedtls_platform_put_uint16_be( p, len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002226 p += len;
2227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002229 }
2230 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2232#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2233 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002234 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002235 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002236 size_t zlen;
2237
Hanno Becker982da7e2019-09-02 09:47:39 +01002238#if defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01002239 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2240 ssl->handshake->ecdh_privkey,
2241 p + 2 );
2242 if( ret == UECC_FAULT_DETECTED )
2243 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2244 if( ret != UECC_SUCCESS )
Hanno Becker982da7e2019-09-02 09:47:39 +01002245 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Hanno Becker982da7e2019-09-02 09:47:39 +01002246
2247 zlen = NUM_ECC_BYTES;
2248#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002250 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002251 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002252 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002255 return( ret );
2256 }
2257
Hanno Becker982da7e2019-09-02 09:47:39 +01002258 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2259 MBEDTLS_DEBUG_ECDH_Z );
2260#endif /* MBEDTLS_USE_TINYCRYPT */
2261
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002262 p = mbedtls_platform_put_uint16_be( p, zlen );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002263 p += zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002264 }
2265 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2269 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002270 }
2271
2272 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002273 if( end - p < 2 )
2274 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002275
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002276 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002277
2278 if( end < p || (size_t)( end - p ) < psk_len )
2279 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2280
Teppo Järvelin91d79382019-10-02 09:09:31 +03002281 mbedtls_platform_memcpy( p, psk, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002282 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002283
2284 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2285
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002286 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2287
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002288 return( 0 );
2289}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002293/*
2294 * SSLv3.0 MAC functions
2295 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002296#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002297static void ssl_mac( mbedtls_md_context_t *md_ctx,
2298 const unsigned char *secret,
2299 const unsigned char *buf, size_t len,
2300 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002301 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002302{
2303 unsigned char header[11];
2304 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002305 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2307 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002308
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002309 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002311 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002312 else
Paul Bakker68884e32013-01-07 18:20:04 +01002313 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002314
Teppo Järvelin91d79382019-10-02 09:09:31 +03002315 mbedtls_platform_memcpy( header, ctr, 8 );
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03002316 header[8] = (unsigned char) type;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002317 (void)mbedtls_platform_put_uint16_be( &header[9], len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002318
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002319 mbedtls_platform_memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 mbedtls_md_starts( md_ctx );
2321 mbedtls_md_update( md_ctx, secret, md_size );
2322 mbedtls_md_update( md_ctx, padding, padlen );
2323 mbedtls_md_update( md_ctx, header, 11 );
2324 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002325 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002326
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002327 mbedtls_platform_memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 mbedtls_md_starts( md_ctx );
2329 mbedtls_md_update( md_ctx, secret, md_size );
2330 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002331 mbedtls_md_update( md_ctx, out, md_size );
2332 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002333}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002335
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002336/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002337 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002338#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002339 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2340 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2341 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2342/* This function makes sure every byte in the memory region is accessed
2343 * (in ascending addresses order) */
2344static void ssl_read_memory( unsigned char *p, size_t len )
2345{
2346 unsigned char acc = 0;
2347 volatile unsigned char force;
2348
2349 for( ; len != 0; p++, len-- )
2350 acc ^= *p;
2351
2352 force = acc;
2353 (void) force;
2354}
2355#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2356
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002357/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002358 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002359 */
Hanno Becker3307b532017-12-27 21:37:21 +00002360
Hanno Beckera5a2b082019-05-15 14:03:01 +01002361#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002362/* This functions transforms a DTLS plaintext fragment and a record content
2363 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002364 *
2365 * struct {
2366 * opaque content[DTLSPlaintext.length];
2367 * ContentType real_type;
2368 * uint8 zeros[length_of_padding];
2369 * } DTLSInnerPlaintext;
2370 *
2371 * Input:
2372 * - `content`: The beginning of the buffer holding the
2373 * plaintext to be wrapped.
2374 * - `*content_size`: The length of the plaintext in Bytes.
2375 * - `max_len`: The number of Bytes available starting from
2376 * `content`. This must be `>= *content_size`.
2377 * - `rec_type`: The desired record content type.
2378 *
2379 * Output:
2380 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2381 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2382 *
2383 * Returns:
2384 * - `0` on success.
2385 * - A negative error code if `max_len` didn't offer enough space
2386 * for the expansion.
2387 */
2388static int ssl_cid_build_inner_plaintext( unsigned char *content,
2389 size_t *content_size,
2390 size_t remaining,
2391 uint8_t rec_type )
2392{
2393 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002394 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2395 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2396 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002397
2398 /* Write real content type */
2399 if( remaining == 0 )
2400 return( -1 );
2401 content[ len ] = rec_type;
2402 len++;
2403 remaining--;
2404
2405 if( remaining < pad )
2406 return( -1 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002407 mbedtls_platform_memset( content + len, 0, pad );
Hanno Becker92c930f2019-04-29 17:31:37 +01002408 len += pad;
2409 remaining -= pad;
2410
2411 *content_size = len;
2412 return( 0 );
2413}
2414
Hanno Becker7dc25772019-05-20 15:08:01 +01002415/* This function parses a DTLSInnerPlaintext structure.
2416 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002417static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2418 size_t *content_size,
2419 uint8_t *rec_type )
2420{
2421 size_t remaining = *content_size;
2422
2423 /* Determine length of padding by skipping zeroes from the back. */
2424 do
2425 {
2426 if( remaining == 0 )
2427 return( -1 );
2428 remaining--;
2429 } while( content[ remaining ] == 0 );
2430
2431 *content_size = remaining;
2432 *rec_type = content[ remaining ];
2433
2434 return( 0 );
2435}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002436#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002437
Hanno Becker99abf512019-05-20 14:50:53 +01002438/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002439 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002440static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002441 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002442 mbedtls_record *rec )
2443{
Hanno Becker99abf512019-05-20 14:50:53 +01002444 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002445 *
2446 * additional_data = seq_num + TLSCompressed.type +
2447 * TLSCompressed.version + TLSCompressed.length;
2448 *
Hanno Becker99abf512019-05-20 14:50:53 +01002449 * For the CID extension, this is extended as follows
2450 * (quoting draft-ietf-tls-dtls-connection-id-05,
2451 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002452 *
2453 * additional_data = seq_num + DTLSPlaintext.type +
2454 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002455 * cid +
2456 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002457 * length_of_DTLSInnerPlaintext;
2458 */
2459
Teppo Järvelin91d79382019-10-02 09:09:31 +03002460 mbedtls_platform_memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002461 add_data[8] = rec->type;
Teppo Järvelin91d79382019-10-02 09:09:31 +03002462 mbedtls_platform_memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002463
Hanno Beckera5a2b082019-05-15 14:03:01 +01002464#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002465 if( rec->cid_len != 0 )
2466 {
Teppo Järvelin91d79382019-10-02 09:09:31 +03002467 mbedtls_platform_memcpy( add_data + 11, rec->cid, rec->cid_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002468 add_data[11 + rec->cid_len + 0] = rec->cid_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002469 (void)mbedtls_platform_put_uint16_be( &add_data[11 + rec->cid_len + 1],
2470 rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002471 *add_data_len = 13 + 1 + rec->cid_len;
2472 }
2473 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002474#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002475 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002476 (void)mbedtls_platform_put_uint16_be( &add_data[11], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002477 *add_data_len = 13;
2478 }
Hanno Becker3307b532017-12-27 21:37:21 +00002479}
2480
Hanno Becker611a83b2018-01-03 14:27:32 +00002481int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2482 mbedtls_ssl_transform *transform,
2483 mbedtls_record *rec,
2484 int (*f_rng)(void *, unsigned char *, size_t),
2485 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002486{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002488 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002489 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002490 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002491 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002492 size_t post_avail;
2493
2494 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002495#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002496 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002497 ((void) ssl);
2498#endif
2499
2500 /* The PRNG is used for dynamic IV generation that's used
2501 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2502#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2503 ( defined(MBEDTLS_AES_C) || \
2504 defined(MBEDTLS_ARIA_C) || \
2505 defined(MBEDTLS_CAMELLIA_C) ) && \
2506 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2507 ((void) f_rng);
2508 ((void) p_rng);
2509#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002512
Hanno Becker3307b532017-12-27 21:37:21 +00002513 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002514 {
Hanno Becker3307b532017-12-27 21:37:21 +00002515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2516 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2517 }
Hanno Becker505089d2019-05-01 09:45:57 +01002518 if( rec == NULL
2519 || rec->buf == NULL
2520 || rec->buf_len < rec->data_offset
2521 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002522#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002523 || rec->cid_len != 0
2524#endif
2525 )
Hanno Becker3307b532017-12-27 21:37:21 +00002526 {
2527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002529 }
2530
Hanno Becker3307b532017-12-27 21:37:21 +00002531 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002532 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002534 data, rec->data_len );
2535
2536 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2537
2538 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2539 {
2540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2541 (unsigned) rec->data_len,
2542 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2543 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2544 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002545
Hanno Beckera5a2b082019-05-15 14:03:01 +01002546#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002547 /*
2548 * Add CID information
2549 */
2550 rec->cid_len = transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03002551 /* Not using more secure mbedtls_platform_memcpy as cid is public */
2552 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
Hanno Beckere83efe62019-04-29 13:52:53 +01002553 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002554
2555 if( rec->cid_len != 0 )
2556 {
2557 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002558 * Wrap plaintext into DTLSInnerPlaintext structure.
2559 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002560 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002561 * Note that this changes `rec->data_len`, and hence
2562 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002563 */
2564 if( ssl_cid_build_inner_plaintext( data,
2565 &rec->data_len,
2566 post_avail,
2567 rec->type ) != 0 )
2568 {
2569 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2570 }
2571
2572 rec->type = MBEDTLS_SSL_MSG_CID;
2573 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002574#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002575
Hanno Becker92c930f2019-04-29 17:31:37 +01002576 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2577
Paul Bakker5121ce52009-01-03 21:22:43 +00002578 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002579 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002581#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 if( mode == MBEDTLS_MODE_STREAM ||
2583 ( mode == MBEDTLS_MODE_CBC
2584#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002585 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002586#endif
2587 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 {
Hanno Becker3307b532017-12-27 21:37:21 +00002589 if( post_avail < transform->maclen )
2590 {
2591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2592 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2593 }
2594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002596 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2597 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002598 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002599 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002600 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2601 data, rec->data_len, rec->ctr, rec->type, mac );
Teppo Järvelin91d79382019-10-02 09:09:31 +03002602 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002603 }
2604 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002605#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2607 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002608 if( mbedtls_ssl_ver_geq(
2609 mbedtls_ssl_transform_get_minor_ver( transform ),
2610 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002611 {
Hanno Becker992b6872017-11-09 18:57:39 +00002612 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2613
Hanno Beckere83efe62019-04-29 13:52:53 +01002614 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002615
Hanno Becker3307b532017-12-27 21:37:21 +00002616 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002617 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002618 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2619 data, rec->data_len );
2620 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2621 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2622
Teppo Järvelin91d79382019-10-02 09:09:31 +03002623 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002624 }
2625 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002626#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2629 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002630 }
2631
Hanno Becker3307b532017-12-27 21:37:21 +00002632 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2633 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002634
Hanno Becker3307b532017-12-27 21:37:21 +00002635 rec->data_len += transform->maclen;
2636 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002637 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002638 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002639#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002640
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002641 /*
2642 * Encrypt
2643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2645 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002646 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002647 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002648 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002650 "including %d bytes of padding",
2651 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
Hanno Becker3307b532017-12-27 21:37:21 +00002653 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2654 transform->iv_enc, transform->ivlen,
2655 data, rec->data_len,
2656 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002659 return( ret );
2660 }
2661
Hanno Becker3307b532017-12-27 21:37:21 +00002662 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2665 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002666 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002667 }
Paul Bakker68884e32013-01-07 18:20:04 +01002668 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002670
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002671#if defined(MBEDTLS_GCM_C) || \
2672 defined(MBEDTLS_CCM_C) || \
2673 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002675 mode == MBEDTLS_MODE_CCM ||
2676 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002677 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002678 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002679 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002680 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002681
Hanno Becker3307b532017-12-27 21:37:21 +00002682 /* Check that there's space for both the authentication tag
2683 * and the explicit IV before and after the record content. */
2684 if( post_avail < transform->taglen ||
2685 rec->data_offset < explicit_iv_len )
2686 {
2687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2688 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2689 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002690
Paul Bakker68884e32013-01-07 18:20:04 +01002691 /*
2692 * Generate IV
2693 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002694 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2695 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002696 /* GCM and CCM: fixed || explicit (=seqnum) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002697 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2698 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, rec->ctr,
Hanno Becker3307b532017-12-27 21:37:21 +00002699 explicit_iv_len );
2700 /* Prefix record content with explicit IV. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002701 mbedtls_platform_memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002702 }
2703 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2704 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002705 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002706 unsigned char i;
2707
Teppo Järvelin91d79382019-10-02 09:09:31 +03002708 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002709
2710 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002711 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002712 }
2713 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002714 {
2715 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2717 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002718 }
2719
Hanno Beckere83efe62019-04-29 13:52:53 +01002720 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002721
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002722 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2723 iv, transform->ivlen );
2724 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002725 data - explicit_iv_len, explicit_iv_len );
2726 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002727 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002729 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002730 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002731
Paul Bakker68884e32013-01-07 18:20:04 +01002732 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002733 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002734 */
Hanno Becker3307b532017-12-27 21:37:21 +00002735
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002736 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002737 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002738 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002739 data, rec->data_len, /* source */
2740 data, &rec->data_len, /* destination */
2741 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002744 return( ret );
2745 }
2746
Hanno Becker3307b532017-12-27 21:37:21 +00002747 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2748 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002749
Hanno Becker3307b532017-12-27 21:37:21 +00002750 rec->data_len += transform->taglen + explicit_iv_len;
2751 rec->data_offset -= explicit_iv_len;
2752 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002753 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002754 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002755 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2757#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002758 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002760 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002761 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002762 size_t padlen, i;
2763 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002764
Hanno Becker3307b532017-12-27 21:37:21 +00002765 /* Currently we're always using minimal padding
2766 * (up to 255 bytes would be allowed). */
2767 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2768 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002769 padlen = 0;
2770
Hanno Becker3307b532017-12-27 21:37:21 +00002771 /* Check there's enough space in the buffer for the padding. */
2772 if( post_avail < padlen + 1 )
2773 {
2774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2775 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2776 }
2777
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002779 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002780
Hanno Becker3307b532017-12-27 21:37:21 +00002781 rec->data_len += padlen + 1;
2782 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002785 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002786 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2787 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002788 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002789 if( mbedtls_ssl_ver_geq(
2790 mbedtls_ssl_transform_get_minor_ver( transform ),
2791 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002792 {
Hanno Becker3307b532017-12-27 21:37:21 +00002793 if( f_rng == NULL )
2794 {
2795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2796 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2797 }
2798
2799 if( rec->data_offset < transform->ivlen )
2800 {
2801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2802 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2803 }
2804
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002805 /*
2806 * Generate IV
2807 */
Hanno Becker3307b532017-12-27 21:37:21 +00002808 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002809 if( ret != 0 )
2810 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002811
Teppo Järvelin91d79382019-10-02 09:09:31 +03002812 mbedtls_platform_memcpy( data - transform->ivlen, transform->iv_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002813 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002814
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002815 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002819 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002820 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002821 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002822
Hanno Becker3307b532017-12-27 21:37:21 +00002823 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2824 transform->iv_enc,
2825 transform->ivlen,
2826 data, rec->data_len,
2827 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002830 return( ret );
2831 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002832
Hanno Becker3307b532017-12-27 21:37:21 +00002833 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2836 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002837 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002840 if( mbedtls_ssl_ver_lt(
2841 mbedtls_ssl_transform_get_minor_ver( transform ),
2842 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02002843 {
2844 /*
2845 * Save IV in SSL3 and TLS1
2846 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002847 mbedtls_platform_memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
Hanno Becker3307b532017-12-27 21:37:21 +00002848 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002849 }
Hanno Becker3307b532017-12-27 21:37:21 +00002850 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002851#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002852 {
2853 data -= transform->ivlen;
2854 rec->data_offset -= transform->ivlen;
2855 rec->data_len += transform->ivlen;
2856 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002859 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002860 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002861 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2862
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002863 /*
2864 * MAC(MAC_write_key, seq_num +
2865 * TLSCipherText.type +
2866 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002867 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002868 * IV + // except for TLS 1.0
2869 * ENC(content + padding + padding_length));
2870 */
Hanno Becker3307b532017-12-27 21:37:21 +00002871
2872 if( post_avail < transform->maclen)
2873 {
2874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2875 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2876 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002877
Hanno Beckere83efe62019-04-29 13:52:53 +01002878 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002881 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002882 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002883
Hanno Becker3307b532017-12-27 21:37:21 +00002884 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002885 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002886 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2887 data, rec->data_len );
2888 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2889 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002890
Teppo Järvelin91d79382019-10-02 09:09:31 +03002891 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002892
Hanno Becker3307b532017-12-27 21:37:21 +00002893 rec->data_len += transform->maclen;
2894 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002895 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002896 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002898 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002899 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002901 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2904 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002905 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002906
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002907 /* Make extra sure authentication was performed, exactly once */
2908 if( auth_done != 1 )
2909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2911 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002912 }
2913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002915
2916 return( 0 );
2917}
2918
Hanno Becker40478be2019-07-12 08:23:59 +01002919int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002920 mbedtls_ssl_transform *transform,
2921 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002922{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002923 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002925 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002926#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002927 size_t padlen = 0, correct = 1;
2928#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002929 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002930 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002931 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002932
Hanno Becker611a83b2018-01-03 14:27:32 +00002933#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002934 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002935 ((void) ssl);
2936#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002939 if( rec == NULL ||
2940 rec->buf == NULL ||
2941 rec->buf_len < rec->data_offset ||
2942 rec->buf_len - rec->data_offset < rec->data_len )
2943 {
2944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002946 }
2947
Hanno Becker4c6876b2017-12-27 21:28:58 +00002948 data = rec->buf + rec->data_offset;
2949 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002950
Hanno Beckera5a2b082019-05-15 14:03:01 +01002951#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002952 /*
2953 * Match record's CID with incoming CID.
2954 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002955 if( rec->cid_len != transform->in_cid_len ||
Teppo Järvelin0efac532019-10-04 13:21:08 +03002956 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) // use regular memcmp as CID is public
Hanno Beckerabd7c892019-05-08 13:02:22 +01002957 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002958 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002959 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002960#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2963 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002964 {
2965 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002966 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2967 transform->iv_dec,
2968 transform->ivlen,
2969 data, rec->data_len,
2970 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002973 return( ret );
2974 }
2975
Hanno Becker4c6876b2017-12-27 21:28:58 +00002976 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2979 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002980 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002981 }
Paul Bakker68884e32013-01-07 18:20:04 +01002982 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002984#if defined(MBEDTLS_GCM_C) || \
2985 defined(MBEDTLS_CCM_C) || \
2986 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002988 mode == MBEDTLS_MODE_CCM ||
2989 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002990 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002991 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002992 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002993
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002994 /*
2995 * Prepare IV from explicit and implicit data.
2996 */
2997
2998 /* Check that there's enough space for the explicit IV
2999 * (at the beginning of the record) and the MAC (at the
3000 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003001 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003004 "+ taglen (%d)", rec->data_len,
3005 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003007 }
Paul Bakker68884e32013-01-07 18:20:04 +01003008
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003009#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003010 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
3011 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003012 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01003013
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003014 /* Fixed */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003015 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003016 /* Explicit */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003017 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003018 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003019 else
3020#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3021#if defined(MBEDTLS_CHACHAPOLY_C)
3022 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003023 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02003024 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003025 unsigned char i;
3026
Teppo Järvelin91d79382019-10-02 09:09:31 +03003027 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003028
3029 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003030 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003031 }
3032 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003033#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003034 {
3035 /* Reminder if we ever add an AEAD mode with a different size */
3036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3037 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3038 }
3039
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003040 /* Group changes to data, data_len, and add_data, because
3041 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003042 data += explicit_iv_len;
3043 rec->data_offset += explicit_iv_len;
3044 rec->data_len -= explicit_iv_len + transform->taglen;
3045
Hanno Beckere83efe62019-04-29 13:52:53 +01003046 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003047 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01003048 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003049
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003050 /* Because of the check above, we know that there are
3051 * explicit_iv_len Bytes preceeding data, and taglen
3052 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01003053 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003054 * mbedtls_cipher_auth_decrypt() below. */
3055
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003056 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003057 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00003058 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01003059
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003060 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003061 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003062 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003063 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
3064 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01003065 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003066 data, rec->data_len,
3067 data, &olen,
3068 data + rec->data_len,
3069 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
3074 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003075
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003076 return( ret );
3077 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003078 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003079
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003080 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003081 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3084 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003085 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003086 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003087 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3089#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003090 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003092 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003093 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003094
Paul Bakker5121ce52009-01-03 21:22:43 +00003095 /*
Paul Bakker45829992013-01-03 14:52:21 +01003096 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003099 if( mbedtls_ssl_ver_geq(
3100 mbedtls_ssl_transform_get_minor_ver( transform ),
3101 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003102 {
3103 /* The ciphertext is prefixed with the CBC IV. */
3104 minlen += transform->ivlen;
3105 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003106#endif
Paul Bakker45829992013-01-03 14:52:21 +01003107
Hanno Becker4c6876b2017-12-27 21:28:58 +00003108 /* Size considerations:
3109 *
3110 * - The CBC cipher text must not be empty and hence
3111 * at least of size transform->ivlen.
3112 *
3113 * Together with the potential IV-prefix, this explains
3114 * the first of the two checks below.
3115 *
3116 * - The record must contain a MAC, either in plain or
3117 * encrypted, depending on whether Encrypt-then-MAC
3118 * is used or not.
3119 * - If it is, the message contains the IV-prefix,
3120 * the CBC ciphertext, and the MAC.
3121 * - If it is not, the padded plaintext, and hence
3122 * the CBC ciphertext, has at least length maclen + 1
3123 * because there is at least the padding length byte.
3124 *
3125 * As the CBC ciphertext is not empty, both cases give the
3126 * lower bound minlen + maclen + 1 on the record size, which
3127 * we test for in the second check below.
3128 */
3129 if( rec->data_len < minlen + transform->ivlen ||
3130 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003133 "+ 1 ) ( + expl IV )", rec->data_len,
3134 transform->ivlen,
3135 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003137 }
3138
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003139 /*
3140 * Authenticate before decrypt if enabled
3141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003143 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003144 {
Hanno Becker992b6872017-11-09 18:57:39 +00003145 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003148
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003149 /* Update data_len in tandem with add_data.
3150 *
3151 * The subtraction is safe because of the previous check
3152 * data_len >= minlen + maclen + 1.
3153 *
3154 * Afterwards, we know that data + data_len is followed by at
3155 * least maclen Bytes, which justifies the call to
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003156 * mbedtls_platform_memcmp() below.
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003157 *
3158 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003159 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003160 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003161
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003162 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003163 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3164 add_data_len );
3165 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3166 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003167 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3168 data, rec->data_len );
3169 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3170 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003171
Hanno Becker4c6876b2017-12-27 21:28:58 +00003172 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3173 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003174 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003175 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003176
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003177 /* Compare expected MAC with MAC at the end of the record. */
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003178 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003179 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003183 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003184 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003185 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003187
3188 /*
3189 * Check length sanity
3190 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003191
3192 /* We know from above that data_len > minlen >= 0,
3193 * so the following check in particular implies that
3194 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003195 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003198 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003200 }
3201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003203 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003204 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003205 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003206 if( mbedtls_ssl_ver_geq(
3207 mbedtls_ssl_transform_get_minor_ver( transform ),
3208 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003209 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003210 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003211 mbedtls_platform_memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003212
Hanno Becker4c6876b2017-12-27 21:28:58 +00003213 data += transform->ivlen;
3214 rec->data_offset += transform->ivlen;
3215 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003216 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003218
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003219 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3220
Hanno Becker4c6876b2017-12-27 21:28:58 +00003221 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3222 transform->iv_dec, transform->ivlen,
3223 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003226 return( ret );
3227 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003228
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003229 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003230 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3233 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003234 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003237 if( mbedtls_ssl_ver_lt(
3238 mbedtls_ssl_transform_get_minor_ver( transform ),
3239 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02003240 {
3241 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003242 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3243 * records is equivalent to CBC decryption of the concatenation
3244 * of the records; in other words, IVs are maintained across
3245 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003246 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003247 mbedtls_platform_memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003248 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003249 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003250#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003251
Hanno Becker4c6876b2017-12-27 21:28:58 +00003252 /* Safe since data_len >= minlen + maclen + 1, so after having
3253 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003254 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3255 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003256 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003257
Hanno Becker4c6876b2017-12-27 21:28:58 +00003258 if( auth_done == 1 )
3259 {
3260 correct *= ( rec->data_len >= padlen + 1 );
3261 padlen *= ( rec->data_len >= padlen + 1 );
3262 }
3263 else
Paul Bakker45829992013-01-03 14:52:21 +01003264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003265#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003266 if( rec->data_len < transform->maclen + padlen + 1 )
3267 {
3268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3269 rec->data_len,
3270 transform->maclen,
3271 padlen + 1 ) );
3272 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003273#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003274
3275 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3276 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003277 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003278
Hanno Becker4c6876b2017-12-27 21:28:58 +00003279 padlen++;
3280
3281 /* Regardless of the validity of the padding,
3282 * we have data_len >= padlen here. */
3283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003285 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3286 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003287 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003288 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290#if defined(MBEDTLS_SSL_DEBUG_ALL)
3291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003292 "should be no more than %d",
3293 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003294#endif
Paul Bakker45829992013-01-03 14:52:21 +01003295 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 }
3297 }
3298 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3300#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3301 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003302 if( mbedtls_ssl_ver_gt(
3303 mbedtls_ssl_transform_get_minor_ver( transform ),
3304 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003305 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003306 /* The padding check involves a series of up to 256
3307 * consecutive memory reads at the end of the record
3308 * plaintext buffer. In order to hide the length and
3309 * validity of the padding, always perform exactly
3310 * `min(256,plaintext_len)` reads (but take into account
3311 * only the last `padlen` bytes for the padding check). */
3312 size_t pad_count = 0;
3313 size_t real_count = 0;
3314 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003315
Hanno Becker4c6876b2017-12-27 21:28:58 +00003316 /* Index of first padding byte; it has been ensured above
3317 * that the subtraction is safe. */
3318 size_t const padding_idx = rec->data_len - padlen;
3319 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3320 size_t const start_idx = rec->data_len - num_checks;
3321 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003322
Hanno Becker4c6876b2017-12-27 21:28:58 +00003323 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003324 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003325 real_count |= ( idx >= padding_idx );
3326 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003327 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003328 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003331 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003333#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003334 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003335 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003336 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3338 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3341 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003342 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003343
Hanno Becker4c6876b2017-12-27 21:28:58 +00003344 /* If the padding was found to be invalid, padlen == 0
3345 * and the subtraction is safe. If the padding was found valid,
3346 * padlen hasn't been changed and the previous assertion
3347 * data_len >= padlen still holds. */
3348 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003349 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003350 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003352 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3355 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003356 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003357
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003358#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003360 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003361#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003362
3363 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003364 * Authenticate if not done yet.
3365 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003366 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003367#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003368 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003369 {
Hanno Becker992b6872017-11-09 18:57:39 +00003370 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003371
Hanno Becker4c6876b2017-12-27 21:28:58 +00003372 /* If the initial value of padlen was such that
3373 * data_len < maclen + padlen + 1, then padlen
3374 * got reset to 1, and the initial check
3375 * data_len >= minlen + maclen + 1
3376 * guarantees that at this point we still
3377 * have at least data_len >= maclen.
3378 *
3379 * If the initial value of padlen was such that
3380 * data_len >= maclen + padlen + 1, then we have
3381 * subtracted either padlen + 1 (if the padding was correct)
3382 * or 0 (if the padding was incorrect) since then,
3383 * hence data_len >= maclen in any case.
3384 */
3385 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003386 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003389 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3390 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003391 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003392 ssl_mac( &transform->md_ctx_dec,
3393 transform->mac_dec,
3394 data, rec->data_len,
3395 rec->ctr, rec->type,
3396 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003397 }
3398 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3400#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3401 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003402 if( mbedtls_ssl_ver_gt(
3403 mbedtls_ssl_transform_get_minor_ver( transform ),
3404 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003405 {
3406 /*
3407 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003408 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003409 *
3410 * Known timing attacks:
3411 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3412 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003413 * To compensate for different timings for the MAC calculation
3414 * depending on how much padding was removed (which is determined
3415 * by padlen), process extra_run more blocks through the hash
3416 * function.
3417 *
3418 * The formula in the paper is
3419 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3420 * where L1 is the size of the header plus the decrypted message
3421 * plus CBC padding and L2 is the size of the header plus the
3422 * decrypted message. This is for an underlying hash function
3423 * with 64-byte blocks.
3424 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3425 * correctly. We round down instead of up, so -56 is the correct
3426 * value for our calculations instead of -55.
3427 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003428 * Repeat the formula rather than defining a block_size variable.
3429 * This avoids requiring division by a variable at runtime
3430 * (which would be marginally less efficient and would require
3431 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003432 */
3433 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003434 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003435
3436 /*
3437 * The next two sizes are the minimum and maximum values of
3438 * in_msglen over all padlen values.
3439 *
3440 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003441 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003442 *
3443 * Note that max_len + maclen is never more than the buffer
3444 * length, as we previously did in_msglen -= maclen too.
3445 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003446 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003447 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3448
Hanno Becker4c6876b2017-12-27 21:28:58 +00003449 memset( tmp, 0, sizeof( tmp ) );
3450
Hanno Beckera5cedbc2019-07-17 11:21:02 +01003451 switch( mbedtls_md_get_type(
3452 mbedtls_md_get_handle( &transform->md_ctx_dec ) ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003453 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003454#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3455 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003456 case MBEDTLS_MD_MD5:
3457 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003458 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003459 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003460 extra_run =
3461 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3462 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003463 break;
3464#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003465#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003466 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003467 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003468 extra_run =
3469 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3470 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003471 break;
3472#endif
3473 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003475 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3476 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003477
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003478 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003479
Hanno Beckere83efe62019-04-29 13:52:53 +01003480 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3481 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003482 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3483 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003484 /* Make sure we access everything even when padlen > 0. This
3485 * makes the synchronisation requirements for just-in-time
3486 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003487 ssl_read_memory( data + rec->data_len, padlen );
3488 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003489
3490 /* Call mbedtls_md_process at least once due to cache attacks
3491 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003492 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003493 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003494
Hanno Becker4c6876b2017-12-27 21:28:58 +00003495 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003496
3497 /* Make sure we access all the memory that could contain the MAC,
3498 * before we check it in the next code block. This makes the
3499 * synchronisation requirements for just-in-time Prime+Probe
3500 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003501 ssl_read_memory( data + min_len,
3502 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003503 }
3504 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3506 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3509 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003510 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003511
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003512#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003513 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3514 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003515#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003516
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003517 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003518 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520#if defined(MBEDTLS_SSL_DEBUG_ALL)
3521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003522#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003523 correct = 0;
3524 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003525 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003526 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003527
3528 /*
3529 * Finally check the correct flag
3530 */
3531 if( correct == 0 )
3532 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003533#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003534
3535 /* Make extra sure authentication was performed, exactly once */
3536 if( auth_done != 1 )
3537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3539 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003540 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003541
Hanno Beckera5a2b082019-05-15 14:03:01 +01003542#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003543 if( rec->cid_len != 0 )
3544 {
3545 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3546 &rec->type );
3547 if( ret != 0 )
3548 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3549 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003550#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003553
3554 return( 0 );
3555}
3556
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003557#undef MAC_NONE
3558#undef MAC_PLAINTEXT
3559#undef MAC_CIPHERTEXT
3560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003562/*
3563 * Compression/decompression functions
3564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003565static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003566{
3567 int ret;
3568 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003569 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003570 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003571 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003574
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003575 if( len_pre == 0 )
3576 return( 0 );
3577
Teppo Järvelin91d79382019-10-02 09:09:31 +03003578 mbedtls_platform_memcpy( msg_pre, ssl->out_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003581 ssl->out_msglen ) );
3582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003584 ssl->out_msg, ssl->out_msglen );
3585
Paul Bakker48916f92012-09-16 19:57:18 +00003586 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3587 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3588 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003589 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003590
Paul Bakker48916f92012-09-16 19:57:18 +00003591 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003592 if( ret != Z_OK )
3593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3595 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003596 }
3597
Angus Grattond8213d02016-05-25 20:56:48 +10003598 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003599 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003602 ssl->out_msglen ) );
3603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003605 ssl->out_msg, ssl->out_msglen );
3606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003608
3609 return( 0 );
3610}
3611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003613{
3614 int ret;
3615 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003616 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003617 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003618 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003621
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003622 if( len_pre == 0 )
3623 return( 0 );
3624
Teppo Järvelin91d79382019-10-02 09:09:31 +03003625 mbedtls_platform_memcpy( msg_pre, ssl->in_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003628 ssl->in_msglen ) );
3629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003631 ssl->in_msg, ssl->in_msglen );
3632
Paul Bakker48916f92012-09-16 19:57:18 +00003633 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3634 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3635 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003636 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003637 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003638
Paul Bakker48916f92012-09-16 19:57:18 +00003639 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003640 if( ret != Z_OK )
3641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3643 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003644 }
3645
Angus Grattond8213d02016-05-25 20:56:48 +10003646 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003647 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003650 ssl->in_msglen ) );
3651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003653 ssl->in_msg, ssl->in_msglen );
3654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003656
3657 return( 0 );
3658}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3662static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664#if defined(MBEDTLS_SSL_PROTO_DTLS)
3665static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003666{
3667 /* If renegotiation is not enforced, retransmit until we would reach max
3668 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003669 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003670 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003671 uint32_t ratio =
3672 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3673 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003674 unsigned char doublings = 1;
3675
3676 while( ratio != 0 )
3677 {
3678 ++doublings;
3679 ratio >>= 1;
3680 }
3681
3682 if( ++ssl->renego_records_seen > doublings )
3683 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003685 return( 0 );
3686 }
3687 }
3688
3689 return( ssl_write_hello_request( ssl ) );
3690}
3691#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003692#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003693
Paul Bakker5121ce52009-01-03 21:22:43 +00003694/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003695 * Fill the input message buffer by appending data to it.
3696 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003697 *
3698 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3699 * available (from this read and/or a previous one). Otherwise, an error code
3700 * is returned (possibly EOF or WANT_READ).
3701 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003702 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3703 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3704 * since we always read a whole datagram at once.
3705 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003706 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003707 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003709int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003710{
Paul Bakker23986e52011-04-24 08:57:21 +00003711 int ret;
3712 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003715
Hanno Beckera58a8962019-06-13 16:11:15 +01003716 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3717 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003720 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003722 }
3723
Angus Grattond8213d02016-05-25 20:56:48 +10003724 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3727 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003728 }
3729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003731 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003732 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003733 uint32_t timeout;
3734
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003735 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003736 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3737 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003738 {
3739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3740 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3741 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3742 }
3743
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003744 /*
3745 * The point is, we need to always read a full datagram at once, so we
3746 * sometimes read more then requested, and handle the additional data.
3747 * It could be the rest of the current record (while fetching the
3748 * header) and/or some other records in the same datagram.
3749 */
3750
3751 /*
3752 * Move to the next record in the already read datagram if applicable
3753 */
3754 if( ssl->next_record_offset != 0 )
3755 {
3756 if( ssl->in_left < ssl->next_record_offset )
3757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3759 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003760 }
3761
3762 ssl->in_left -= ssl->next_record_offset;
3763
3764 if( ssl->in_left != 0 )
3765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003767 ssl->next_record_offset ) );
3768 memmove( ssl->in_hdr,
3769 ssl->in_hdr + ssl->next_record_offset,
3770 ssl->in_left );
3771 }
3772
3773 ssl->next_record_offset = 0;
3774 }
3775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003777 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003778
3779 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003780 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003781 */
3782 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003785 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003786 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003787
3788 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003789 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003790 * are not at the beginning of a new record, the caller did something
3791 * wrong.
3792 */
3793 if( ssl->in_left != 0 )
3794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3796 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003797 }
3798
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003799 /*
3800 * Don't even try to read if time's out already.
3801 * This avoids by-passing the timer when repeatedly receiving messages
3802 * that will end up being dropped.
3803 */
3804 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003805 {
3806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003807 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003808 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003809 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003810 {
Angus Grattond8213d02016-05-25 20:56:48 +10003811 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003814 timeout = ssl->handshake->retransmit_timeout;
3815 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003816 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003818 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003819
Hanno Beckera58a8962019-06-13 16:11:15 +01003820 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3821 {
3822 ret = mbedtls_ssl_get_recv_timeout( ssl )
3823 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3824 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003825 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003826 {
3827 ret = mbedtls_ssl_get_recv( ssl )
3828 ( ssl->p_bio, ssl->in_hdr, len );
3829 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003832
3833 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003834 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003835 }
3836
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003837 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003840 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003843 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003844 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003847 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003848 }
3849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003853 return( ret );
3854 }
3855
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003856 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003857 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003859 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3860 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003862 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003863 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003866 return( ret );
3867 }
3868
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003869 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003870 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003871#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003872 }
3873
Paul Bakker5121ce52009-01-03 21:22:43 +00003874 if( ret < 0 )
3875 return( ret );
3876
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003877 ssl->in_left = ret;
3878 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003879 MBEDTLS_SSL_TRANSPORT_ELSE
3880#endif /* MBEDTLS_SSL_PROTO_DTLS */
3881#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003884 ssl->in_left, nb_want ) );
3885
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003886 while( ssl->in_left < nb_want )
3887 {
3888 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003889
3890 if( ssl_check_timer( ssl ) != 0 )
3891 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3892 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003893 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003894 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003895 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003896 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3897 ssl->in_hdr + ssl->in_left, len,
3898 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003899 }
3900 else
3901 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003902 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003903 ssl->in_hdr + ssl->in_left, len );
3904 }
3905 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003908 ssl->in_left, nb_want ) );
3909 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003910
3911 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003913
3914 if( ret < 0 )
3915 return( ret );
3916
mohammad160352aecb92018-03-28 23:41:40 -07003917 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003918 {
Darryl Green11999bb2018-03-13 15:22:58 +00003919 MBEDTLS_SSL_DEBUG_MSG( 1,
3920 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003921 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003922 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3923 }
3924
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003925 ssl->in_left += ret;
3926 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003927 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003928#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003931
3932 return( 0 );
3933}
3934
3935/*
3936 * Flush any data not yet written
3937 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003939{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003940 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003941 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003944
Hanno Beckera58a8962019-06-13 16:11:15 +01003945 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003948 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003950 }
3951
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003952 /* Avoid incrementing counter if data is flushed */
3953 if( ssl->out_left == 0 )
3954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003956 return( 0 );
3957 }
3958
Paul Bakker5121ce52009-01-03 21:22:43 +00003959 while( ssl->out_left > 0 )
3960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003962 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003963
Hanno Becker2b1e3542018-08-06 11:19:13 +01003964 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003965 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003968
3969 if( ret <= 0 )
3970 return( ret );
3971
mohammad160352aecb92018-03-28 23:41:40 -07003972 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003973 {
Darryl Green11999bb2018-03-13 15:22:58 +00003974 MBEDTLS_SSL_DEBUG_MSG( 1,
3975 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003976 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003977 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3978 }
3979
Paul Bakker5121ce52009-01-03 21:22:43 +00003980 ssl->out_left -= ret;
3981 }
3982
Hanno Becker2b1e3542018-08-06 11:19:13 +01003983#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003984 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003985 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003986 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003987 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003988 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003989#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003990#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003991 {
3992 ssl->out_hdr = ssl->out_buf + 8;
3993 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003994#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003995 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003998
3999 return( 0 );
4000}
4001
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004002/*
4003 * Functions to handle the DTLS retransmission state machine
4004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004006/*
4007 * Append current handshake message to current outgoing flight
4008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004010{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004011 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01004012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
4013 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
4014 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004015
4016 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004017 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004018 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004020 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004021 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004022 }
4023
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004024 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004025 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004028 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004029 }
4030
4031 /* Copy current handshake message with headers */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004032 mbedtls_platform_memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004033 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004034 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004035 msg->next = NULL;
4036
4037 /* Append to the current flight */
4038 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004039 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004040 else
4041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004042 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004043 while( cur->next != NULL )
4044 cur = cur->next;
4045 cur->next = msg;
4046 }
4047
Hanno Becker3b235902018-08-06 09:54:53 +01004048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004049 return( 0 );
4050}
4051
4052/*
4053 * Free the current flight of handshake messages
4054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004055static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004057 mbedtls_ssl_flight_item *cur = flight;
4058 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004059
4060 while( cur != NULL )
4061 {
4062 next = cur->next;
4063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064 mbedtls_free( cur->p );
4065 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004066
4067 cur = next;
4068 }
4069}
4070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4072static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004073#endif
4074
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004075/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004076 * Swap transform_out and out_ctr with the alternative ones
4077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004079{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004081 unsigned char tmp_out_ctr[8];
4082
4083 if( ssl->transform_out == ssl->handshake->alt_transform_out )
4084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004085 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004086 return;
4087 }
4088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004090
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004091 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004092 tmp_transform = ssl->transform_out;
4093 ssl->transform_out = ssl->handshake->alt_transform_out;
4094 ssl->handshake->alt_transform_out = tmp_transform;
4095
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004096 /* Swap epoch + sequence_number */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004097 mbedtls_platform_memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4098 mbedtls_platform_memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
4099 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004100
4101 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004102 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4105 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004107 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004109 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4110 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004111 }
4112 }
4113#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004114}
4115
4116/*
4117 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004118 */
4119int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4120{
4121 int ret = 0;
4122
4123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4124
4125 ret = mbedtls_ssl_flight_transmit( ssl );
4126
4127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4128
4129 return( ret );
4130}
4131
4132/*
4133 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004134 *
4135 * Need to remember the current message in case flush_output returns
4136 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004137 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004138 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004139int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004140{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004141 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004144 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004145 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004147
4148 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004149 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004150 ssl_swap_epochs( ssl );
4151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004152 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004153 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004154
4155 while( ssl->handshake->cur_msg != NULL )
4156 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004157 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004158 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004159
Hanno Beckere1dcb032018-08-17 16:47:58 +01004160 int const is_finished =
4161 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4162 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4163
Hanno Becker04da1892018-08-14 13:22:10 +01004164 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4165 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4166
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004167 /* Swap epochs before sending Finished: we can't do it after
4168 * sending ChangeCipherSpec, in case write returns WANT_READ.
4169 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004170 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004171 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004173 ssl_swap_epochs( ssl );
4174 }
4175
Hanno Becker67bc7c32018-08-06 11:33:50 +01004176 ret = ssl_get_remaining_payload_in_datagram( ssl );
4177 if( ret < 0 )
4178 return( ret );
4179 max_frag_len = (size_t) ret;
4180
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004181 /* CCS is copied as is, while HS messages may need fragmentation */
4182 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4183 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004184 if( max_frag_len == 0 )
4185 {
4186 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4187 return( ret );
4188
4189 continue;
4190 }
4191
Teppo Järvelin91d79382019-10-02 09:09:31 +03004192 mbedtls_platform_memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004193 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004194 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004195
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004196 /* Update position inside current message */
4197 ssl->handshake->cur_msg_p += cur->len;
4198 }
4199 else
4200 {
4201 const unsigned char * const p = ssl->handshake->cur_msg_p;
4202 const size_t hs_len = cur->len - 12;
4203 const size_t frag_off = p - ( cur->p + 12 );
4204 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004205 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004206
Hanno Beckere1dcb032018-08-17 16:47:58 +01004207 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004208 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004209 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004210 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004211
Hanno Becker67bc7c32018-08-06 11:33:50 +01004212 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4213 return( ret );
4214
4215 continue;
4216 }
4217 max_hs_frag_len = max_frag_len - 12;
4218
4219 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4220 max_hs_frag_len : rem_len;
4221
4222 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004223 {
4224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004225 (unsigned) cur_hs_frag_len,
4226 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004227 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004228
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004229 /* Messages are stored with handshake headers as if not fragmented,
4230 * copy beginning of headers then fill fragmentation fields.
4231 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004232 mbedtls_platform_memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004233
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004234 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[6], frag_off );
4235 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[9],
4236 cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004237
4238 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4239
Hanno Becker3f7b9732018-08-28 09:53:25 +01004240 /* Copy the handshake message content and set records fields */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004241 mbedtls_platform_memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004242 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004243 ssl->out_msgtype = cur->type;
4244
4245 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004246 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004247 }
4248
4249 /* If done with the current message move to the next one if any */
4250 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4251 {
4252 if( cur->next != NULL )
4253 {
4254 ssl->handshake->cur_msg = cur->next;
4255 ssl->handshake->cur_msg_p = cur->next->p + 12;
4256 }
4257 else
4258 {
4259 ssl->handshake->cur_msg = NULL;
4260 ssl->handshake->cur_msg_p = NULL;
4261 }
4262 }
4263
4264 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004265 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004267 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004268 return( ret );
4269 }
4270 }
4271
Hanno Becker67bc7c32018-08-06 11:33:50 +01004272 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4273 return( ret );
4274
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004275 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004276 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4277 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004278 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004280 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004281 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4282 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004283
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004285
4286 return( 0 );
4287}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004288
4289/*
4290 * To be called when the last message of an incoming flight is received.
4291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004292void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004293{
4294 /* We won't need to resend that one any more */
4295 ssl_flight_free( ssl->handshake->flight );
4296 ssl->handshake->flight = NULL;
4297 ssl->handshake->cur_msg = NULL;
4298
4299 /* The next incoming flight will start with this msg_seq */
4300 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4301
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004302 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004303 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004304
Hanno Becker0271f962018-08-16 13:23:47 +01004305 /* Clear future message buffering structure. */
4306 ssl_buffering_free( ssl );
4307
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004308 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004309 ssl_set_timer( ssl, 0 );
4310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4312 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004315 }
4316 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004317 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004318}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004319
4320/*
4321 * To be called when the last message of an outgoing flight is send.
4322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004324{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004325 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004326 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004328 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4329 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004332 }
4333 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004335}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004336#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004337
Paul Bakker5121ce52009-01-03 21:22:43 +00004338/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004339 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004340 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004341
4342/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004343 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004344 *
4345 * - fill in handshake headers
4346 * - update handshake checksum
4347 * - DTLS: save message for resending
4348 * - then pass to the record layer
4349 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004350 * DTLS: except for HelloRequest, messages are only queued, and will only be
4351 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004352 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004353 * Inputs:
4354 * - ssl->out_msglen: 4 + actual handshake message len
4355 * (4 is the size of handshake headers for TLS)
4356 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4357 * - ssl->out_msg + 4: the handshake message body
4358 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004359 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004360 * - ssl->out_msglen: the length of the record contents
4361 * (including handshake headers but excluding record headers)
4362 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004363 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004364int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004365{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004366 int ret;
4367 const size_t hs_len = ssl->out_msglen - 4;
4368 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004369
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4371
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004372 /*
4373 * Sanity checks
4374 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004375 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004376 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4377 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004378 /* In SSLv3, the client might send a NoCertificate alert. */
4379#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004380 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004381 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004382 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4383 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004384#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4385 {
4386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4387 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4388 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004389 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004390
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004391 /* Whenever we send anything different from a
4392 * HelloRequest we should be in a handshake - double check. */
4393 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4394 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004395 ssl->handshake == NULL )
4396 {
4397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4398 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4399 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004402 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004403 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004405 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4407 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004408 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004409#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004410
Hanno Beckerb50a2532018-08-06 11:52:54 +01004411 /* Double-check that we did not exceed the bounds
4412 * of the outgoing record buffer.
4413 * This should never fail as the various message
4414 * writing functions must obey the bounds of the
4415 * outgoing record buffer, but better be safe.
4416 *
4417 * Note: We deliberately do not check for the MTU or MFL here.
4418 */
4419 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4420 {
4421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4422 "size %u, maximum %u",
4423 (unsigned) ssl->out_msglen,
4424 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4425 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4426 }
4427
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004428 /*
4429 * Fill handshake headers
4430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004431 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004432 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004433 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[1], hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004434
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004435 /*
4436 * DTLS has additional fields in the Handshake layer,
4437 * between the length field and the actual payload:
4438 * uint16 message_seq;
4439 * uint24 fragment_offset;
4440 * uint24 fragment_length;
4441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004442#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004443 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004444 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004445 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004446 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004447 {
4448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4449 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004450 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004451 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004452 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4453 }
4454
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004455 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004456 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004457
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004458 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004459 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004460 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004461 (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[4],
4462 ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004463 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004464 }
4465 else
4466 {
4467 ssl->out_msg[4] = 0;
4468 ssl->out_msg[5] = 0;
4469 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004470
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004471 /* Handshake hashes are computed without fragmentation,
4472 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004473 mbedtls_platform_memset( ssl->out_msg + 6, 0x00, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03004474 mbedtls_platform_memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004475 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004476#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004477
Hanno Becker0207e532018-08-28 10:28:28 +01004478 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004479 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004480 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004481 }
4482
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004483 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004484#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004485 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004486 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4487 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004488 {
4489 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004491 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004492 return( ret );
4493 }
4494 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004495 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004496#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004497 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004498 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004499 {
4500 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4501 return( ret );
4502 }
4503 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004504
4505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4506
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004507 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004508}
4509
4510/*
4511 * Record layer functions
4512 */
4513
4514/*
4515 * Write current record.
4516 *
4517 * Uses:
4518 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4519 * - ssl->out_msglen: length of the record content (excl headers)
4520 * - ssl->out_msg: record content
4521 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004522int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004523{
4524 int ret, done = 0;
4525 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004526 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004527
4528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004530#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004531 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004532 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004533 {
4534 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004536 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004537 return( ret );
4538 }
4539
4540 len = ssl->out_msglen;
4541 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004544#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4545 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004549 ret = mbedtls_ssl_hw_record_write( ssl );
4550 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4553 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004554 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004555
4556 if( ret == 0 )
4557 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004558 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004559#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004560 if( !done )
4561 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004562 unsigned i;
4563 size_t protected_record_size;
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004564 volatile int encrypted_fi = 0;
Hanno Becker2b1e3542018-08-06 11:19:13 +01004565
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004566 /* Skip writing the record content type to after the encryption,
4567 * as it may change when using the CID extension. */
4568
Hanno Becker2881d802019-05-22 14:44:53 +01004569 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4570 mbedtls_ssl_get_minor_ver( ssl ),
4571 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004572
Teppo Järvelin91d79382019-10-02 09:09:31 +03004573 mbedtls_platform_memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004574 (void)mbedtls_platform_put_uint16_be( ssl->out_len, len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004575
Paul Bakker48916f92012-09-16 19:57:18 +00004576 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004577 {
Hanno Becker3307b532017-12-27 21:37:21 +00004578 mbedtls_record rec;
4579
4580 rec.buf = ssl->out_iv;
4581 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4582 ( ssl->out_iv - ssl->out_buf );
4583 rec.data_len = ssl->out_msglen;
4584 rec.data_offset = ssl->out_msg - rec.buf;
4585
Teppo Järvelin91d79382019-10-02 09:09:31 +03004586 mbedtls_platform_memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004587 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4588 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004589 ssl->conf->transport, rec.ver );
4590 rec.type = ssl->out_msgtype;
4591
Hanno Beckera5a2b082019-05-15 14:03:01 +01004592#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004593 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004594 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004595#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004596
Hanno Becker611a83b2018-01-03 14:27:32 +00004597 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004598 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004599 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004601 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004602 return( ret );
4603 }
4604
Hanno Becker3307b532017-12-27 21:37:21 +00004605 if( rec.data_offset != 0 )
4606 {
4607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4608 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4609 }
4610
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004611 /* Update the record content type and CID. */
4612 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004613#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03004614 /* Not using more secure mbedtls_platform_memcpy as cid is public */
4615 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004616#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004617 ssl->out_msglen = len = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004618 (void)mbedtls_platform_put_uint16_be( ssl->out_len, rec.data_len );
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004619 encrypted_fi = 1;
4620 }
4621
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004622 /* Double check to ensure the encryption has been done */
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004623 if( ssl->transform_out != NULL && encrypted_fi == 0 )
4624 {
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004625 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004626 }
4627
Hanno Becker43395762019-05-03 14:46:38 +01004628 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004629
4630#if defined(MBEDTLS_SSL_PROTO_DTLS)
4631 /* In case of DTLS, double-check that we don't exceed
4632 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004633 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004634 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004635 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004636 if( ret < 0 )
4637 return( ret );
4638
4639 if( protected_record_size > (size_t) ret )
4640 {
4641 /* Should never happen */
4642 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4643 }
4644 }
4645#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004646
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004647 /* Now write the potentially updated record content type. */
4648 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004650 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004651 "version = [%d:%d], msglen = %d",
4652 ssl->out_hdr[0], ssl->out_hdr[1],
4653 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004655 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004656 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004657
4658 ssl->out_left += protected_record_size;
4659 ssl->out_hdr += protected_record_size;
4660 ssl_update_out_pointers( ssl, ssl->transform_out );
4661
Hanno Becker04484622018-08-06 09:49:38 +01004662 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4663 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4664 break;
4665
4666 /* The loop goes to its end iff the counter is wrapping */
4667 if( i == ssl_ep_len( ssl ) )
4668 {
4669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4670 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4671 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004672 }
4673
Hanno Becker67bc7c32018-08-06 11:33:50 +01004674#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004675 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004676 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004677 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004678 size_t remaining;
4679 ret = ssl_get_remaining_payload_in_datagram( ssl );
4680 if( ret < 0 )
4681 {
4682 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4683 ret );
4684 return( ret );
4685 }
4686
4687 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004688 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004689 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004690 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004691 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004692 else
4693 {
Hanno Becker513815a2018-08-20 11:56:09 +01004694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004695 }
4696 }
4697#endif /* MBEDTLS_SSL_PROTO_DTLS */
4698
4699 if( ( flush == SSL_FORCE_FLUSH ) &&
4700 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004702 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004703 return( ret );
4704 }
4705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004707
4708 return( 0 );
4709}
4710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004711#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004712
4713static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4714{
4715 if( ssl->in_msglen < ssl->in_hslen ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +03004716 mbedtls_platform_memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4717 mbedtls_platform_memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
Hanno Beckere25e3b72018-08-16 09:30:53 +01004718 {
4719 return( 1 );
4720 }
4721 return( 0 );
4722}
Hanno Becker44650b72018-08-16 12:51:11 +01004723
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004724static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004725{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004726 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[9] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004727}
4728
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004729static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004730{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004731 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[6] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004732}
4733
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004734static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004735{
4736 uint32_t msg_len, frag_off, frag_len;
4737
4738 msg_len = ssl_get_hs_total_len( ssl );
4739 frag_off = ssl_get_hs_frag_off( ssl );
4740 frag_len = ssl_get_hs_frag_len( ssl );
4741
4742 if( frag_off > msg_len )
4743 return( -1 );
4744
4745 if( frag_len > msg_len - frag_off )
4746 return( -1 );
4747
4748 if( frag_len + 12 > ssl->in_msglen )
4749 return( -1 );
4750
4751 return( 0 );
4752}
4753
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004754/*
4755 * Mark bits in bitmask (used for DTLS HS reassembly)
4756 */
4757static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4758{
4759 unsigned int start_bits, end_bits;
4760
4761 start_bits = 8 - ( offset % 8 );
4762 if( start_bits != 8 )
4763 {
4764 size_t first_byte_idx = offset / 8;
4765
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004766 /* Special case */
4767 if( len <= start_bits )
4768 {
4769 for( ; len != 0; len-- )
4770 mask[first_byte_idx] |= 1 << ( start_bits - len );
4771
4772 /* Avoid potential issues with offset or len becoming invalid */
4773 return;
4774 }
4775
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004776 offset += start_bits; /* Now offset % 8 == 0 */
4777 len -= start_bits;
4778
4779 for( ; start_bits != 0; start_bits-- )
4780 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4781 }
4782
4783 end_bits = len % 8;
4784 if( end_bits != 0 )
4785 {
4786 size_t last_byte_idx = ( offset + len ) / 8;
4787
4788 len -= end_bits; /* Now len % 8 == 0 */
4789
4790 for( ; end_bits != 0; end_bits-- )
4791 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4792 }
4793
4794 memset( mask + offset / 8, 0xFF, len / 8 );
4795}
4796
4797/*
4798 * Check that bitmask is full
4799 */
4800static int ssl_bitmask_check( unsigned char *mask, size_t len )
4801{
4802 size_t i;
4803
4804 for( i = 0; i < len / 8; i++ )
4805 if( mask[i] != 0xFF )
4806 return( -1 );
4807
4808 for( i = 0; i < len % 8; i++ )
4809 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4810 return( -1 );
4811
4812 return( 0 );
4813}
4814
Hanno Becker56e205e2018-08-16 09:06:12 +01004815/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004816static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004817 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004818{
Hanno Becker56e205e2018-08-16 09:06:12 +01004819 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004820
Hanno Becker56e205e2018-08-16 09:06:12 +01004821 alloc_len = 12; /* Handshake header */
4822 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004823
Hanno Beckerd07df862018-08-16 09:14:58 +01004824 if( add_bitmap )
4825 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004826
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004827 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004828}
Hanno Becker56e205e2018-08-16 09:06:12 +01004829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004830#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004831
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004832static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004833{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004834 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[1] ) );
Hanno Becker12555c62018-08-16 12:47:53 +01004835}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004836
Simon Butcher99000142016-10-13 17:21:01 +01004837int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004838{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004839 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004842 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004843 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004844 }
4845
Hanno Becker12555c62018-08-16 12:47:53 +01004846 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004848 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004849 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004850 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004852#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004853 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004854 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004855 int ret;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004856 unsigned int recv_msg_seq = (unsigned int)
4857 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004858
Hanno Becker44650b72018-08-16 12:51:11 +01004859 if( ssl_check_hs_header( ssl ) != 0 )
4860 {
4861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4862 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4863 }
4864
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004865 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004866 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4867 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4868 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4869 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004870 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004871 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4872 {
4873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4874 recv_msg_seq,
4875 ssl->handshake->in_msg_seq ) );
4876 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4877 }
4878
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004879 /* Retransmit only on last message from previous flight, to avoid
4880 * too many retransmissions.
4881 * Besides, No sane server ever retransmits HelloVerifyRequest */
4882 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004883 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004886 "message_seq = %d, start_of_flight = %d",
4887 recv_msg_seq,
4888 ssl->handshake->in_flight_start_seq ) );
4889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004890 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004892 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004893 return( ret );
4894 }
4895 }
4896 else
4897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004899 "message_seq = %d, expected = %d",
4900 recv_msg_seq,
4901 ssl->handshake->in_msg_seq ) );
4902 }
4903
Hanno Becker90333da2017-10-10 11:27:13 +01004904 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004905 }
4906 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004907
Hanno Becker6d97ef52018-08-16 13:09:04 +01004908 /* Message reassembly is handled alongside buffering of future
4909 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004910 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004911 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004912 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004915 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004916 }
4917 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004918 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004919#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004920#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004921 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004922 /* With TLS we don't handle fragmentation (for now) */
4923 if( ssl->in_msglen < ssl->in_hslen )
4924 {
4925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4926 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4927 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004928 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004929#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004930
Simon Butcher99000142016-10-13 17:21:01 +01004931 return( 0 );
4932}
4933
4934void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4935{
Hanno Becker0271f962018-08-16 13:23:47 +01004936 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004937
Hanno Becker0271f962018-08-16 13:23:47 +01004938 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004939 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004940
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004941 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004943 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004944 ssl->handshake != NULL )
4945 {
Hanno Becker0271f962018-08-16 13:23:47 +01004946 unsigned offset;
4947 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004948
Hanno Becker0271f962018-08-16 13:23:47 +01004949 /* Increment handshake sequence number */
4950 hs->in_msg_seq++;
4951
4952 /*
4953 * Clear up handshake buffering and reassembly structure.
4954 */
4955
4956 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004957 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004958
4959 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004960 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4961 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004962 offset++, hs_buf++ )
4963 {
4964 *hs_buf = *(hs_buf + 1);
4965 }
4966
4967 /* Create a fresh last entry */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004968 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004969 }
4970#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004971}
4972
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004973/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004974 * DTLS anti-replay: RFC 6347 4.1.2.6
4975 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004976 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4977 * Bit n is set iff record number in_window_top - n has been seen.
4978 *
4979 * Usually, in_window_top is the last record number seen and the lsb of
4980 * in_window is set. The only exception is the initial state (record number 0
4981 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004982 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004983#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4984static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004985{
4986 ssl->in_window_top = 0;
4987 ssl->in_window = 0;
4988}
4989
4990static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4991{
4992 return( ( (uint64_t) buf[0] << 40 ) |
4993 ( (uint64_t) buf[1] << 32 ) |
4994 ( (uint64_t) buf[2] << 24 ) |
4995 ( (uint64_t) buf[3] << 16 ) |
4996 ( (uint64_t) buf[4] << 8 ) |
4997 ( (uint64_t) buf[5] ) );
4998}
4999
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005000static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
5001{
5002 int ret;
5003 unsigned char *original_in_ctr;
5004
5005 // save original in_ctr
5006 original_in_ctr = ssl->in_ctr;
5007
5008 // use counter from record
5009 ssl->in_ctr = record_in_ctr;
5010
5011 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
5012
5013 // restore the counter
5014 ssl->in_ctr = original_in_ctr;
5015
5016 return ret;
5017}
5018
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005019/*
5020 * Return 0 if sequence number is acceptable, -1 otherwise
5021 */
Hanno Beckerfc551722019-07-12 08:50:37 +01005022int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005023{
5024 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5025 uint64_t bit;
5026
Hanno Becker7f376f42019-06-12 16:20:48 +01005027 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5028 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5029 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005030 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01005031 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005032
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005033 if( rec_seqnum > ssl->in_window_top )
5034 return( 0 );
5035
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005036 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005037
5038 if( bit >= 64 )
5039 return( -1 );
5040
5041 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
5042 return( -1 );
5043
5044 return( 0 );
5045}
5046
5047/*
5048 * Update replay window on new validated record
5049 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005050void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005051{
5052 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5053
Hanno Becker7f376f42019-06-12 16:20:48 +01005054 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5055 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5056 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005057 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01005058 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005059
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005060 if( rec_seqnum > ssl->in_window_top )
5061 {
5062 /* Update window_top and the contents of the window */
5063 uint64_t shift = rec_seqnum - ssl->in_window_top;
5064
5065 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005066 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005067 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005068 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005069 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005070 ssl->in_window |= 1;
5071 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005072
5073 ssl->in_window_top = rec_seqnum;
5074 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005075 else
5076 {
5077 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005078 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005079
5080 if( bit < 64 ) /* Always true, but be extra sure */
5081 ssl->in_window |= (uint64_t) 1 << bit;
5082 }
5083}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005085
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005086#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005087/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005088static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
5089
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005090/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005091 * Without any SSL context, check if a datagram looks like a ClientHello with
5092 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01005093 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005094 *
5095 * - if cookie is valid, return 0
5096 * - if ClientHello looks superficially valid but cookie is not,
5097 * fill obuf and set olen, then
5098 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
5099 * - otherwise return a specific error code
5100 */
5101static int ssl_check_dtls_clihlo_cookie(
5102 mbedtls_ssl_cookie_write_t *f_cookie_write,
5103 mbedtls_ssl_cookie_check_t *f_cookie_check,
5104 void *p_cookie,
5105 const unsigned char *cli_id, size_t cli_id_len,
5106 const unsigned char *in, size_t in_len,
5107 unsigned char *obuf, size_t buf_len, size_t *olen )
5108{
5109 size_t sid_len, cookie_len;
5110 unsigned char *p;
5111
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005112 /*
5113 * Structure of ClientHello with record and handshake headers,
5114 * and expected values. We don't need to check a lot, more checks will be
5115 * done when actually parsing the ClientHello - skipping those checks
5116 * avoids code duplication and does not make cookie forging any easier.
5117 *
5118 * 0-0 ContentType type; copied, must be handshake
5119 * 1-2 ProtocolVersion version; copied
5120 * 3-4 uint16 epoch; copied, must be 0
5121 * 5-10 uint48 sequence_number; copied
5122 * 11-12 uint16 length; (ignored)
5123 *
5124 * 13-13 HandshakeType msg_type; (ignored)
5125 * 14-16 uint24 length; (ignored)
5126 * 17-18 uint16 message_seq; copied
5127 * 19-21 uint24 fragment_offset; copied, must be 0
5128 * 22-24 uint24 fragment_length; (ignored)
5129 *
5130 * 25-26 ProtocolVersion client_version; (ignored)
5131 * 27-58 Random random; (ignored)
5132 * 59-xx SessionID session_id; 1 byte len + sid_len content
5133 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5134 * ...
5135 *
5136 * Minimum length is 61 bytes.
5137 */
5138 if( in_len < 61 ||
5139 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5140 in[3] != 0 || in[4] != 0 ||
5141 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5142 {
5143 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5144 }
5145
5146 sid_len = in[59];
5147 if( sid_len > in_len - 61 )
5148 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5149
5150 cookie_len = in[60 + sid_len];
5151 if( cookie_len > in_len - 60 )
5152 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5153
5154 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5155 cli_id, cli_id_len ) == 0 )
5156 {
5157 /* Valid cookie */
5158 return( 0 );
5159 }
5160
5161 /*
5162 * If we get here, we've got an invalid cookie, let's prepare HVR.
5163 *
5164 * 0-0 ContentType type; copied
5165 * 1-2 ProtocolVersion version; copied
5166 * 3-4 uint16 epoch; copied
5167 * 5-10 uint48 sequence_number; copied
5168 * 11-12 uint16 length; olen - 13
5169 *
5170 * 13-13 HandshakeType msg_type; hello_verify_request
5171 * 14-16 uint24 length; olen - 25
5172 * 17-18 uint16 message_seq; copied
5173 * 19-21 uint24 fragment_offset; copied
5174 * 22-24 uint24 fragment_length; olen - 25
5175 *
5176 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5177 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5178 *
5179 * Minimum length is 28.
5180 */
5181 if( buf_len < 28 )
5182 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5183
5184 /* Copy most fields and adapt others */
Teppo Järvelin91d79382019-10-02 09:09:31 +03005185 mbedtls_platform_memcpy( obuf, in, 25 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005186 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5187 obuf[25] = 0xfe;
5188 obuf[26] = 0xff;
5189
5190 /* Generate and write actual cookie */
5191 p = obuf + 28;
5192 if( f_cookie_write( p_cookie,
5193 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5194 {
5195 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5196 }
5197
5198 *olen = p - obuf;
5199
5200 /* Go back and fill length fields */
5201 obuf[27] = (unsigned char)( *olen - 28 );
5202
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005203 (void)mbedtls_platform_put_uint24_be( &obuf[14], ( *olen - 25 ) );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005204 obuf[22] = obuf[14];
5205 obuf[23] = obuf[15];
5206 obuf[24] = obuf[16];
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005207
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005208 (void)mbedtls_platform_put_uint16_be( &obuf[11], ( *olen - 13 ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005209
5210 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5211}
5212
5213/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005214 * Handle possible client reconnect with the same UDP quadruplet
5215 * (RFC 6347 Section 4.2.8).
5216 *
5217 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5218 * that looks like a ClientHello.
5219 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005220 * - if the input looks like a ClientHello without cookies,
5221 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005222 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005223 * - if the input looks like a ClientHello with a valid cookie,
5224 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005225 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005226 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005227 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005228 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005229 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5230 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005231 */
5232static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5233{
5234 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005235 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005236
Hanno Becker87b56262019-07-10 14:37:41 +01005237 if( ssl->conf->f_cookie_write == NULL ||
5238 ssl->conf->f_cookie_check == NULL )
5239 {
5240 /* If we can't use cookies to verify reachability of the peer,
5241 * drop the record. */
5242 return( 0 );
5243 }
5244
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005245 ret = ssl_check_dtls_clihlo_cookie(
5246 ssl->conf->f_cookie_write,
5247 ssl->conf->f_cookie_check,
5248 ssl->conf->p_cookie,
5249 ssl->cli_id, ssl->cli_id_len,
5250 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005251 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005252
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005253 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5254
5255 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005256 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005257 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005258 * If the error is permanent we'll catch it later,
5259 * if it's not, then hopefully it'll work next time. */
Hanno Beckera58a8962019-06-13 16:11:15 +01005260 (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
Hanno Becker87b56262019-07-10 14:37:41 +01005261 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005262 }
5263
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005264 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005265 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005266 /* Got a valid cookie, partially reset context */
5267 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5268 {
5269 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5270 return( ret );
5271 }
5272
5273 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005274 }
5275
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005276 return( ret );
5277}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005278#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005279
Hanno Becker46483f12019-05-03 13:25:54 +01005280static int ssl_check_record_type( uint8_t record_type )
5281{
5282 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5283 record_type != MBEDTLS_SSL_MSG_ALERT &&
5284 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5285 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5286 {
5287 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5288 }
5289
5290 return( 0 );
5291}
5292
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005293/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005294 * ContentType type;
5295 * ProtocolVersion version;
5296 * uint16 epoch; // DTLS only
5297 * uint48 sequence_number; // DTLS only
5298 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005299 *
5300 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005301 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005302 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5303 *
5304 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005305 * 1. proceed with the record if this function returns 0
5306 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5307 * 3. return CLIENT_RECONNECT if this function return that value
5308 * 4. drop the whole datagram if this function returns anything else.
5309 * Point 2 is needed when the peer is resending, and we have already received
5310 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005311 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005312static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005313 unsigned char *buf,
5314 size_t len,
5315 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005316{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005317 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005318
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005319 size_t const rec_hdr_type_offset = 0;
5320 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005321
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005322 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5323 rec_hdr_type_len;
5324 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005325
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005326 size_t const rec_hdr_ctr_len = 8;
5327#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005328 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005329 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5330 rec_hdr_version_len;
5331
Hanno Beckera5a2b082019-05-15 14:03:01 +01005332#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005333 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5334 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005335 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005336#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5337#endif /* MBEDTLS_SSL_PROTO_DTLS */
5338
5339 size_t rec_hdr_len_offset; /* To be determined */
5340 size_t const rec_hdr_len_len = 2;
5341
5342 /*
5343 * Check minimum lengths for record header.
5344 */
5345
5346#if defined(MBEDTLS_SSL_PROTO_DTLS)
5347 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5348 {
5349 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5350 }
5351 MBEDTLS_SSL_TRANSPORT_ELSE
5352#endif /* MBEDTLS_SSL_PROTO_DTLS */
5353#if defined(MBEDTLS_SSL_PROTO_TLS)
5354 {
5355 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5356 }
5357#endif /* MBEDTLS_SSL_PROTO_DTLS */
5358
5359 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5360 {
5361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5362 (unsigned) len,
5363 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5364 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5365 }
5366
5367 /*
5368 * Parse and validate record content type
5369 */
5370
5371 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005372
5373 /* Check record content type */
5374#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5375 rec->cid_len = 0;
5376
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005377 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005378 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5379 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005380 {
5381 /* Shift pointers to account for record header including CID
5382 * struct {
5383 * ContentType special_type = tls12_cid;
5384 * ProtocolVersion version;
5385 * uint16 epoch;
5386 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005387 * opaque cid[cid_length]; // Additional field compared to
5388 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005389 * uint16 length;
5390 * opaque enc_content[DTLSCiphertext.length];
5391 * } DTLSCiphertext;
5392 */
5393
5394 /* So far, we only support static CID lengths
5395 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005396 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5397 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005398
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005399 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005400 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5402 (unsigned) len,
5403 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005404 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005405 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005406
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005407 /* configured CID len is guaranteed at most 255, see
5408 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5409 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005410 /* Not using more secure mbedtls_platform_memcpy as cid is public */
5411 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005412 }
5413 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005414#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005415 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005416 if( ssl_check_record_type( rec->type ) )
5417 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5419 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005420 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5421 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005422 }
5423
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005424 /*
5425 * Parse and validate record version
5426 */
5427
Hanno Becker8061c6e2019-07-26 08:07:03 +01005428 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5429 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005430 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5431 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005432 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005433
Hanno Becker2881d802019-05-22 14:44:53 +01005434 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5437 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005438 }
5439
Hanno Becker7bcf2b52019-07-26 09:02:40 +01005440 if( mbedtls_ssl_ver_gt( minor_ver,
5441 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5444 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005445 }
5446
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005447 /*
5448 * Parse/Copy record sequence number.
5449 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005450
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005451#if defined(MBEDTLS_SSL_PROTO_DTLS)
5452 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5453 {
5454 /* Copy explicit record sequence number from input buffer. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005455 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5456 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005457 rec_hdr_ctr_len );
5458 }
5459 MBEDTLS_SSL_TRANSPORT_ELSE
5460#endif /* MBEDTLS_SSL_PROTO_DTLS */
5461#if defined(MBEDTLS_SSL_PROTO_TLS)
5462 {
5463 /* Copy implicit record sequence number from SSL context structure. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005464 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5465 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005466 }
5467#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005468
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005469 /*
5470 * Parse record length.
5471 */
5472
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005473 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005474 rec->data_len = mbedtls_platform_get_uint16_be( &buf[rec_hdr_len_offset] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005475 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5476
Hanno Becker8b09b732019-05-08 12:03:28 +01005477 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005478 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005479 rec->type,
5480 major_ver, minor_ver, rec->data_len ) );
5481
5482 rec->buf = buf;
5483 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005484
Hanno Beckerec014082019-07-26 08:20:27 +01005485 if( rec->data_len == 0 )
5486 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5487
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005488 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005489 * DTLS-related tests.
5490 * Check epoch before checking length constraint because
5491 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5492 * message gets duplicated before the corresponding Finished message,
5493 * the second ChangeCipherSpec should be discarded because it belongs
5494 * to an old epoch, but not because its length is shorter than
5495 * the minimum record length for packets using the new record transform.
5496 * Note that these two kinds of failures are handled differently,
5497 * as an unexpected record is silently skipped but an invalid
5498 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005499 */
5500#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005501 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005502 {
Arto Kinnunena3fa06e2019-09-09 12:22:51 +03005503 rec_epoch = (uint32_t)mbedtls_platform_get_uint16_be( rec->ctr );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005504
Hanno Beckere0452772019-07-10 17:12:07 +01005505 /* Check that the datagram is large enough to contain a record
5506 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005507 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005508 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5510 (unsigned) len,
5511 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005512 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5513 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005514
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005515 /* Records from other, non-matching epochs are silently discarded.
5516 * (The case of same-port Client reconnects must be considered in
5517 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005518 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005519 {
5520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5521 "expected %d, received %d",
5522 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005523
5524 /* Records from the next epoch are considered for buffering
5525 * (concretely: early Finished messages). */
5526 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5527 {
5528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5529 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5530 }
5531
Hanno Becker87b56262019-07-10 14:37:41 +01005532 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005533 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005534#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005535 /* For records from the correct epoch, check whether their
5536 * sequence number has been seen before. */
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005537 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
5538 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005539 {
5540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5541 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5542 }
5543#endif
5544 }
5545#endif /* MBEDTLS_SSL_PROTO_DTLS */
5546
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005547 return( 0 );
5548}
Paul Bakker5121ce52009-01-03 21:22:43 +00005549
Hanno Becker87b56262019-07-10 14:37:41 +01005550
5551#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5552static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5553{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005554 unsigned int rec_epoch = (unsigned int)
5555 mbedtls_platform_get_uint16_be( &ssl->in_ctr[0] );
Hanno Becker87b56262019-07-10 14:37:41 +01005556
5557 /*
5558 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5559 * access the first byte of record content (handshake type), as we
5560 * have an active transform (possibly iv_len != 0), so use the
5561 * fact that the record header len is 13 instead.
5562 */
5563 if( rec_epoch == 0 &&
5564 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5565 MBEDTLS_SSL_IS_SERVER &&
5566 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5567 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5568 ssl->in_left > 13 &&
5569 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5570 {
5571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5572 "from the same port" ) );
5573 return( ssl_handle_possible_reconnect( ssl ) );
5574 }
5575
5576 return( 0 );
5577}
5578#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5579
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005580/*
5581 * If applicable, decrypt (and decompress) record content
5582 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005583static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5584 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005585{
5586 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005589 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005591#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5592 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005596 ret = mbedtls_ssl_hw_record_read( ssl );
5597 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5600 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005601 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005602
5603 if( ret == 0 )
5604 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005605 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005606#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005607 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005608 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005609 unsigned char const old_msg_type = rec->type;
5610
Hanno Becker611a83b2018-01-03 14:27:32 +00005611 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005612 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005615
Hanno Beckera5a2b082019-05-15 14:03:01 +01005616#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005617 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005618 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5619 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005620 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005621 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005622 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005623 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005624#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005625
Paul Bakker5121ce52009-01-03 21:22:43 +00005626 return( ret );
5627 }
5628
Hanno Becker106f3da2019-07-12 09:35:58 +01005629 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005630 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005631 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005632 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005633 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005634
Paul Bakker5121ce52009-01-03 21:22:43 +00005635 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005636 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005637
Hanno Beckera5a2b082019-05-15 14:03:01 +01005638#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005639 /* We have already checked the record content type
5640 * in ssl_parse_record_header(), failing or silently
5641 * dropping the record in the case of an unknown type.
5642 *
5643 * Since with the use of CIDs, the record content type
5644 * might change during decryption, re-check the record
5645 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005646 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005647 {
5648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5649 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5650 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005651#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005652
Hanno Becker106f3da2019-07-12 09:35:58 +01005653 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005654 {
5655#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005656 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005657 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005658 {
5659 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5661 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5662 }
5663#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5664
5665 ssl->nb_zero++;
5666
5667 /*
5668 * Three or more empty messages may be a DoS attack
5669 * (excessive CPU consumption).
5670 */
5671 if( ssl->nb_zero > 3 )
5672 {
5673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005674 "messages, possible DoS attack" ) );
5675 /* Treat the records as if they were not properly authenticated,
5676 * thereby failing the connection if we see more than allowed
5677 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005678 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5679 }
5680 }
5681 else
5682 ssl->nb_zero = 0;
5683
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005684 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5685#if defined(MBEDTLS_SSL_PROTO_TLS)
5686 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005687 {
5688 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005689 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005690 if( ++ssl->in_ctr[i - 1] != 0 )
5691 break;
5692
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005693 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005694 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005695 {
5696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5697 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5698 }
5699 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005700#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005701
Paul Bakker5121ce52009-01-03 21:22:43 +00005702 }
5703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005705 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005707 {
5708 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005710 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005711 return( ret );
5712 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005713 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005714#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005717 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005719 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005720 }
5721#endif
5722
Hanno Beckerf0242852019-07-09 17:30:02 +01005723 /* Check actual (decrypted) record content length against
5724 * configured maximum. */
5725 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5726 {
5727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5728 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5729 }
5730
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005731 return( 0 );
5732}
5733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005734static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005735
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005736/*
5737 * Read a record.
5738 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005739 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5740 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5741 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005742 */
Hanno Becker1097b342018-08-15 14:09:41 +01005743
5744/* Helper functions for mbedtls_ssl_read_record(). */
5745static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005746static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5747static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005748
Hanno Becker327c93b2018-08-15 13:56:18 +01005749int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005750 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005751{
5752 int ret;
5753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005755
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005756 if( ssl->keep_current_message == 0 )
5757 {
5758 do {
Simon Butcher99000142016-10-13 17:21:01 +01005759
Hanno Becker26994592018-08-15 14:14:59 +01005760 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005761 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005762 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005763
Hanno Beckere74d5562018-08-15 14:26:08 +01005764 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005765 {
Hanno Becker40f50842018-08-15 14:48:01 +01005766#if defined(MBEDTLS_SSL_PROTO_DTLS)
5767 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005768
Hanno Becker40f50842018-08-15 14:48:01 +01005769 /* We only check for buffered messages if the
5770 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005771 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005772 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005773 {
Hanno Becker40f50842018-08-15 14:48:01 +01005774 if( ssl_load_buffered_message( ssl ) == 0 )
5775 have_buffered = 1;
5776 }
5777
5778 if( have_buffered == 0 )
5779#endif /* MBEDTLS_SSL_PROTO_DTLS */
5780 {
5781 ret = ssl_get_next_record( ssl );
5782 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5783 continue;
5784
5785 if( ret != 0 )
5786 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005787 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005788 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005789 return( ret );
5790 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005791 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005792 }
5793
5794 ret = mbedtls_ssl_handle_message_type( ssl );
5795
Hanno Becker40f50842018-08-15 14:48:01 +01005796#if defined(MBEDTLS_SSL_PROTO_DTLS)
5797 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5798 {
5799 /* Buffer future message */
5800 ret = ssl_buffer_message( ssl );
5801 if( ret != 0 )
5802 return( ret );
5803
5804 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5805 }
5806#endif /* MBEDTLS_SSL_PROTO_DTLS */
5807
Hanno Becker90333da2017-10-10 11:27:13 +01005808 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5809 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005810
5811 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005812 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005813 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005814 return( ret );
5815 }
5816
Hanno Becker327c93b2018-08-15 13:56:18 +01005817 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005818 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005819 {
5820 mbedtls_ssl_update_handshake_status( ssl );
5821 }
Simon Butcher99000142016-10-13 17:21:01 +01005822 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005823 else
Simon Butcher99000142016-10-13 17:21:01 +01005824 {
Hanno Becker02f59072018-08-15 14:00:24 +01005825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005826 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005827 }
5828
5829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5830
5831 return( 0 );
5832}
5833
Hanno Becker40f50842018-08-15 14:48:01 +01005834#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005835static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005836{
Hanno Becker40f50842018-08-15 14:48:01 +01005837 if( ssl->in_left > ssl->next_record_offset )
5838 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005839
Hanno Becker40f50842018-08-15 14:48:01 +01005840 return( 0 );
5841}
5842
5843static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5844{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005845 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005846 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005847 int ret = 0;
5848
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005849 if( hs == NULL )
5850 return( -1 );
5851
Hanno Beckere00ae372018-08-20 09:39:42 +01005852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5853
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005854 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5855 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5856 {
5857 /* Check if we have seen a ChangeCipherSpec before.
5858 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005859 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005860 {
5861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5862 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005863 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005864 }
5865
Hanno Becker39b8bc92018-08-28 17:17:13 +01005866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005867 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5868 ssl->in_msglen = 1;
5869 ssl->in_msg[0] = 1;
5870
5871 /* As long as they are equal, the exact value doesn't matter. */
5872 ssl->in_left = 0;
5873 ssl->next_record_offset = 0;
5874
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005875 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005876 goto exit;
5877 }
Hanno Becker37f95322018-08-16 13:55:32 +01005878
Hanno Beckerb8f50142018-08-28 10:01:34 +01005879#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005880 /* Debug only */
5881 {
5882 unsigned offset;
5883 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5884 {
5885 hs_buf = &hs->buffering.hs[offset];
5886 if( hs_buf->is_valid == 1 )
5887 {
5888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5889 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005890 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005891 }
5892 }
5893 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005894#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005895
5896 /* Check if we have buffered and/or fully reassembled the
5897 * next handshake message. */
5898 hs_buf = &hs->buffering.hs[0];
5899 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5900 {
5901 /* Synthesize a record containing the buffered HS message. */
Arto Kinnunen84eeb4f2019-09-10 10:32:30 +03005902 size_t msg_len = mbedtls_platform_get_uint24_be( &hs_buf->data[1] );
Hanno Becker37f95322018-08-16 13:55:32 +01005903
5904 /* Double-check that we haven't accidentally buffered
5905 * a message that doesn't fit into the input buffer. */
5906 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5907 {
5908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5909 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5910 }
5911
5912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5913 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5914 hs_buf->data, msg_len + 12 );
5915
5916 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5917 ssl->in_hslen = msg_len + 12;
5918 ssl->in_msglen = msg_len + 12;
Teppo Järvelin91d79382019-10-02 09:09:31 +03005919 mbedtls_platform_memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
Hanno Becker37f95322018-08-16 13:55:32 +01005920
5921 ret = 0;
5922 goto exit;
5923 }
5924 else
5925 {
5926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5927 hs->in_msg_seq ) );
5928 }
5929
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005930 ret = -1;
5931
5932exit:
5933
5934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5935 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005936}
5937
Hanno Beckera02b0b42018-08-21 17:20:27 +01005938static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5939 size_t desired )
5940{
5941 int offset;
5942 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5944 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005945
Hanno Becker01315ea2018-08-21 17:22:17 +01005946 /* Get rid of future records epoch first, if such exist. */
5947 ssl_free_buffered_record( ssl );
5948
5949 /* Check if we have enough space available now. */
5950 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5951 hs->buffering.total_bytes_buffered ) )
5952 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005954 return( 0 );
5955 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005956
Hanno Becker4f432ad2018-08-28 10:02:32 +01005957 /* We don't have enough space to buffer the next expected handshake
5958 * message. Remove buffers used for future messages to gain space,
5959 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005960 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5961 offset >= 0; offset-- )
5962 {
5963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5964 offset ) );
5965
Hanno Beckerb309b922018-08-23 13:18:05 +01005966 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005967
5968 /* Check if we have enough space available now. */
5969 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5970 hs->buffering.total_bytes_buffered ) )
5971 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005973 return( 0 );
5974 }
5975 }
5976
5977 return( -1 );
5978}
5979
Hanno Becker40f50842018-08-15 14:48:01 +01005980static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5981{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005982 int ret = 0;
5983 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5984
5985 if( hs == NULL )
5986 return( 0 );
5987
5988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5989
5990 switch( ssl->in_msgtype )
5991 {
5992 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005994
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005995 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005996 break;
5997
5998 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005999 {
6000 unsigned recv_msg_seq_offset;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03006001 unsigned recv_msg_seq = (unsigned)
6002 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006003
Hanno Becker37f95322018-08-16 13:55:32 +01006004 mbedtls_ssl_hs_buffer *hs_buf;
6005 size_t msg_len = ssl->in_hslen - 12;
6006
6007 /* We should never receive an old handshake
6008 * message - double-check nonetheless. */
6009 if( recv_msg_seq < ssl->handshake->in_msg_seq )
6010 {
6011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6012 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6013 }
6014
6015 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
6016 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
6017 {
6018 /* Silently ignore -- message too far in the future */
6019 MBEDTLS_SSL_DEBUG_MSG( 2,
6020 ( "Ignore future HS message with sequence number %u, "
6021 "buffering window %u - %u",
6022 recv_msg_seq, ssl->handshake->in_msg_seq,
6023 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
6024
6025 goto exit;
6026 }
6027
6028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
6029 recv_msg_seq, recv_msg_seq_offset ) );
6030
6031 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
6032
6033 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01006034 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01006035 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006036 size_t reassembly_buf_sz;
6037
Hanno Becker37f95322018-08-16 13:55:32 +01006038 hs_buf->is_fragmented =
6039 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
6040
6041 /* We copy the message back into the input buffer
6042 * after reassembly, so check that it's not too large.
6043 * This is an implementation-specific limitation
6044 * and not one from the standard, hence it is not
6045 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01006046 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01006047 {
6048 /* Ignore message */
6049 goto exit;
6050 }
6051
Hanno Beckere0b150f2018-08-21 15:51:03 +01006052 /* Check if we have enough space to buffer the message. */
6053 if( hs->buffering.total_bytes_buffered >
6054 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
6055 {
6056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6057 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6058 }
6059
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006060 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
6061 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01006062
6063 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
6064 hs->buffering.total_bytes_buffered ) )
6065 {
6066 if( recv_msg_seq_offset > 0 )
6067 {
6068 /* If we can't buffer a future message because
6069 * of space limitations -- ignore. */
6070 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",
6071 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6072 (unsigned) hs->buffering.total_bytes_buffered ) );
6073 goto exit;
6074 }
Hanno Beckere1801392018-08-21 16:51:05 +01006075 else
6076 {
6077 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",
6078 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6079 (unsigned) hs->buffering.total_bytes_buffered ) );
6080 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006081
Hanno Beckera02b0b42018-08-21 17:20:27 +01006082 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006083 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01006084 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",
6085 (unsigned) msg_len,
6086 (unsigned) reassembly_buf_sz,
6087 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01006088 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006089 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6090 goto exit;
6091 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006092 }
6093
6094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
6095 msg_len ) );
6096
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006097 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
6098 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01006099 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01006100 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01006101 goto exit;
6102 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006103 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006104
6105 /* Prepare final header: copy msg_type, length and message_seq,
6106 * then add standardised fragment_offset and fragment_length */
Teppo Järvelin91d79382019-10-02 09:09:31 +03006107 mbedtls_platform_memcpy( hs_buf->data, ssl->in_msg, 6 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02006108 mbedtls_platform_memset( hs_buf->data + 6, 0, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006109 mbedtls_platform_memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
Hanno Becker37f95322018-08-16 13:55:32 +01006110
6111 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01006112
6113 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006114 }
6115 else
6116 {
6117 /* Make sure msg_type and length are consistent */
Teppo Järvelin0efac532019-10-04 13:21:08 +03006118 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 ) // use regular memcmp as msg type is public
Hanno Becker37f95322018-08-16 13:55:32 +01006119 {
6120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6121 /* Ignore */
6122 goto exit;
6123 }
6124 }
6125
Hanno Becker4422bbb2018-08-20 09:40:19 +01006126 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006127 {
6128 size_t frag_len, frag_off;
6129 unsigned char * const msg = hs_buf->data + 12;
6130
6131 /*
6132 * Check and copy current fragment
6133 */
6134
6135 /* Validation of header fields already done in
6136 * mbedtls_ssl_prepare_handshake_record(). */
6137 frag_off = ssl_get_hs_frag_off( ssl );
6138 frag_len = ssl_get_hs_frag_len( ssl );
6139
6140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6141 frag_off, frag_len ) );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006142 mbedtls_platform_memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
Hanno Becker37f95322018-08-16 13:55:32 +01006143
6144 if( hs_buf->is_fragmented )
6145 {
6146 unsigned char * const bitmask = msg + msg_len;
6147 ssl_bitmask_set( bitmask, frag_off, frag_len );
6148 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6149 msg_len ) == 0 );
6150 }
6151 else
6152 {
6153 hs_buf->is_complete = 1;
6154 }
6155
6156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6157 hs_buf->is_complete ? "" : "not yet " ) );
6158 }
6159
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006160 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006161 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006162
6163 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006164 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006165 break;
6166 }
6167
6168exit:
6169
6170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6171 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006172}
6173#endif /* MBEDTLS_SSL_PROTO_DTLS */
6174
Hanno Becker1097b342018-08-15 14:09:41 +01006175static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006176{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006177 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006178 * Consume last content-layer message and potentially
6179 * update in_msglen which keeps track of the contents'
6180 * consumption state.
6181 *
6182 * (1) Handshake messages:
6183 * Remove last handshake message, move content
6184 * and adapt in_msglen.
6185 *
6186 * (2) Alert messages:
6187 * Consume whole record content, in_msglen = 0.
6188 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006189 * (3) Change cipher spec:
6190 * Consume whole record content, in_msglen = 0.
6191 *
6192 * (4) Application data:
6193 * Don't do anything - the record layer provides
6194 * the application data as a stream transport
6195 * and consumes through mbedtls_ssl_read only.
6196 *
6197 */
6198
6199 /* Case (1): Handshake messages */
6200 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006201 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006202 /* Hard assertion to be sure that no application data
6203 * is in flight, as corrupting ssl->in_msglen during
6204 * ssl->in_offt != NULL is fatal. */
6205 if( ssl->in_offt != NULL )
6206 {
6207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6208 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6209 }
6210
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006211 /*
6212 * Get next Handshake message in the current record
6213 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006214
Hanno Becker4a810fb2017-05-24 16:27:30 +01006215 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006216 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006217 * current handshake content: If DTLS handshake
6218 * fragmentation is used, that's the fragment
6219 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006220 * size here is faulty and should be changed at
6221 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006222 * (2) While it doesn't seem to cause problems, one
6223 * has to be very careful not to assume that in_hslen
6224 * is always <= in_msglen in a sensible communication.
6225 * Again, it's wrong for DTLS handshake fragmentation.
6226 * The following check is therefore mandatory, and
6227 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006228 * Additionally, ssl->in_hslen might be arbitrarily out of
6229 * bounds after handling a DTLS message with an unexpected
6230 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006231 */
6232 if( ssl->in_hslen < ssl->in_msglen )
6233 {
6234 ssl->in_msglen -= ssl->in_hslen;
6235 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6236 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006237
Hanno Becker4a810fb2017-05-24 16:27:30 +01006238 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6239 ssl->in_msg, ssl->in_msglen );
6240 }
6241 else
6242 {
6243 ssl->in_msglen = 0;
6244 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006245
Hanno Becker4a810fb2017-05-24 16:27:30 +01006246 ssl->in_hslen = 0;
6247 }
6248 /* Case (4): Application data */
6249 else if( ssl->in_offt != NULL )
6250 {
6251 return( 0 );
6252 }
6253 /* Everything else (CCS & Alerts) */
6254 else
6255 {
6256 ssl->in_msglen = 0;
6257 }
6258
Hanno Becker1097b342018-08-15 14:09:41 +01006259 return( 0 );
6260}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006261
Hanno Beckere74d5562018-08-15 14:26:08 +01006262static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6263{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006264 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006265 return( 1 );
6266
6267 return( 0 );
6268}
6269
Hanno Becker5f066e72018-08-16 14:56:31 +01006270#if defined(MBEDTLS_SSL_PROTO_DTLS)
6271
6272static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6273{
6274 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6275 if( hs == NULL )
6276 return;
6277
Hanno Becker01315ea2018-08-21 17:22:17 +01006278 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006279 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006280 hs->buffering.total_bytes_buffered -=
6281 hs->buffering.future_record.len;
6282
6283 mbedtls_free( hs->buffering.future_record.data );
6284 hs->buffering.future_record.data = NULL;
6285 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006286}
6287
6288static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6289{
6290 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6291 unsigned char * rec;
6292 size_t rec_len;
6293 unsigned rec_epoch;
6294
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006295 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006296 return( 0 );
6297
6298 if( hs == NULL )
6299 return( 0 );
6300
Hanno Becker5f066e72018-08-16 14:56:31 +01006301 rec = hs->buffering.future_record.data;
6302 rec_len = hs->buffering.future_record.len;
6303 rec_epoch = hs->buffering.future_record.epoch;
6304
6305 if( rec == NULL )
6306 return( 0 );
6307
Hanno Becker4cb782d2018-08-20 11:19:05 +01006308 /* Only consider loading future records if the
6309 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006310 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006311 return( 0 );
6312
Hanno Becker5f066e72018-08-16 14:56:31 +01006313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6314
6315 if( rec_epoch != ssl->in_epoch )
6316 {
6317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6318 goto exit;
6319 }
6320
6321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6322
6323 /* Double-check that the record is not too large */
6324 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6325 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6326 {
6327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6328 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6329 }
6330
Teppo Järvelin91d79382019-10-02 09:09:31 +03006331 mbedtls_platform_memcpy( ssl->in_hdr, rec, rec_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006332 ssl->in_left = rec_len;
6333 ssl->next_record_offset = 0;
6334
6335 ssl_free_buffered_record( ssl );
6336
6337exit:
6338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6339 return( 0 );
6340}
6341
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006342static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6343 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006344{
6345 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006346
6347 /* Don't buffer future records outside handshakes. */
6348 if( hs == NULL )
6349 return( 0 );
6350
6351 /* Only buffer handshake records (we are only interested
6352 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006353 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006354 return( 0 );
6355
6356 /* Don't buffer more than one future epoch record. */
6357 if( hs->buffering.future_record.data != NULL )
6358 return( 0 );
6359
Hanno Becker01315ea2018-08-21 17:22:17 +01006360 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006361 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006362 hs->buffering.total_bytes_buffered ) )
6363 {
6364 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",
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006365 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006366 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006367 return( 0 );
6368 }
6369
Hanno Becker5f066e72018-08-16 14:56:31 +01006370 /* Buffer record */
6371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6372 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006373 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006374
6375 /* ssl_parse_record_header() only considers records
6376 * of the next epoch as candidates for buffering. */
6377 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006378 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006379
6380 hs->buffering.future_record.data =
6381 mbedtls_calloc( 1, hs->buffering.future_record.len );
6382 if( hs->buffering.future_record.data == NULL )
6383 {
6384 /* If we run out of RAM trying to buffer a
6385 * record from the next epoch, just ignore. */
6386 return( 0 );
6387 }
6388
Teppo Järvelin91d79382019-10-02 09:09:31 +03006389 mbedtls_platform_memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006390
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006391 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006392 return( 0 );
6393}
6394
6395#endif /* MBEDTLS_SSL_PROTO_DTLS */
6396
Hanno Beckere74d5562018-08-15 14:26:08 +01006397static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006398{
6399 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006400 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006401
Hanno Becker5f066e72018-08-16 14:56:31 +01006402#if defined(MBEDTLS_SSL_PROTO_DTLS)
6403 /* We might have buffered a future record; if so,
6404 * and if the epoch matches now, load it.
6405 * On success, this call will set ssl->in_left to
6406 * the length of the buffered record, so that
6407 * the calls to ssl_fetch_input() below will
6408 * essentially be no-ops. */
6409 ret = ssl_load_buffered_record( ssl );
6410 if( ret != 0 )
6411 return( ret );
6412#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006413
Hanno Becker8b09b732019-05-08 12:03:28 +01006414 /* Ensure that we have enough space available for the default form
6415 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6416 * with no space for CIDs counted in). */
6417 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6418 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006421 return( ret );
6422 }
6423
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006424 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6425 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006427#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006428 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006429 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006430 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6431 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006432 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006433 if( ret != 0 )
6434 return( ret );
6435
6436 /* Fall through to handling of unexpected records */
6437 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6438 }
6439
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006440 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6441 {
Hanno Becker87b56262019-07-10 14:37:41 +01006442#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006443 /* Reset in pointers to default state for TLS/DTLS records,
6444 * assuming no CID and no offset between record content and
6445 * record plaintext. */
6446 ssl_update_in_pointers( ssl );
6447
Hanno Becker69412452019-07-12 08:33:49 +01006448 /* Setup internal message pointers from record structure. */
6449 ssl->in_msgtype = rec.type;
6450#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6451 ssl->in_len = ssl->in_cid + rec.cid_len;
6452#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006453 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006454 ssl->in_msglen = rec.data_len;
6455
Hanno Becker87b56262019-07-10 14:37:41 +01006456 ret = ssl_check_client_reconnect( ssl );
6457 if( ret != 0 )
6458 return( ret );
6459#endif
6460
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006461 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006462 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006463
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6465 "(header)" ) );
6466 }
6467 else
6468 {
6469 /* Skip invalid record and the rest of the datagram */
6470 ssl->next_record_offset = 0;
6471 ssl->in_left = 0;
6472
6473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6474 "(header)" ) );
6475 }
6476
6477 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006478 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006479 }
Hanno Becker87b56262019-07-10 14:37:41 +01006480 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006481#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006482 {
6483 return( ret );
6484 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006485 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006488 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006489 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006490 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006491 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006492 if( ssl->next_record_offset < ssl->in_left )
6493 {
6494 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6495 }
6496 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006497 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006498#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006499#if defined(MBEDTLS_SSL_PROTO_TLS)
6500 {
Hanno Beckere0452772019-07-10 17:12:07 +01006501 /*
6502 * Fetch record contents from underlying transport.
6503 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006504 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006505 if( ret != 0 )
6506 {
6507 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6508 return( ret );
6509 }
6510
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006511 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006512 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006513#endif /* MBEDTLS_SSL_PROTO_TLS */
6514
6515 /*
6516 * Decrypt record contents.
6517 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006518
Hanno Beckera89610a2019-07-11 13:07:45 +01006519 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006521#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006522 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006523 {
6524 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006525 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006526 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006527 /* Except when waiting for Finished as a bad mac here
6528 * probably means something went wrong in the handshake
6529 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6530 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6531 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6532 {
6533#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6534 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6535 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006536 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006537 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6538 }
6539#endif
6540 return( ret );
6541 }
6542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006543#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006544 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6545 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6548 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006549 }
6550#endif
6551
Hanno Becker4a810fb2017-05-24 16:27:30 +01006552 /* As above, invalid records cause
6553 * dismissal of the whole datagram. */
6554
6555 ssl->next_record_offset = 0;
6556 ssl->in_left = 0;
6557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006559 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006560 }
6561
6562 return( ret );
6563 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006564 MBEDTLS_SSL_TRANSPORT_ELSE
6565#endif /* MBEDTLS_SSL_PROTO_DTLS */
6566#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006567 {
6568 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6570 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006571 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006572 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006574 }
6575#endif
6576 return( ret );
6577 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006578#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006579 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006580
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006581
6582 /* Reset in pointers to default state for TLS/DTLS records,
6583 * assuming no CID and no offset between record content and
6584 * record plaintext. */
6585 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006586#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6587 ssl->in_len = ssl->in_cid + rec.cid_len;
6588#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006589 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006590
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006591 /* The record content type may change during decryption,
6592 * so re-read it. */
6593 ssl->in_msgtype = rec.type;
6594 /* Also update the input buffer, because unfortunately
6595 * the server-side ssl_parse_client_hello() reparses the
6596 * record header when receiving a ClientHello initiating
6597 * a renegotiation. */
6598 ssl->in_hdr[0] = rec.type;
6599 ssl->in_msg = rec.buf + rec.data_offset;
6600 ssl->in_msglen = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006601 (void)mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len );
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006602
Simon Butcher99000142016-10-13 17:21:01 +01006603 return( 0 );
6604}
6605
6606int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6607{
6608 int ret;
6609
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006610 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006611 * Handle particular types of records
6612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006614 {
Simon Butcher99000142016-10-13 17:21:01 +01006615 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6616 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006617 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006618 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006619 }
6620
Hanno Beckere678eaa2018-08-21 14:57:46 +01006621 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006622 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006623 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006624 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6626 ssl->in_msglen ) );
6627 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006628 }
6629
Hanno Beckere678eaa2018-08-21 14:57:46 +01006630 if( ssl->in_msg[0] != 1 )
6631 {
6632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6633 ssl->in_msg[0] ) );
6634 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6635 }
6636
6637#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006638 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006639 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6640 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6641 {
6642 if( ssl->handshake == NULL )
6643 {
6644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6645 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6646 }
6647
6648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6649 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6650 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006651#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006652 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006654 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006655 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006656 if( ssl->in_msglen != 2 )
6657 {
6658 /* Note: Standard allows for more than one 2 byte alert
6659 to be packed in a single message, but Mbed TLS doesn't
6660 currently support this. */
6661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6662 ssl->in_msglen ) );
6663 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6664 }
6665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006667 ssl->in_msg[0], ssl->in_msg[1] ) );
6668
6669 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006670 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006671 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006675 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006676 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006677 }
6678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006679 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6680 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6683 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006684 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006685
6686#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6687 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6688 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6689 {
Hanno Becker90333da2017-10-10 11:27:13 +01006690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006691 /* Will be handled when trying to parse ServerHello */
6692 return( 0 );
6693 }
6694#endif
6695
6696#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006697 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006698 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6699 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006700 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6701 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6702 {
6703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6704 /* Will be handled in mbedtls_ssl_parse_certificate() */
6705 return( 0 );
6706 }
6707#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6708
6709 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006710 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006711 }
6712
Hanno Beckerc76c6192017-06-06 10:03:17 +01006713#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006714 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006715 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006716 /* Drop unexpected ApplicationData records,
6717 * except at the beginning of renegotiations */
6718 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6719 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6720#if defined(MBEDTLS_SSL_RENEGOTIATION)
6721 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6722 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006723#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006724 )
6725 {
6726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6727 return( MBEDTLS_ERR_SSL_NON_FATAL );
6728 }
6729
6730 if( ssl->handshake != NULL &&
6731 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6732 {
6733 ssl_handshake_wrapup_free_hs_transform( ssl );
6734 }
6735 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006736#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006737
Paul Bakker5121ce52009-01-03 21:22:43 +00006738 return( 0 );
6739}
6740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006741int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006742{
6743 int ret;
6744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6746 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6747 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006748 {
6749 return( ret );
6750 }
6751
6752 return( 0 );
6753}
6754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006756 unsigned char level,
6757 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006758{
6759 int ret;
6760
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006761 if( ssl == NULL || ssl->conf == NULL )
6762 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006765 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006767 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006768 ssl->out_msglen = 2;
6769 ssl->out_msg[0] = level;
6770 ssl->out_msg[1] = message;
6771
Hanno Becker67bc7c32018-08-06 11:33:50 +01006772 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006775 return( ret );
6776 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006778
6779 return( 0 );
6780}
6781
Hanno Becker17572472019-02-08 07:19:04 +00006782#if defined(MBEDTLS_X509_CRT_PARSE_C)
6783static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6784{
6785#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6786 if( session->peer_cert != NULL )
6787 {
6788 mbedtls_x509_crt_free( session->peer_cert );
6789 mbedtls_free( session->peer_cert );
6790 session->peer_cert = NULL;
6791 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006792#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006793 if( session->peer_cert_digest != NULL )
6794 {
6795 /* Zeroization is not necessary. */
6796 mbedtls_free( session->peer_cert_digest );
6797 session->peer_cert_digest = NULL;
6798 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6799 session->peer_cert_digest_len = 0;
6800 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006801#else
6802 ((void) session);
6803#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006804}
6805#endif /* MBEDTLS_X509_CRT_PARSE_C */
6806
Paul Bakker5121ce52009-01-03 21:22:43 +00006807/*
6808 * Handshake functions
6809 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006810#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006811/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006813{
Hanno Beckerdf645962019-06-26 13:02:22 +01006814 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6815 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006818
Hanno Becker5097cba2019-02-05 13:36:46 +00006819 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006822 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6823 {
6824 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6825 }
6826 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6827 {
6828 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6829 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006830 else
6831 {
6832 ssl->state = MBEDTLS_SSL_INVALID;
6833 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006834 return( 0 );
6835 }
6836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6838 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006839}
6840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006841int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006842{
Hanno Beckerdf645962019-06-26 13:02:22 +01006843 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6844 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006847
Hanno Becker5097cba2019-02-05 13:36:46 +00006848 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006851 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6852 {
6853 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6854 }
6855 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6856 {
6857 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6858 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006859 else
6860 {
6861 ssl->state = MBEDTLS_SSL_INVALID;
6862 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006863 return( 0 );
6864 }
6865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6867 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006868}
Gilles Peskinef9828522017-05-03 12:28:43 +02006869
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006870#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006871/* Some certificate support -> implement write and parse */
6872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006873int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006874{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006875 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006876 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006877 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006878 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6879 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006882
Hanno Becker5097cba2019-02-05 13:36:46 +00006883 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006886 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6887 {
6888 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6889 }
6890 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6891 {
6892 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6893 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006894 else
6895 {
6896 ssl->state = MBEDTLS_SSL_INVALID;
6897 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006898 return( 0 );
6899 }
6900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006901#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006902 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6903 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006904 {
6905 if( ssl->client_auth == 0 )
6906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006908 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6909 {
6910 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6911 }
6912 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6913 {
6914 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6915 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006916 else
6917 {
6918 ssl->state = MBEDTLS_SSL_INVALID;
6919 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006920 return( 0 );
6921 }
6922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006924 /*
6925 * If using SSLv3 and got no cert, send an Alert message
6926 * (otherwise an empty Certificate message will be sent).
6927 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006929 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006930 {
6931 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006932 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6933 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6934 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006937 goto write_msg;
6938 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006940 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006941#endif /* MBEDTLS_SSL_CLI_C */
6942#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006943 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6948 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006949 }
6950 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006951#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006954
6955 /*
6956 * 0 . 0 handshake type
6957 * 1 . 3 handshake length
6958 * 4 . 6 length of all certs
6959 * 7 . 9 length of cert. 1
6960 * 10 . n-1 peer certificate
6961 * n . n+2 length of cert. 2
6962 * n+3 . ... upper level cert, etc.
6963 */
6964 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006966
Paul Bakker29087132010-03-21 21:03:34 +00006967 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006968 {
6969 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006970 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006973 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006975 }
6976
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006977 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006978
Teppo Järvelin91d79382019-10-02 09:09:31 +03006979 i += 3; mbedtls_platform_memcpy( ssl->out_msg + i, crt->raw.p, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006980 i += n; crt = crt->next;
6981 }
6982
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006983 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006984
6985 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6987 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006988
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006989#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006990write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006991#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006992
Jarno Lamsa2b205162019-11-12 15:36:21 +02006993 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6994 {
6995 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6996 }
6997 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6998 {
6999 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7000 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007001 else
7002 {
7003 ssl->state = MBEDTLS_SSL_INVALID;
7004 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007005
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007006 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007007 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007008 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007009 return( ret );
7010 }
7011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007013
Paul Bakkered27a042013-04-18 22:46:23 +02007014 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007015}
7016
Hanno Becker285ff0c2019-01-31 07:44:03 +00007017#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00007018
7019#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00007020static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7021 unsigned char *crt_buf,
7022 size_t crt_buf_len )
7023{
7024 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7025
7026 if( peer_crt == NULL )
7027 return( -1 );
7028
7029 if( peer_crt->raw.len != crt_buf_len )
7030 return( -1 );
7031
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007032 return( mbedtls_platform_memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007033}
Hanno Becker5882dd02019-06-06 16:25:57 +01007034#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00007035static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7036 unsigned char *crt_buf,
7037 size_t crt_buf_len )
7038{
7039 int ret;
7040 unsigned char const * const peer_cert_digest =
7041 ssl->session->peer_cert_digest;
7042 mbedtls_md_type_t const peer_cert_digest_type =
7043 ssl->session->peer_cert_digest_type;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007044 mbedtls_md_handle_t digest_info =
Hanno Beckerdf759382019-02-05 17:02:46 +00007045 mbedtls_md_info_from_type( peer_cert_digest_type );
7046 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7047 size_t digest_len;
7048
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007049 if( peer_cert_digest == NULL ||
7050 digest_info == MBEDTLS_MD_INVALID_HANDLE )
7051 {
Hanno Beckerdf759382019-02-05 17:02:46 +00007052 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007053 }
Hanno Beckerdf759382019-02-05 17:02:46 +00007054
7055 digest_len = mbedtls_md_get_size( digest_info );
7056 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
7057 return( -1 );
7058
7059 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
7060 if( ret != 0 )
7061 return( -1 );
7062
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007063 return( mbedtls_platform_memcmp( tmp_digest, peer_cert_digest, digest_len ) );
Hanno Beckerdf759382019-02-05 17:02:46 +00007064}
Hanno Becker5882dd02019-06-06 16:25:57 +01007065#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00007066#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00007067
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007068/*
7069 * Once the certificate message is read, parse it into a cert chain and
7070 * perform basic checks, but leave actual verification to the caller
7071 */
Hanno Becker35e41772019-02-05 15:37:23 +00007072static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
7073 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00007074{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007075 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00007076#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7077 int crt_cnt=0;
7078#endif
Paul Bakker23986e52011-04-24 08:57:21 +00007079 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02007080 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00007081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007085 mbedtls_ssl_pend_fatal_alert( ssl,
7086 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007087 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007088 }
7089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
7091 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007094 mbedtls_ssl_pend_fatal_alert( ssl,
7095 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007097 }
7098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007100
Paul Bakker5121ce52009-01-03 21:22:43 +00007101 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00007103 */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007104 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007105
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007106 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007110 mbedtls_ssl_pend_fatal_alert( ssl,
7111 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007113 }
7114
Hanno Becker33c3dc82019-01-30 14:46:46 +00007115 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7116 i += 3;
7117
Hanno Becker33c3dc82019-01-30 14:46:46 +00007118 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007119 while( i < ssl->in_hslen )
7120 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007121 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02007122 if ( i + 3 > ssl->in_hslen ) {
7123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007124 mbedtls_ssl_pend_fatal_alert( ssl,
7125 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02007126 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
7127 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007128 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7129 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007130 if( ssl->in_msg[i] != 0 )
7131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007133 mbedtls_ssl_pend_fatal_alert( ssl,
7134 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007135 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007136 }
7137
Hanno Becker33c3dc82019-01-30 14:46:46 +00007138 /* Read length of the next CRT in the chain. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007139 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007140 i += 3;
7141
7142 if( n < 128 || i + n > ssl->in_hslen )
7143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007145 mbedtls_ssl_pend_fatal_alert( ssl,
7146 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007148 }
7149
Hanno Becker33c3dc82019-01-30 14:46:46 +00007150 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00007151#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7152 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01007153 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7154 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00007155 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007156 {
Hanno Becker68b856d2019-02-08 14:00:04 +00007157 /* During client-side renegotiation, check that the server's
7158 * end-CRTs hasn't changed compared to the initial handshake,
7159 * mitigating the triple handshake attack. On success, reuse
7160 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00007161 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7162 if( ssl_check_peer_crt_unchanged( ssl,
7163 &ssl->in_msg[i],
7164 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007165 {
Hanno Becker35e41772019-02-05 15:37:23 +00007166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007167 mbedtls_ssl_pend_fatal_alert( ssl,
7168 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007169 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007170 }
Hanno Becker35e41772019-02-05 15:37:23 +00007171
7172 /* Now we can safely free the original chain. */
7173 ssl_clear_peer_cert( ssl->session );
7174 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007175#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7176
Hanno Becker33c3dc82019-01-30 14:46:46 +00007177 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007178#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007179 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007180#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007181 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007182 * it in-place from the input buffer instead of making a copy. */
7183 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7184#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007185 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007186 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007187 case 0: /*ok*/
7188 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7189 /* Ignore certificate with an unknown algorithm: maybe a
7190 prior certificate was already trusted. */
7191 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007192
Hanno Becker33c3dc82019-01-30 14:46:46 +00007193 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7194 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7195 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007196
Hanno Becker33c3dc82019-01-30 14:46:46 +00007197 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7198 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7199 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007200
Hanno Becker33c3dc82019-01-30 14:46:46 +00007201 default:
7202 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7203 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007204 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007205 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7206 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007207 }
7208
7209 i += n;
7210 }
7211
Hanno Becker35e41772019-02-05 15:37:23 +00007212 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007213 return( 0 );
7214}
7215
Hanno Beckerb8a08572019-02-05 12:49:06 +00007216#if defined(MBEDTLS_SSL_SRV_C)
7217static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7218{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007219 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007220 return( -1 );
7221
7222#if defined(MBEDTLS_SSL_PROTO_SSL3)
7223 /*
7224 * Check if the client sent an empty certificate
7225 */
Hanno Becker2881d802019-05-22 14:44:53 +01007226 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007227 {
7228 if( ssl->in_msglen == 2 &&
7229 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7230 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7231 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7232 {
7233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7234 return( 0 );
7235 }
7236
7237 return( -1 );
7238 }
7239#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7240
7241#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7242 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7243 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7244 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7245 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Teppo Järvelin0efac532019-10-04 13:21:08 +03007246 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) // use regular memcmp as comparing public data
Hanno Beckerb8a08572019-02-05 12:49:06 +00007247 {
7248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7249 return( 0 );
7250 }
7251
Hanno Beckerb8a08572019-02-05 12:49:06 +00007252#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7253 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007254
7255 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007256}
7257#endif /* MBEDTLS_SSL_SRV_C */
7258
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007259/* Check if a certificate message is expected.
7260 * Return either
7261 * - SSL_CERTIFICATE_EXPECTED, or
7262 * - SSL_CERTIFICATE_SKIP
7263 * indicating whether a Certificate message is expected or not.
7264 */
7265#define SSL_CERTIFICATE_EXPECTED 0
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007266#define SSL_CERTIFICATE_SKIP 0xff
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007267static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7268 int authmode )
7269{
Hanno Becker473f98f2019-06-26 10:27:32 +01007270 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007271 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007272
7273 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7274 return( SSL_CERTIFICATE_SKIP );
7275
7276#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007277 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007278 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007279 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7280 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7281 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007282 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007283 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007284
7285 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7286 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007287 ssl->session_negotiate->verify_result =
7288 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7289 return( SSL_CERTIFICATE_SKIP );
7290 }
7291 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007292#else
7293 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007294#endif /* MBEDTLS_SSL_SRV_C */
7295
7296 return( SSL_CERTIFICATE_EXPECTED );
7297}
7298
Hanno Becker3cf50612019-02-05 14:36:34 +00007299static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007300 volatile int authmode,
Hanno Becker3cf50612019-02-05 14:36:34 +00007301 mbedtls_x509_crt *chain,
7302 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007303{
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007304 volatile int verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7305 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7306 volatile int flow_counter = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01007307 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007308 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007309 mbedtls_x509_crt *ca_chain;
7310 mbedtls_x509_crl *ca_crl;
7311
7312 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
Jarno Lamsae1621d42019-12-19 08:58:56 +02007313 {
Hanno Becker3cf50612019-02-05 14:36:34 +00007314 return( 0 );
Jarno Lamsae1621d42019-12-19 08:58:56 +02007315 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007316
Jarno Lamsae1621d42019-12-19 08:58:56 +02007317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Hanno Becker3cf50612019-02-05 14:36:34 +00007318#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7319 if( ssl->handshake->sni_ca_chain != NULL )
7320 {
7321 ca_chain = ssl->handshake->sni_ca_chain;
7322 ca_crl = ssl->handshake->sni_ca_crl;
7323 }
7324 else
7325#endif
7326 {
7327 ca_chain = ssl->conf->ca_chain;
7328 ca_crl = ssl->conf->ca_crl;
7329 }
7330
7331 /*
7332 * Main check: verify certificate
7333 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007334 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007335 chain,
7336 ca_chain, ca_crl,
7337 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007338#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007339 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007340#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007341 &ssl->session_negotiate->verify_result,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01007342#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
7343 ssl->conf->f_vrfy, ssl->conf->p_vrfy,
7344#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
7345 rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007346
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007347 if( verify_ret == 0 )
7348 {
7349 mbedtls_platform_enforce_volatile_reads();
7350 if( verify_ret == 0 )
7351 {
7352 flow_counter++;
7353 }
7354 else
7355 {
7356 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7357 }
7358 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007359 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007360 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007361 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007362 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007363 }
7364
7365#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007366 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007367 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7368#endif
7369
7370 /*
7371 * Secondary checks: always done, but change 'ret' only if it was 0
7372 */
7373
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007374#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007375 {
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007376#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007377 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007378#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007379 mbedtls_pk_context *pk;
7380 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7381 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007382 {
7383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007384 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007385 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007386
7387 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007388 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007389 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007390 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007391 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007392
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007393 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007394#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007395
7396 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007397 {
7398 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7399
7400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007401 if( verify_ret == 0 )
7402 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007403 flow_counter++;
7404 }
7405 if( ret == 0 )
7406 {
7407 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007408 }
7409 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007410#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007411
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007412 ret = mbedtls_ssl_check_cert_usage( chain,
Hanno Becker3cf50612019-02-05 14:36:34 +00007413 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007414 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7415 MBEDTLS_SSL_IS_CLIENT ),
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007416 &ssl->session_negotiate->verify_result );
7417 if( ret == 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007418 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007419 flow_counter++;
7420 }
7421 else
7422 {
7423 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007425 if( verify_ret == 0 )
7426 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007427 }
7428
7429 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7430 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7431 * with details encoded in the verification flags. All other kinds
7432 * of error codes, including those from the user provided f_vrfy
7433 * functions, are treated as fatal and lead to a failure of
7434 * ssl_parse_certificate even if verification was optional. */
7435 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007436 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7437 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007438 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007439 mbedtls_platform_enforce_volatile_reads();
7440 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7441 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7442 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7443 {
7444 verify_ret = 0;
7445 flow_counter++;
7446 }
7447 else
7448 {
7449 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7450 }
7451 } else {
7452 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007453 }
7454
7455 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7456 {
7457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007458 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007459 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007460 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007461 else
7462 {
7463 flow_counter++;
7464 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007465
Hanno Becker8c13ee62019-02-26 16:48:17 +00007466 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007467 {
7468 uint8_t alert;
7469
7470 /* The certificate may have been rejected for several reasons.
7471 Pick one and send the corresponding alert. Which alert to send
7472 may be a subject of debate in some cases. */
7473 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7474 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7475 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7476 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7477 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7478 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7479 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7480 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7481 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7482 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7483 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7484 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7485 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7486 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7487 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7488 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7489 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7490 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7491 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7492 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7493 else
7494 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007495 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007496 }
7497
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007498 if( verify_ret == 0 &&
7499#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7500 flow_counter == 5 )
7501#else
7502 flow_counter == 4 )
7503#endif
7504 {
7505 mbedtls_platform_enforce_volatile_reads();
7506 if( verify_ret == 0 &&
7507#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7508 flow_counter == 5 )
7509#else
7510 flow_counter == 4 )
7511#endif
7512 {
Jarno Lamsae1621d42019-12-19 08:58:56 +02007513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER AUTHENTICATED" ) );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007514 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
7515 }
7516 else
7517 {
7518 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7519 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007520 } else {
7521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER NOT AUTHENTICATED, %d", flow_counter));
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007522 }
7523
Hanno Becker3cf50612019-02-05 14:36:34 +00007524#if defined(MBEDTLS_DEBUG_C)
7525 if( ssl->session_negotiate->verify_result != 0 )
7526 {
7527 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7528 ssl->session_negotiate->verify_result ) );
7529 }
7530 else
7531 {
7532 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7533 }
7534#endif /* MBEDTLS_DEBUG_C */
7535
Hanno Becker8c13ee62019-02-26 16:48:17 +00007536 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007537}
7538
Hanno Becker34106f62019-02-08 14:59:05 +00007539#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007540
7541#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007542static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7543 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007544{
7545 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007546 /* Remember digest of the peer's end-CRT. */
7547 ssl->session_negotiate->peer_cert_digest =
7548 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7549 if( ssl->session_negotiate->peer_cert_digest == NULL )
7550 {
7551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7552 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007553 mbedtls_ssl_pend_fatal_alert( ssl,
7554 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007555
7556 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7557 }
7558
7559 ret = mbedtls_md( mbedtls_md_info_from_type(
7560 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7561 start, len,
7562 ssl->session_negotiate->peer_cert_digest );
7563
7564 ssl->session_negotiate->peer_cert_digest_type =
7565 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7566 ssl->session_negotiate->peer_cert_digest_len =
7567 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7568
7569 return( ret );
7570}
Hanno Becker5882dd02019-06-06 16:25:57 +01007571#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007572
7573static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7574 unsigned char *start, size_t len )
7575{
7576 unsigned char *end = start + len;
7577 int ret;
7578
7579 /* Make a copy of the peer's raw public key. */
7580 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7581 ret = mbedtls_pk_parse_subpubkey( &start, end,
7582 &ssl->handshake->peer_pubkey );
7583 if( ret != 0 )
7584 {
7585 /* We should have parsed the public key before. */
7586 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7587 }
7588
Manuel Pégourié-Gonnard2829bbf2019-09-19 10:45:14 +02007589 ssl->handshake->got_peer_pubkey = 1;
Hanno Becker34106f62019-02-08 14:59:05 +00007590 return( 0 );
7591}
7592#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7593
Hanno Becker3cf50612019-02-05 14:36:34 +00007594int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7595{
7596 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007597 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007598#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7599 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7600 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007601 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007602#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007603 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007604#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007605 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007606 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007607
7608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7609
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007610 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7611 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007612 {
7613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007614 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007615 }
7616
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007617#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7618 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007619 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007620 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007621 chain = ssl->handshake->ecrs_peer_cert;
7622 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007623 goto crt_verify;
7624 }
7625#endif
7626
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007627 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007628 {
7629 /* mbedtls_ssl_read_record may have sent an alert already. We
7630 let it decide whether to alert. */
7631 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007632 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007633 }
7634
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007635#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007636 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7637 {
7638 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007639
Hanno Beckerb8a08572019-02-05 12:49:06 +00007640 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007641 ret = 0;
7642 else
7643 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007644
Hanno Becker613d4902019-02-05 13:11:17 +00007645 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007646 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007647#endif /* MBEDTLS_SSL_SRV_C */
7648
Hanno Becker35e41772019-02-05 15:37:23 +00007649 /* Clear existing peer CRT structure in case we tried to
7650 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007651 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007652
7653 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7654 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007655 {
7656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7657 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007658 mbedtls_ssl_pend_fatal_alert( ssl,
7659 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007660
Hanno Beckere4aeb762019-02-05 17:19:52 +00007661 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7662 goto exit;
7663 }
7664 mbedtls_x509_crt_init( chain );
7665
7666 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007667 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007668 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007669
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007670#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7671 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007672 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007673
7674crt_verify:
7675 if( ssl->handshake->ecrs_enabled)
7676 rs_ctx = &ssl->handshake->ecrs_ctx;
7677#endif
7678
Hanno Becker3cf50612019-02-05 14:36:34 +00007679 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007680 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007681 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007682 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007683
Hanno Becker3008d282019-02-05 17:02:28 +00007684#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007685 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007686 size_t pk_len;
7687 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007688
Hanno Becker34106f62019-02-08 14:59:05 +00007689 /* We parse the CRT chain without copying, so
7690 * these pointers point into the input buffer,
7691 * and are hence still valid after freeing the
7692 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007693
Hanno Becker5882dd02019-06-06 16:25:57 +01007694#if defined(MBEDTLS_SSL_RENEGOTIATION)
7695 unsigned char *crt_start;
7696 size_t crt_len;
7697
Hanno Becker34106f62019-02-08 14:59:05 +00007698 crt_start = chain->raw.p;
7699 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007700#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007701
Hanno Becker8c13ee62019-02-26 16:48:17 +00007702 pk_start = chain->cache->pk_raw.p;
7703 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007704
7705 /* Free the CRT structures before computing
7706 * digest and copying the peer's public key. */
7707 mbedtls_x509_crt_free( chain );
7708 mbedtls_free( chain );
7709 chain = NULL;
7710
Hanno Becker5882dd02019-06-06 16:25:57 +01007711#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007712 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007713 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007714 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007715#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007716
Hanno Becker34106f62019-02-08 14:59:05 +00007717 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007718 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007719 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007720 }
Hanno Becker34106f62019-02-08 14:59:05 +00007721#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7722 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007723 ssl->session_negotiate->peer_cert = chain;
7724 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007725#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007728
Hanno Becker613d4902019-02-05 13:11:17 +00007729exit:
7730
Hanno Beckere4aeb762019-02-05 17:19:52 +00007731 if( ret == 0 )
Jarno Lamsa2b205162019-11-12 15:36:21 +02007732 {
7733 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7734 {
7735 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7736 }
7737 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7738 {
7739 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7740 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007741 else
7742 {
7743 ssl->state = MBEDTLS_SSL_INVALID;
7744 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02007745 }
Hanno Beckere4aeb762019-02-05 17:19:52 +00007746
7747#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7748 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7749 {
7750 ssl->handshake->ecrs_peer_cert = chain;
7751 chain = NULL;
7752 }
7753#endif
7754
7755 if( chain != NULL )
7756 {
7757 mbedtls_x509_crt_free( chain );
7758 mbedtls_free( chain );
7759 }
7760
Paul Bakker5121ce52009-01-03 21:22:43 +00007761 return( ret );
7762}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007763#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007766{
7767 int ret;
7768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007772 ssl->out_msglen = 1;
7773 ssl->out_msg[0] = 1;
7774
Jarno Lamsa2b205162019-11-12 15:36:21 +02007775 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7776 {
7777 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7778 }
7779 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7780 {
7781 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7782 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007783 else
7784 {
7785 ssl->state = MBEDTLS_SSL_INVALID;
7786 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007787
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007788 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007789 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007790 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007791 return( ret );
7792 }
7793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007795
7796 return( 0 );
7797}
7798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007800{
7801 int ret;
7802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007804
Hanno Becker327c93b2018-08-15 13:56:18 +01007805 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007808 return( ret );
7809 }
7810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007814 mbedtls_ssl_pend_fatal_alert( ssl,
7815 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007816 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007817 }
7818
Hanno Beckere678eaa2018-08-21 14:57:46 +01007819 /* CCS records are only accepted if they have length 1 and content '1',
7820 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007821
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007822 /*
7823 * Switch to our negotiated transform and session parameters for inbound
7824 * data.
7825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007827 ssl->transform_in = ssl->transform_negotiate;
7828 ssl->session_in = ssl->session_negotiate;
7829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007831 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007833#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007834 ssl_dtls_replay_reset( ssl );
7835#endif
7836
7837 /* Increment epoch */
7838 if( ++ssl->in_epoch == 0 )
7839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007841 /* This is highly unlikely to happen for legitimate reasons, so
7842 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007844 }
7845 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007846 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007848#if defined(MBEDTLS_SSL_PROTO_TLS)
7849 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02007850 mbedtls_platform_memset( ssl->in_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007851 }
7852#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007853
Hanno Beckerf5970a02019-05-08 09:38:41 +01007854 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7857 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007859 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007862 mbedtls_ssl_pend_fatal_alert( ssl,
7863 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007865 }
7866 }
7867#endif
7868
Jarno Lamsa2b205162019-11-12 15:36:21 +02007869 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7870 {
7871 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7872 }
7873 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7874 {
7875 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7876 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007877 else
7878 {
7879 ssl->state = MBEDTLS_SSL_INVALID;
7880 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007883
7884 return( 0 );
7885}
7886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007888{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7890 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007891 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007892 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007893#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7895#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007896 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007897#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007899 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007900#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007902}
7903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007905{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007907
7908 /*
7909 * Free our handshake params
7910 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007911 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007913 ssl->handshake = NULL;
7914
7915 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007916 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007917 */
7918 if( ssl->transform )
7919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920 mbedtls_ssl_transform_free( ssl->transform );
7921 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007922 }
7923 ssl->transform = ssl->transform_negotiate;
7924 ssl->transform_negotiate = NULL;
7925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007927}
7928
Jarno Lamsae1621d42019-12-19 08:58:56 +02007929int mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007930{
Jarno Lamsae1621d42019-12-19 08:58:56 +02007931 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007932
7933#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7934 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7935 ? ssl->handshake->sni_authmode
7936 : mbedtls_ssl_conf_get_authmode( ssl->conf );
7937#else
7938 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
7939#endif
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007940#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7941 volatile int crt_expected = SSL_CERTIFICATE_EXPECTED;
7942 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7943#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946#if defined(MBEDTLS_SSL_RENEGOTIATION)
7947 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007950 ssl->renego_records_seen = 0;
7951 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007952#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007953
7954 /*
7955 * Free the previous session and switch in the current one
7956 */
Paul Bakker0a597072012-09-25 21:55:46 +00007957 if( ssl->session )
7958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007960 /* RFC 7366 3.1: keep the EtM state */
7961 ssl->session_negotiate->encrypt_then_mac =
7962 ssl->session->encrypt_then_mac;
7963#endif
7964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 mbedtls_ssl_session_free( ssl->session );
7966 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007967 }
7968 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007969 ssl->session_negotiate = NULL;
7970
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007971#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007972 /*
7973 * Add cache entry
7974 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007975 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007976 ssl->session->id_len != 0 &&
Jarno Lamsa8d09e572019-12-19 15:20:19 +02007977 ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_UNSET )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007978 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007979 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007981 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007982#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007983
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007984 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
7985 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
7986#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7987 crt_expected == SSL_CERTIFICATE_SKIP )
7988#else
7989 1 )
7990#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007991 {
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007992 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
7993 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
7994#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7995 crt_expected == SSL_CERTIFICATE_SKIP )
7996#else
7997 1 )
7998#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007999 {
8000 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
8001 }
8002 else
8003 {
8004 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8005 goto cleanup;
8006 }
8007 }
8008
Jarno Lamsae1621d42019-12-19 08:58:56 +02008009#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008010 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008011 {
8012 mbedtls_platform_enforce_volatile_reads();
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008013 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008014 {
Jarno Lamsa06164052019-12-19 14:40:36 +02008015 /* When doing session resume, no premaster or peer authentication */
Jarno Lamsae1621d42019-12-19 08:58:56 +02008016 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsa06164052019-12-19 14:40:36 +02008017 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008018 }
8019 else
8020 {
8021 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008022 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008023 }
8024 }
8025#endif
8026
8027 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8028 {
8029 mbedtls_platform_enforce_volatile_reads();
8030 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8031 {
8032 ret = 0;
8033 }
8034 else
8035 {
8036 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008037 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008038 }
8039 }
8040 else
8041 {
8042 ret = MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008043 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008044 }
8045
Jarno Lamsa06164052019-12-19 14:40:36 +02008046 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8047 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8048 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8049 {
8050 mbedtls_platform_enforce_volatile_reads();
8051 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8052 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8053 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8054 {
8055 ret = 0;
8056 }
8057 else
8058 {
8059 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8060 goto cleanup;
8061 }
8062 }
8063 else
8064 {
8065 MBEDTLS_SSL_DEBUG_MSG( 3, ( "hello random %d", ssl->handshake->hello_random_set ) );
8066 MBEDTLS_SSL_DEBUG_MSG( 3, ( "key_derivation_done %d", ssl->handshake->key_derivation_done ) );
8067 MBEDTLS_SSL_DEBUG_MSG( 3, ( "premaster_generated %d", ssl->handshake->premaster_generated ) );
8068 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8069 }
8070
8071cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008073 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008074 ssl->handshake->flight != NULL )
8075 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008076 /* Cancel handshake timer */
8077 ssl_set_timer( ssl, 0 );
8078
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008079 /* Keep last flight around in case we need to resend it:
8080 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008082 }
8083 else
8084#endif
8085 ssl_handshake_wrapup_free_hs_transform( ssl );
8086
Jarno Lamsa2b205162019-11-12 15:36:21 +02008087 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Paul Bakker48916f92012-09-16 19:57:18 +00008088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008089 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Jarno Lamsae1621d42019-12-19 08:58:56 +02008090 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00008091}
8092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008093int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00008094{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008095 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00008096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008098
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008099 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01008100
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008101 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8102 mbedtls_ssl_suite_get_mac(
8103 mbedtls_ssl_ciphersuite_from_id(
8104 mbedtls_ssl_session_get_ciphersuite(
8105 ssl->session_negotiate ) ) ),
8106 ssl, ssl->out_msg + 4,
8107 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008108
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01008109 /*
8110 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8111 * may define some other value. Currently (early 2016), no defined
8112 * ciphersuite does this (and this is unlikely to change as activity has
8113 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8114 */
Hanno Becker2881d802019-05-22 14:44:53 +01008115 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008117#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008118 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008119 mbedtls_platform_memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008120#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008121
Paul Bakker5121ce52009-01-03 21:22:43 +00008122 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8124 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008125
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008126#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00008127 /*
8128 * In case of session resuming, invert the client and server
8129 * ChangeCipherSpec messages order.
8130 */
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008131 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008133#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008134 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8135 MBEDTLS_SSL_IS_CLIENT )
8136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008137 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008138 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008139#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008140#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008141 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8142 MBEDTLS_SSL_IS_SERVER )
8143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008144 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008145 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008146#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008147 }
8148 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008149#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008150 {
8151 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8152 {
8153 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8154 }
8155 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8156 {
8157 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8158 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008159 else
8160 {
8161 ssl->state = MBEDTLS_SSL_INVALID;
8162 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008163 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008164
Paul Bakker48916f92012-09-16 19:57:18 +00008165 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008166 * Switch to our negotiated transform and session parameters for outbound
8167 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008169 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008171#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008172 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008173 {
8174 unsigned char i;
8175
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008176 /* Remember current epoch settings for resending */
8177 ssl->handshake->alt_transform_out = ssl->transform_out;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008178 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008179
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008180 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008181 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008182
8183 /* Increment epoch */
8184 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008185 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008186 break;
8187
8188 /* The loop goes to its end iff the counter is wrapping */
8189 if( i == 0 )
8190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8192 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008193 }
8194 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008195 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008197#if defined(MBEDTLS_SSL_PROTO_TLS)
8198 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008199 mbedtls_platform_memset( ssl->cur_out_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008200 }
8201#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008202
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008203 ssl->transform_out = ssl->transform_negotiate;
8204 ssl->session_out = ssl->session_negotiate;
8205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008206#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8207 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008211 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8212 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008213 }
8214 }
8215#endif
8216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008218 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008219 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008220#endif
8221
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008222 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008223 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008224 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008225 return( ret );
8226 }
8227
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008228#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008229 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008230 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8231 {
8232 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8233 return( ret );
8234 }
8235#endif
8236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008238
8239 return( 0 );
8240}
8241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008242#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008243#define SSL_MAX_HASH_LEN 36
8244#else
8245#define SSL_MAX_HASH_LEN 12
8246#endif
8247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008248int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008249{
Paul Bakker23986e52011-04-24 08:57:21 +00008250 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008251 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008252 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008255
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008256 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8257 mbedtls_ssl_suite_get_mac(
8258 mbedtls_ssl_ciphersuite_from_id(
8259 mbedtls_ssl_session_get_ciphersuite(
8260 ssl->session_negotiate ) ) ),
8261 ssl, buf,
8262 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008263
Hanno Becker327c93b2018-08-15 13:56:18 +01008264 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008267 return( ret );
8268 }
8269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008270 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008273 mbedtls_ssl_pend_fatal_alert( ssl,
8274 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008276 }
8277
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008278 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008279#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01008280 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008281 hash_len = 36;
8282 else
8283#endif
8284 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008286 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8287 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008290 mbedtls_ssl_pend_fatal_alert( ssl,
8291 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008292 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008293 }
8294
Teppo Järvelin707ceb82019-10-04 07:49:39 +03008295 if( mbedtls_platform_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008296 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008299 mbedtls_ssl_pend_fatal_alert( ssl,
8300 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008301 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008302 }
8303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008305 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008306 mbedtls_platform_memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008307#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008308
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008309#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008310 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008312#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008313 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008314 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008315#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008317 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008318 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008319#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008320 }
8321 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008322#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008323 {
8324 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8325 {
8326 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8327 }
8328 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8329 {
8330 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8331 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008332 else
8333 {
8334 ssl->state = MBEDTLS_SSL_INVALID;
8335 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008336 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008338#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008339 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008340 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008341#endif
8342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008344
8345 return( 0 );
8346}
8347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008348static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008349{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008350 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008352#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8353 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8354 mbedtls_md5_init( &handshake->fin_md5 );
8355 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008356 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8357 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008358#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8360#if defined(MBEDTLS_SHA256_C)
8361 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008362 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008363#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008364#if defined(MBEDTLS_SHA512_C)
8365 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008366 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008367#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008368#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008369
Hanno Becker7e5437a2017-04-28 17:15:26 +01008370#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8371 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8372 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8373#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008375#if defined(MBEDTLS_DHM_C)
8376 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008377#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008378#if defined(MBEDTLS_ECDH_C)
8379 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008380#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008381#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008382 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008383#if defined(MBEDTLS_SSL_CLI_C)
8384 handshake->ecjpake_cache = NULL;
8385 handshake->ecjpake_cache_len = 0;
8386#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008387#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008388
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008389#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008390 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008391#endif
8392
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008393#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8394 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8395#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008396
8397#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8398 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8399 mbedtls_pk_init( &handshake->peer_pubkey );
8400#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008401}
8402
Hanno Becker611a83b2018-01-03 14:27:32 +00008403void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008404{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008407 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8408 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008409
Hanno Becker92231322018-01-03 15:32:51 +00008410#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008411 mbedtls_md_init( &transform->md_ctx_enc );
8412 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008413#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008414}
8415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008416void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008417{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008418 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008419}
8420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008421static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008422{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008423 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008424 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008425 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008426 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008427 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008428 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008429 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008430
8431 /*
8432 * Either the pointers are now NULL or cleared properly and can be freed.
8433 * Now allocate missing structures.
8434 */
8435 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008436 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008437 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008438 }
Paul Bakker48916f92012-09-16 19:57:18 +00008439
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008440 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008441 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008442 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008443 }
Paul Bakker48916f92012-09-16 19:57:18 +00008444
Paul Bakker82788fb2014-10-20 13:59:19 +02008445 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008446 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008447 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008448 }
Paul Bakker48916f92012-09-16 19:57:18 +00008449
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008450 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008451 if( ssl->handshake == NULL ||
8452 ssl->transform_negotiate == NULL ||
8453 ssl->session_negotiate == NULL )
8454 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008457 mbedtls_free( ssl->handshake );
8458 mbedtls_free( ssl->transform_negotiate );
8459 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008460
8461 ssl->handshake = NULL;
8462 ssl->transform_negotiate = NULL;
8463 ssl->session_negotiate = NULL;
8464
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008465 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008466 }
8467
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008468 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008469 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008470 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008471 ssl_handshake_params_init( ssl->handshake );
8472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008473#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008474 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008475 {
8476 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008477
Hanno Becker2d9623f2019-06-13 12:07:05 +01008478 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008479 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8480 else
8481 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8482 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008483#endif
8484
Paul Bakker48916f92012-09-16 19:57:18 +00008485 return( 0 );
8486}
8487
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008488#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008489/* Dummy cookie callbacks for defaults */
8490static int ssl_cookie_write_dummy( void *ctx,
8491 unsigned char **p, unsigned char *end,
8492 const unsigned char *cli_id, size_t cli_id_len )
8493{
8494 ((void) ctx);
8495 ((void) p);
8496 ((void) end);
8497 ((void) cli_id);
8498 ((void) cli_id_len);
8499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008500 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008501}
8502
8503static int ssl_cookie_check_dummy( void *ctx,
8504 const unsigned char *cookie, size_t cookie_len,
8505 const unsigned char *cli_id, size_t cli_id_len )
8506{
8507 ((void) ctx);
8508 ((void) cookie);
8509 ((void) cookie_len);
8510 ((void) cli_id);
8511 ((void) cli_id_len);
8512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008513 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008514}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008515#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008516
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008517/* Once ssl->out_hdr as the address of the beginning of the
8518 * next outgoing record is set, deduce the other pointers.
8519 *
8520 * Note: For TLS, we save the implicit record sequence number
8521 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8522 * and the caller has to make sure there's space for this.
8523 */
8524
8525static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8526 mbedtls_ssl_transform *transform )
8527{
8528#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008529 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008530 {
8531 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008532#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008533 ssl->out_cid = ssl->out_ctr + 8;
8534 ssl->out_len = ssl->out_cid;
8535 if( transform != NULL )
8536 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008537#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008538 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008539#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008540 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008541 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008542 MBEDTLS_SSL_TRANSPORT_ELSE
8543#endif /* MBEDTLS_SSL_PROTO_DTLS */
8544#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008545 {
8546 ssl->out_ctr = ssl->out_hdr - 8;
8547 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008548#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008549 ssl->out_cid = ssl->out_len;
8550#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008551 ssl->out_iv = ssl->out_hdr + 5;
8552 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008553#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008554
8555 /* Adjust out_msg to make space for explicit IV, if used. */
8556 if( transform != NULL &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008557 mbedtls_ssl_ver_geq(
8558 mbedtls_ssl_get_minor_ver( ssl ),
8559 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008560 {
8561 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8562 }
8563 else
8564 ssl->out_msg = ssl->out_iv;
8565}
8566
8567/* Once ssl->in_hdr as the address of the beginning of the
8568 * next incoming record is set, deduce the other pointers.
8569 *
8570 * Note: For TLS, we save the implicit record sequence number
8571 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8572 * and the caller has to make sure there's space for this.
8573 */
8574
Hanno Beckerf5970a02019-05-08 09:38:41 +01008575static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008576{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008577 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008578 * of unprotected TLS/DTLS records, with ssl->in_msg
8579 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008580 *
8581 * When decrypting a protected record, ssl->in_msg
8582 * will be shifted to point to the beginning of the
8583 * record plaintext.
8584 */
8585
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008586#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008587 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008588 {
Hanno Becker70e79282019-05-03 14:34:53 +01008589 /* This sets the header pointers to match records
8590 * without CID. When we receive a record containing
8591 * a CID, the fields are shifted accordingly in
8592 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008593 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008594#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008595 ssl->in_cid = ssl->in_ctr + 8;
8596 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008597#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008598 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008599#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008600 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008601 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008602 MBEDTLS_SSL_TRANSPORT_ELSE
8603#endif /* MBEDTLS_SSL_PROTO_DTLS */
8604#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008605 {
8606 ssl->in_ctr = ssl->in_hdr - 8;
8607 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008608#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008609 ssl->in_cid = ssl->in_len;
8610#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008611 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008612 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008613#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008614}
8615
Paul Bakker5121ce52009-01-03 21:22:43 +00008616/*
8617 * Initialize an SSL context
8618 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008619void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8620{
8621 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8622}
8623
8624/*
8625 * Setup an SSL context
8626 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008627
8628static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8629{
8630 /* Set the incoming and outgoing record pointers. */
8631#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008632 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008633 {
8634 ssl->out_hdr = ssl->out_buf;
8635 ssl->in_hdr = ssl->in_buf;
8636 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008637 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008638#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008639#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008640 {
8641 ssl->out_hdr = ssl->out_buf + 8;
8642 ssl->in_hdr = ssl->in_buf + 8;
8643 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008644#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008645
8646 /* Derive other internal pointers. */
8647 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008648 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008649}
8650
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008651int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008652 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008653{
Paul Bakker48916f92012-09-16 19:57:18 +00008654 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008655
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008656 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008657
Hanno Beckeref982d52019-07-23 15:56:18 +01008658#if defined(MBEDTLS_USE_TINYCRYPT)
8659 uECC_set_rng( &uecc_rng_wrapper );
8660#endif
8661
Paul Bakker62f2dee2012-09-28 07:31:51 +00008662 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008663 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008664 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008665
8666 /* Set to NULL in case of an error condition */
8667 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008668
Angus Grattond8213d02016-05-25 20:56:48 +10008669 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8670 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008671 {
Angus Grattond8213d02016-05-25 20:56:48 +10008672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008673 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008674 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008675 }
8676
8677 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8678 if( ssl->out_buf == NULL )
8679 {
8680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008681 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008682 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008683 }
8684
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008685 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008686
Paul Bakker48916f92012-09-16 19:57:18 +00008687 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008688 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008689
Hanno Beckerc8f52992019-07-25 11:15:08 +01008690 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008691
Paul Bakker5121ce52009-01-03 21:22:43 +00008692 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008693
8694error:
8695 mbedtls_free( ssl->in_buf );
8696 mbedtls_free( ssl->out_buf );
8697
8698 ssl->conf = NULL;
8699
8700 ssl->in_buf = NULL;
8701 ssl->out_buf = NULL;
8702
8703 ssl->in_hdr = NULL;
8704 ssl->in_ctr = NULL;
8705 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008706 ssl->in_msg = NULL;
8707
8708 ssl->out_hdr = NULL;
8709 ssl->out_ctr = NULL;
8710 ssl->out_len = NULL;
8711 ssl->out_iv = NULL;
8712 ssl->out_msg = NULL;
8713
k-stachowiak9f7798e2018-07-31 16:52:32 +02008714 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008715}
8716
8717/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008718 * Reset an initialized and used SSL context for re-use while retaining
8719 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008720 *
8721 * If partial is non-zero, keep data in the input buffer and client ID.
8722 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008723 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008724static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008725{
Paul Bakker48916f92012-09-16 19:57:18 +00008726 int ret;
8727
Hanno Becker7e772132018-08-10 12:38:21 +01008728#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8729 !defined(MBEDTLS_SSL_SRV_C)
8730 ((void) partial);
8731#endif
8732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008733 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008734
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008735 /* Cancel any possibly running timer */
8736 ssl_set_timer( ssl, 0 );
8737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008738#if defined(MBEDTLS_SSL_RENEGOTIATION)
8739 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008740 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008741
8742 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008743 mbedtls_platform_memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8744 mbedtls_platform_memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008745#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008746 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008747
Paul Bakker7eb013f2011-10-06 12:37:39 +00008748 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008749 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008750
8751 ssl->in_msgtype = 0;
8752 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008753#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008754 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008755 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008756#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008757#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008758 ssl_dtls_replay_reset( ssl );
8759#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008760
8761 ssl->in_hslen = 0;
8762 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008763
8764 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008765
8766 ssl->out_msgtype = 0;
8767 ssl->out_msglen = 0;
8768 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008769#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8770 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008771 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008772#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008773
Hanno Becker19859472018-08-06 09:40:20 +01008774 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8775
Paul Bakker48916f92012-09-16 19:57:18 +00008776 ssl->transform_in = NULL;
8777 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008778
Hanno Becker78640902018-08-13 16:35:15 +01008779 ssl->session_in = NULL;
8780 ssl->session_out = NULL;
8781
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008782 mbedtls_platform_memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008783
8784#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008785 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008786#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8787 {
8788 ssl->in_left = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008789 mbedtls_platform_memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008790 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008792#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8793 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8796 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008798 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8799 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008800 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008801 }
8802#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008803
Paul Bakker48916f92012-09-16 19:57:18 +00008804 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008806 mbedtls_ssl_transform_free( ssl->transform );
8807 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008808 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008809 }
Paul Bakker48916f92012-09-16 19:57:18 +00008810
Paul Bakkerc0463502013-02-14 11:19:38 +01008811 if( ssl->session )
8812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008813 mbedtls_ssl_session_free( ssl->session );
8814 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008815 ssl->session = NULL;
8816 }
8817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008818#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008819 ssl->alpn_chosen = NULL;
8820#endif
8821
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008822#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008823#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008824 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008825#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008826 {
8827 mbedtls_free( ssl->cli_id );
8828 ssl->cli_id = NULL;
8829 ssl->cli_id_len = 0;
8830 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008831#endif
8832
Paul Bakker48916f92012-09-16 19:57:18 +00008833 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8834 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008835
8836 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008837}
8838
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008839/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008840 * Reset an initialized and used SSL context for re-use while retaining
8841 * all application-set variables, function pointers and data.
8842 */
8843int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8844{
8845 return( ssl_session_reset_int( ssl, 0 ) );
8846}
8847
8848/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008849 * SSL set accessors
8850 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008851#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008852void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008853{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008854 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008855}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008856#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008857
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008858void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008859{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008860 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008861}
8862
Hanno Becker7f376f42019-06-12 16:20:48 +01008863#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8864 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008865void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008866{
Hanno Becker7f376f42019-06-12 16:20:48 +01008867 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008868}
Hanno Becker7f376f42019-06-12 16:20:48 +01008869#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008870
Hanno Beckerde671542019-06-12 16:30:46 +01008871#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8872 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8873void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8874 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008875{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008876 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008877}
Hanno Beckerde671542019-06-12 16:30:46 +01008878#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008880#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008881
Hanno Becker1841b0a2018-08-24 11:13:57 +01008882void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8883 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008884{
8885 ssl->disable_datagram_packing = !allow_packing;
8886}
8887
Hanno Becker1f835fa2019-06-13 10:14:59 +01008888#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8889 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008890void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8891 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008892{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008893 conf->hs_timeout_min = min;
8894 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008895}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008896#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8897 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8898void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8899 uint32_t min, uint32_t max )
8900{
8901 ((void) conf);
8902 ((void) min);
8903 ((void) max);
8904}
8905#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8906 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8907
8908#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008909
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008910void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008911{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008912#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8913 conf->authmode = authmode;
8914#else
8915 ((void) conf);
8916 ((void) authmode);
8917#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008918}
8919
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008920#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8921 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008922void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008923 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008924 void *p_vrfy )
8925{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008926 conf->f_vrfy = f_vrfy;
8927 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008928}
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008929#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008930
Hanno Beckerece325c2019-06-13 15:39:27 +01008931#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008932void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008933 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008934 void *p_rng )
8935{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008936 conf->f_rng = f_rng;
8937 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008938}
Hanno Beckerece325c2019-06-13 15:39:27 +01008939#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008940
Hanno Becker14a4a442019-07-02 17:00:34 +01008941#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008942void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008943 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008944 void *p_dbg )
8945{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008946 conf->f_dbg = f_dbg;
8947 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008948}
Hanno Becker14a4a442019-07-02 17:00:34 +01008949#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008950
Hanno Beckera58a8962019-06-13 16:11:15 +01008951#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8952 !defined(MBEDTLS_SSL_CONF_SEND) && \
8953 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008954void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008955 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008956 mbedtls_ssl_send_t *f_send,
8957 mbedtls_ssl_recv_t *f_recv,
8958 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008959{
Hanno Beckera58a8962019-06-13 16:11:15 +01008960 ssl->p_bio = p_bio;
8961 ssl->f_send = f_send;
8962 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008963 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008964}
Hanno Beckera58a8962019-06-13 16:11:15 +01008965#else
8966void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8967 void *p_bio )
8968{
8969 ssl->p_bio = p_bio;
8970}
8971#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008972
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008973#if defined(MBEDTLS_SSL_PROTO_DTLS)
8974void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8975{
8976 ssl->mtu = mtu;
8977}
8978#endif
8979
Hanno Becker1f835fa2019-06-13 10:14:59 +01008980#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008981void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008982{
8983 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008984}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008985#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008986
Hanno Becker0ae6b242019-06-13 16:45:36 +01008987#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
8988 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008989void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8990 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008991 mbedtls_ssl_set_timer_t *f_set_timer,
8992 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008993{
8994 ssl->p_timer = p_timer;
8995 ssl->f_set_timer = f_set_timer;
8996 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008997 /* Make sure we start with no timer running */
8998 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008999}
Hanno Becker0ae6b242019-06-13 16:45:36 +01009000#else
9001void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
9002 void *p_timer )
9003{
9004 ssl->p_timer = p_timer;
9005 /* Make sure we start with no timer running */
9006 ssl_set_timer( ssl, 0 );
9007}
9008#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009009
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009010#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009011void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009012 void *p_cache,
9013 int (*f_get_cache)(void *, mbedtls_ssl_session *),
9014 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00009015{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009016 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009017 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009018 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00009019}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009020#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009021
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009022#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009023int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00009024{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009025 int ret;
9026
9027 if( ssl == NULL ||
9028 session == NULL ||
9029 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009030 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009032 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009033 }
9034
Hanno Becker58fccf22019-02-06 14:30:46 +00009035 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
9036 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009037 return( ret );
9038
Jarno Lamsa8d09e572019-12-19 15:20:19 +02009039 ssl->handshake->resume = MBEDTLS_SSL_FI_FLAG_SET;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009040
9041 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009042}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009043#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009044
Hanno Becker73f4cb12019-06-27 13:51:07 +01009045#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009046void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009047 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00009048{
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009049 conf->ciphersuite_list[0] = ciphersuites;
9050 conf->ciphersuite_list[1] = ciphersuites;
9051 conf->ciphersuite_list[2] = ciphersuites;
9052 conf->ciphersuite_list[3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009053}
9054
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009055void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02009056 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009057 int major, int minor )
9058{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009059 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009060 return;
9061
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009062 if( mbedtls_ssl_ver_lt( minor, MBEDTLS_SSL_MINOR_VERSION_0 ) ||
9063 mbedtls_ssl_ver_gt( minor, MBEDTLS_SSL_MINOR_VERSION_3 ) )
9064 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009065 return;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009066 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009067
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009068 conf->ciphersuite_list[mbedtls_ssl_minor_ver_index( minor )] =
9069 ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00009070}
Hanno Becker73f4cb12019-06-27 13:51:07 +01009071#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009073#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009074void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01009075 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009076{
9077 conf->cert_profile = profile;
9078}
9079
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009080/* Append a new keycert entry to a (possibly empty) list */
9081static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
9082 mbedtls_x509_crt *cert,
9083 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009084{
niisato8ee24222018-06-25 19:05:48 +09009085 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009086
niisato8ee24222018-06-25 19:05:48 +09009087 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
9088 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009089 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009090
niisato8ee24222018-06-25 19:05:48 +09009091 new_cert->cert = cert;
9092 new_cert->key = key;
9093 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009094
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009095 /* Update head is the list was null, else add to the end */
9096 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01009097 {
niisato8ee24222018-06-25 19:05:48 +09009098 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01009099 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009100 else
9101 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009102 mbedtls_ssl_key_cert *cur = *head;
9103 while( cur->next != NULL )
9104 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09009105 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009106 }
9107
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009108 return( 0 );
9109}
9110
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009111int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009112 mbedtls_x509_crt *own_cert,
9113 mbedtls_pk_context *pk_key )
9114{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02009115 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009116}
9117
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009118void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009119 mbedtls_x509_crt *ca_chain,
9120 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009121{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009122 conf->ca_chain = ca_chain;
9123 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00009124}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009125#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00009126
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009127#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9128int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
9129 mbedtls_x509_crt *own_cert,
9130 mbedtls_pk_context *pk_key )
9131{
9132 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
9133 own_cert, pk_key ) );
9134}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02009135
9136void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
9137 mbedtls_x509_crt *ca_chain,
9138 mbedtls_x509_crl *ca_crl )
9139{
9140 ssl->handshake->sni_ca_chain = ca_chain;
9141 ssl->handshake->sni_ca_crl = ca_crl;
9142}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02009143
9144void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
9145 int authmode )
9146{
9147 ssl->handshake->sni_authmode = authmode;
9148}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009149#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9150
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009151#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009152/*
9153 * Set EC J-PAKE password for current handshake
9154 */
9155int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
9156 const unsigned char *pw,
9157 size_t pw_len )
9158{
9159 mbedtls_ecjpake_role role;
9160
Janos Follath8eb64132016-06-03 15:40:57 +01009161 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009162 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9163
Hanno Becker2d9623f2019-06-13 12:07:05 +01009164 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009165 role = MBEDTLS_ECJPAKE_SERVER;
9166 else
9167 role = MBEDTLS_ECJPAKE_CLIENT;
9168
9169 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
9170 role,
9171 MBEDTLS_MD_SHA256,
9172 MBEDTLS_ECP_DP_SECP256R1,
9173 pw, pw_len ) );
9174}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009175#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009177#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009178int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009179 const unsigned char *psk, size_t psk_len,
9180 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009181{
Paul Bakker6db455e2013-09-18 17:29:31 +02009182 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009183 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02009184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009185 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9186 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01009187
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009188 /* Identity len will be encoded on two bytes */
9189 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009190 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009191 {
9192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9193 }
9194
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009195 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02009196 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009197 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009198
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009199 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009200 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009201 conf->psk_len = 0;
9202 }
9203 if( conf->psk_identity != NULL )
9204 {
9205 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009206 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009207 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02009208 }
9209
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009210 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
9211 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05009212 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009213 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009214 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009215 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009216 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009217 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05009218 }
Paul Bakker6db455e2013-09-18 17:29:31 +02009219
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009220 conf->psk_len = psk_len;
9221 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02009222
Teppo Järvelin91d79382019-10-02 09:09:31 +03009223 mbedtls_platform_memcpy( conf->psk, psk, conf->psk_len );
9224 mbedtls_platform_memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02009225
9226 return( 0 );
9227}
9228
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009229int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9230 const unsigned char *psk, size_t psk_len )
9231{
9232 if( psk == NULL || ssl->handshake == NULL )
9233 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9234
9235 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9236 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9237
9238 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009239 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009240 mbedtls_platform_zeroize( ssl->handshake->psk,
9241 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01009242 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009243 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009244 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009245
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009246 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009247 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009248
9249 ssl->handshake->psk_len = psk_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03009250 mbedtls_platform_memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009251
9252 return( 0 );
9253}
9254
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009255void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009256 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009257 size_t),
9258 void *p_psk )
9259{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009260 conf->f_psk = f_psk;
9261 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009262}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009263#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009264
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009265#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009266
9267#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009268int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009269{
9270 int ret;
9271
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009272 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9273 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9274 {
9275 mbedtls_mpi_free( &conf->dhm_P );
9276 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009277 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009278 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009279
9280 return( 0 );
9281}
Hanno Becker470a8c42017-10-04 15:28:46 +01009282#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009283
Hanno Beckera90658f2017-10-04 15:29:08 +01009284int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9285 const unsigned char *dhm_P, size_t P_len,
9286 const unsigned char *dhm_G, size_t G_len )
9287{
9288 int ret;
9289
9290 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9291 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9292 {
9293 mbedtls_mpi_free( &conf->dhm_P );
9294 mbedtls_mpi_free( &conf->dhm_G );
9295 return( ret );
9296 }
9297
9298 return( 0 );
9299}
Paul Bakker5121ce52009-01-03 21:22:43 +00009300
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009301int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009302{
9303 int ret;
9304
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009305 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9306 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9307 {
9308 mbedtls_mpi_free( &conf->dhm_P );
9309 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009310 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009311 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009312
9313 return( 0 );
9314}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009315#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009316
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009317#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9318/*
9319 * Set the minimum length for Diffie-Hellman parameters
9320 */
9321void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9322 unsigned int bitlen )
9323{
9324 conf->dhm_min_bitlen = bitlen;
9325}
9326#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9327
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009328#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009329/*
9330 * Set allowed/preferred hashes for handshake signatures
9331 */
9332void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9333 const int *hashes )
9334{
Hanno Becker56595f42019-06-19 16:31:38 +01009335#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009336 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01009337#else
9338 ((void) conf);
9339 ((void) hashes);
9340#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009341}
Hanno Becker947194e2017-04-07 13:25:49 +01009342#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009343
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009344#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01009345#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009346/*
9347 * Set the allowed elliptic curves
9348 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009349void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009350 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009351{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009352 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009353}
Hanno Beckerc1096e72019-06-19 12:30:41 +01009354#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01009355#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009356
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009357#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009358int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009359{
Hanno Becker947194e2017-04-07 13:25:49 +01009360 /* Initialize to suppress unnecessary compiler warning */
9361 size_t hostname_len = 0;
9362
9363 /* Check if new hostname is valid before
9364 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009365 if( hostname != NULL )
9366 {
9367 hostname_len = strlen( hostname );
9368
9369 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9370 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9371 }
9372
9373 /* Now it's clear that we will overwrite the old hostname,
9374 * so we can free it safely */
9375
9376 if( ssl->hostname != NULL )
9377 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009378 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009379 mbedtls_free( ssl->hostname );
9380 }
9381
9382 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009383
Paul Bakker5121ce52009-01-03 21:22:43 +00009384 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009385 {
9386 ssl->hostname = NULL;
9387 }
9388 else
9389 {
9390 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009391 if( ssl->hostname == NULL )
9392 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009393
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03009394 /* Not using more secure mbedtls_platform_memcpy as hostname is public in initial handshake */
9395 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009396
Hanno Becker947194e2017-04-07 13:25:49 +01009397 ssl->hostname[hostname_len] = '\0';
9398 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009399
9400 return( 0 );
9401}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009402#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009403
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009404#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009405void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009406 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009407 const unsigned char *, size_t),
9408 void *p_sni )
9409{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009410 conf->f_sni = f_sni;
9411 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009412}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009413#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009415#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009416int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009417{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009418 size_t cur_len, tot_len;
9419 const char **p;
9420
9421 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009422 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9423 * MUST NOT be truncated."
9424 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009425 */
9426 tot_len = 0;
9427 for( p = protos; *p != NULL; p++ )
9428 {
9429 cur_len = strlen( *p );
9430 tot_len += cur_len;
9431
9432 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009433 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009434 }
9435
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009436 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009437
9438 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009439}
9440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009441const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009442{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009443 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009444}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009445#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009446
Hanno Becker33b9b252019-07-05 11:23:25 +01009447#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9448 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9449void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9450 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009451{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009452 conf->max_major_ver = major;
9453 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009454}
Hanno Becker33b9b252019-07-05 11:23:25 +01009455#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9456 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009457
Hanno Becker33b9b252019-07-05 11:23:25 +01009458#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9459 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9460void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9461 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009462{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009463 conf->min_major_ver = major;
9464 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009465}
Hanno Becker33b9b252019-07-05 11:23:25 +01009466#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9467 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009469#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009470void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009471{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009472 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009473}
9474#endif
9475
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009476#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009477void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9478 char cert_req_ca_list )
9479{
9480 conf->cert_req_ca_list = cert_req_ca_list;
9481}
9482#endif
9483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009484#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009485void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009486{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009487 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009488}
9489#endif
9490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009491#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009492#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009493void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009494{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009495 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009496}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009497#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9498#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009499void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009500 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009501{
9502 conf->enforce_extended_master_secret = ems_enf;
9503}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009504#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009505#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009506
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009507#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009508void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009509{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009510 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009511}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009512#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009514#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009515int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009516{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009518 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009521 }
9522
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009523 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009524
9525 return( 0 );
9526}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009527#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009529#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009530void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009531{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009532 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009533}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009536#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009537void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009538{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009539 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009540}
9541#endif
9542
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009543#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009544void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009545{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009546 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009547}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009548#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009550#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009551void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009552{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009553 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009554}
9555
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009556void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009557{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009558 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009559}
9560
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009561void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009562 const unsigned char period[8] )
9563{
Teppo Järvelin91d79382019-10-02 09:09:31 +03009564 mbedtls_platform_memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009565}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009568#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009569#if defined(MBEDTLS_SSL_CLI_C)
9570void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009571{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009572 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009573}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009574#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009575
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009576#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009577void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9578 mbedtls_ssl_ticket_write_t *f_ticket_write,
9579 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9580 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009581{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009582 conf->f_ticket_write = f_ticket_write;
9583 conf->f_ticket_parse = f_ticket_parse;
9584 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009585}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009586#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009587#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009588
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009589#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9590void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9591 mbedtls_ssl_export_keys_t *f_export_keys,
9592 void *p_export_keys )
9593{
9594 conf->f_export_keys = f_export_keys;
9595 conf->p_export_keys = p_export_keys;
9596}
9597#endif
9598
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009599#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009600void mbedtls_ssl_conf_async_private_cb(
9601 mbedtls_ssl_config *conf,
9602 mbedtls_ssl_async_sign_t *f_async_sign,
9603 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9604 mbedtls_ssl_async_resume_t *f_async_resume,
9605 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009606 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009607{
9608 conf->f_async_sign_start = f_async_sign;
9609 conf->f_async_decrypt_start = f_async_decrypt;
9610 conf->f_async_resume = f_async_resume;
9611 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009612 conf->p_async_config_data = async_config_data;
9613}
9614
Gilles Peskine8f97af72018-04-26 11:46:10 +02009615void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9616{
9617 return( conf->p_async_config_data );
9618}
9619
Gilles Peskine1febfef2018-04-30 11:54:39 +02009620void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009621{
9622 if( ssl->handshake == NULL )
9623 return( NULL );
9624 else
9625 return( ssl->handshake->user_async_ctx );
9626}
9627
Gilles Peskine1febfef2018-04-30 11:54:39 +02009628void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009629 void *ctx )
9630{
9631 if( ssl->handshake != NULL )
9632 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009633}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009634#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009635
Paul Bakker5121ce52009-01-03 21:22:43 +00009636/*
9637 * SSL get accessors
9638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009639size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009640{
9641 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9642}
9643
Hanno Becker8b170a02017-10-10 11:51:19 +01009644int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9645{
9646 /*
9647 * Case A: We're currently holding back
9648 * a message for further processing.
9649 */
9650
9651 if( ssl->keep_current_message == 1 )
9652 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009653 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009654 return( 1 );
9655 }
9656
9657 /*
9658 * Case B: Further records are pending in the current datagram.
9659 */
9660
9661#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009662 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009663 ssl->in_left > ssl->next_record_offset )
9664 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009665 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009666 return( 1 );
9667 }
9668#endif /* MBEDTLS_SSL_PROTO_DTLS */
9669
9670 /*
9671 * Case C: A handshake message is being processed.
9672 */
9673
Hanno Becker8b170a02017-10-10 11:51:19 +01009674 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9675 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009676 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009677 return( 1 );
9678 }
9679
9680 /*
9681 * Case D: An application data message is being processed
9682 */
9683 if( ssl->in_offt != NULL )
9684 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009685 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009686 return( 1 );
9687 }
9688
9689 /*
9690 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009691 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009692 * we implement support for multiple alerts in single records.
9693 */
9694
9695 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9696 return( 0 );
9697}
9698
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009699uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009700{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009701 if( ssl->session != NULL )
9702 return( ssl->session->verify_result );
9703
9704 if( ssl->session_negotiate != NULL )
9705 return( ssl->session_negotiate->verify_result );
9706
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009707 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009708}
9709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009710const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009711{
Hanno Beckere02758c2019-06-26 15:31:31 +01009712 int suite;
9713
Paul Bakker926c8e42013-03-06 10:23:34 +01009714 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009715 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009716
Hanno Beckere02758c2019-06-26 15:31:31 +01009717 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9718 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009719}
9720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009721const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009722{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009723#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009724 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009725 {
Hanno Becker2881d802019-05-22 14:44:53 +01009726 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009728 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009729 return( "DTLSv1.0" );
9730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009731 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009732 return( "DTLSv1.2" );
9733
9734 default:
9735 return( "unknown (DTLS)" );
9736 }
9737 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009738 MBEDTLS_SSL_TRANSPORT_ELSE
9739#endif /* MBEDTLS_SSL_PROTO_DTLS */
9740#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009741 {
Hanno Becker2881d802019-05-22 14:44:53 +01009742 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009743 {
9744 case MBEDTLS_SSL_MINOR_VERSION_0:
9745 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009746
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009747 case MBEDTLS_SSL_MINOR_VERSION_1:
9748 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009749
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009750 case MBEDTLS_SSL_MINOR_VERSION_2:
9751 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009752
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009753 case MBEDTLS_SSL_MINOR_VERSION_3:
9754 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009755
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009756 default:
9757 return( "unknown" );
9758 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009759 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009760#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009761}
9762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009763int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009764{
Hanno Becker3136ede2018-08-17 15:28:19 +01009765 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009766 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009767
Hanno Becker43395762019-05-03 14:46:38 +01009768 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9769
Hanno Becker78640902018-08-13 16:35:15 +01009770 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009771 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773#if defined(MBEDTLS_ZLIB_SUPPORT)
9774 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9775 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009776#endif
9777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009778 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009779 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009780#if defined(MBEDTLS_GCM_C) || \
9781 defined(MBEDTLS_CCM_C) || \
9782 defined(MBEDTLS_CHACHAPOLY_C)
9783#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009784 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009785#endif
9786#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009787 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009788#endif
9789#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009790 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009791#endif
9792 transform_expansion =
9793 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009794 break;
9795
Hanno Beckera9d5c452019-07-25 16:47:12 +01009796#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9797 MBEDTLS_CHACHAPOLY_C */
9798
9799#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9800 case MBEDTLS_MODE_STREAM:
9801 transform_expansion = transform->maclen;
9802 break;
9803#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9804
9805#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009806 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009807 {
9808 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009809
9810 block_size = mbedtls_cipher_get_block_size(
9811 &transform->cipher_ctx_enc );
9812
Hanno Becker3136ede2018-08-17 15:28:19 +01009813 /* Expansion due to the addition of the MAC. */
9814 transform_expansion += transform->maclen;
9815
9816 /* Expansion due to the addition of CBC padding;
9817 * Theoretically up to 256 bytes, but we never use
9818 * more than the block size of the underlying cipher. */
9819 transform_expansion += block_size;
9820
9821 /* For TLS 1.1 or higher, an explicit IV is added
9822 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009823#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009824 if( mbedtls_ssl_ver_geq(
9825 mbedtls_ssl_get_minor_ver( ssl ),
9826 MBEDTLS_SSL_MINOR_VERSION_2 ) )
9827 {
Hanno Becker3136ede2018-08-17 15:28:19 +01009828 transform_expansion += block_size;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009829 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01009830#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009831
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009832 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009833 }
9834#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009835
9836 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009838 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009839 }
9840
Hanno Beckera5a2b082019-05-15 14:03:01 +01009841#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009842 if( transform->out_cid_len != 0 )
9843 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009844#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009845
Hanno Becker43395762019-05-03 14:46:38 +01009846 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009847}
9848
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009849#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9850size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9851{
9852 size_t max_len;
9853
9854 /*
9855 * Assume mfl_code is correct since it was checked when set
9856 */
Angus Grattond8213d02016-05-25 20:56:48 +10009857 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009858
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009859 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009860 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009861 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009862 {
Angus Grattond8213d02016-05-25 20:56:48 +10009863 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009864 }
9865
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009866 /* During a handshake, use the value being negotiated */
9867 if( ssl->session_negotiate != NULL &&
9868 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9869 {
9870 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9871 }
9872
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009873 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009874}
9875#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9876
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009877#if defined(MBEDTLS_SSL_PROTO_DTLS)
9878static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9879{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009880 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009881 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009882 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9883 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009884 return( 0 );
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009885
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009886 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9887 return( ssl->mtu );
9888
9889 if( ssl->mtu == 0 )
9890 return( ssl->handshake->mtu );
9891
9892 return( ssl->mtu < ssl->handshake->mtu ?
9893 ssl->mtu : ssl->handshake->mtu );
9894}
9895#endif /* MBEDTLS_SSL_PROTO_DTLS */
9896
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009897int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9898{
9899 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9900
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009901#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9902 !defined(MBEDTLS_SSL_PROTO_DTLS)
9903 (void) ssl;
9904#endif
9905
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009906#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9907 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9908
9909 if( max_len > mfl )
9910 max_len = mfl;
9911#endif
9912
9913#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009914 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009915 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009916 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009917 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9918 const size_t overhead = (size_t) ret;
9919
9920 if( ret < 0 )
9921 return( ret );
9922
9923 if( mtu <= overhead )
9924 {
9925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9926 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9927 }
9928
9929 if( max_len > mtu - overhead )
9930 max_len = mtu - overhead;
9931 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009932#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009933
Hanno Becker0defedb2018-08-10 12:35:02 +01009934#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9935 !defined(MBEDTLS_SSL_PROTO_DTLS)
9936 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009937#endif
9938
9939 return( (int) max_len );
9940}
9941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009942#if defined(MBEDTLS_X509_CRT_PARSE_C)
9943const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009944{
9945 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009946 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009947
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009948#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009949 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009950#else
9951 return( NULL );
9952#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009953}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009954#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009956#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009957int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9958 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009959{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009960 if( ssl == NULL ||
9961 dst == NULL ||
9962 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009963 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009965 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009966 }
9967
Hanno Becker58fccf22019-02-06 14:30:46 +00009968 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009969}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009970#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009971
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009972const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9973{
9974 if( ssl == NULL )
9975 return( NULL );
9976
9977 return( ssl->session );
9978}
9979
Paul Bakker5121ce52009-01-03 21:22:43 +00009980/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009981 * Define ticket header determining Mbed TLS version
9982 * and structure of the ticket.
9983 */
9984
Hanno Becker41527622019-05-16 12:50:45 +01009985/*
Hanno Becker26829e92019-05-28 14:30:45 +01009986 * Define bitflag determining compile-time settings influencing
9987 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009988 */
9989
Hanno Becker26829e92019-05-28 14:30:45 +01009990#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009991#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009992#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009993#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009994#endif /* MBEDTLS_HAVE_TIME */
9995
9996#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009997#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009998#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009999#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +010010000#endif /* MBEDTLS_X509_CRT_PARSE_C */
10001
10002#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010003#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +010010004#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010005#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +010010006#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
10007
10008#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010009#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +010010010#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010011#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +010010012#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
10013
10014#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010015#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +010010016#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010017#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +010010018#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
10019
10020#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010021#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +010010022#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010023#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +010010024#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
10025
Hanno Becker41527622019-05-16 12:50:45 +010010026#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10027#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
10028#else
10029#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
10030#endif /* MBEDTLS_SSL_SESSION_TICKETS */
10031
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010032#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10033#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
10034#else
10035#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
10036#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10037
Hanno Becker88440552019-07-03 14:16:13 +010010038#if defined(MBEDTLS_ZLIB_SUPPORT)
10039#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
10040#else
10041#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
10042#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10043
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010044#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
10045#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
10046#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
10047#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
10048#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
10049#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
10050#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010051#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +010010052#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010053
Hanno Becker26829e92019-05-28 14:30:45 +010010054#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010055 ( (uint16_t) ( \
10056 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
10057 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
10058 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
10059 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
10060 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
10061 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010062 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +010010063 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010064 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +010010065
Hanno Becker557fe9f2019-05-16 12:41:07 +010010066static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +010010067 MBEDTLS_VERSION_MAJOR,
10068 MBEDTLS_VERSION_MINOR,
10069 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +010010070 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
10071 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +010010072};
Hanno Beckerb5352f02019-05-16 12:39:07 +010010073
10074/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010075 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010076 * (in the presentation language of TLS, RFC 8446 section 3)
10077 *
Hanno Becker26829e92019-05-28 14:30:45 +010010078 * opaque mbedtls_version[3]; // major, minor, patch
10079 * opaque session_format[2]; // version-specific 16-bit field determining
10080 * // the format of the remaining
10081 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +010010082 *
10083 * Note: When updating the format, remember to keep
10084 * these version+format bytes.
10085 *
Hanno Becker7bf77102019-06-04 09:43:16 +010010086 * // In this version, `session_format` determines
10087 * // the setting of those compile-time
10088 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +010010089 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010090 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +010010091 * uint8 ciphersuite[2]; // defined by the standard
10092 * uint8 compression; // 0 or 1
10093 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010094 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +010010095 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010096 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +010010097 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
10098 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
10099 * case disabled: uint8_t peer_cert_digest_type;
10100 * opaque peer_cert_digest<0..2^8-1>;
10101 * }
Hanno Becker26829e92019-05-28 14:30:45 +010010102 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010103 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +010010104 * uint8 mfl_code; // up to 255 according to standard
10105 * uint8 trunc_hmac; // 0 or 1
10106 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010107 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010108 * The order is the same as in the definition of the structure, except
10109 * verify_result is put before peer_cert so that all mandatory fields come
10110 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010111 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010112static int ssl_session_save( const mbedtls_ssl_session *session,
10113 unsigned char omit_header,
10114 unsigned char *buf,
10115 size_t buf_len,
10116 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010117{
10118 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010119 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010120#if defined(MBEDTLS_HAVE_TIME)
10121 uint64_t start;
10122#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010123#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010124#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010125 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010126#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +000010127#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010128
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010129 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010130 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010131 /*
10132 * Add version identifier
10133 */
10134
10135 used += sizeof( ssl_serialized_session_header );
10136
10137 if( used <= buf_len )
10138 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010139 mbedtls_platform_memcpy( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010140 sizeof( ssl_serialized_session_header ) );
10141 p += sizeof( ssl_serialized_session_header );
10142 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010143 }
10144
10145 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010146 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010147 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010148#if defined(MBEDTLS_HAVE_TIME)
10149 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010150
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010151 if( used <= buf_len )
10152 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010153 start = (uint64_t) session->start;
10154
10155 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
10156 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
10157 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
10158 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
10159 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
10160 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
10161 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
10162 *p++ = (unsigned char)( ( start ) & 0xFF );
10163 }
10164#endif /* MBEDTLS_HAVE_TIME */
10165
10166 /*
10167 * Basic mandatory fields
10168 */
Hanno Becker88440552019-07-03 14:16:13 +010010169 {
10170 size_t const ciphersuite_len = 2;
10171#if defined(MBEDTLS_ZLIB_SUPPORT)
10172 size_t const compression_len = 1;
10173#else
10174 size_t const compression_len = 0;
10175#endif
10176 size_t const id_len_len = 1;
10177 size_t const id_len = 32;
10178 size_t const master_len = 48;
10179 size_t const verif_result_len = 4;
10180
10181 size_t const basic_len =
10182 ciphersuite_len +
10183 compression_len +
10184 id_len_len +
10185 id_len +
10186 master_len +
10187 verif_result_len;
10188
10189 used += basic_len;
10190 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010191
10192 if( used <= buf_len )
10193 {
Hanno Beckere02758c2019-06-26 15:31:31 +010010194 const int ciphersuite =
10195 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010196 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010197
Hanno Becker88440552019-07-03 14:16:13 +010010198#if defined(MBEDTLS_ZLIB_SUPPORT)
10199 *p++ = (unsigned char)(
10200 mbedtls_ssl_session_get_compression( session ) );
10201#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010202
10203 *p++ = (unsigned char)( session->id_len & 0xFF );
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010204 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10205 memcpy( p, session->id, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010206 p += 32;
10207
Teppo Järvelin91d79382019-10-02 09:09:31 +030010208 mbedtls_platform_memcpy( p, session->master, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010209 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010210 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010211 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010212
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010213 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010214 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010215 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010216#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010217#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010218 if( session->peer_cert == NULL )
10219 cert_len = 0;
10220 else
10221 cert_len = session->peer_cert->raw.len;
10222
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010223 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010224
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010225 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010226 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010227 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010228
10229 if( session->peer_cert != NULL )
10230 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010231 mbedtls_platform_memcpy( p, session->peer_cert->raw.p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010232 p += cert_len;
10233 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010234 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010235
Hanno Becker5882dd02019-06-06 16:25:57 +010010236#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010237 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010238 if( session->peer_cert_digest != NULL )
10239 {
10240 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
10241 if( used <= buf_len )
10242 {
10243 *p++ = (unsigned char) session->peer_cert_digest_type;
10244 *p++ = (unsigned char) session->peer_cert_digest_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +030010245 mbedtls_platform_memcpy( p, session->peer_cert_digest,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010246 session->peer_cert_digest_len );
10247 p += session->peer_cert_digest_len;
10248 }
10249 }
10250 else
10251 {
10252 used += 2;
10253 if( used <= buf_len )
10254 {
10255 *p++ = (unsigned char) MBEDTLS_MD_NONE;
10256 *p++ = 0;
10257 }
10258 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010259#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010260#endif /* MBEDTLS_X509_CRT_PARSE_C */
10261
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010262 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010263 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010264 */
10265#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010266 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010267
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010268 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010269 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010270 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010271
10272 if( session->ticket != NULL )
10273 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010274 mbedtls_platform_memcpy( p, session->ticket, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010275 p += session->ticket_len;
10276 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010277
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010278 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010279 }
10280#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10281
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010282 /*
10283 * Misc extension-related info
10284 */
10285#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10286 used += 1;
10287
10288 if( used <= buf_len )
10289 *p++ = session->mfl_code;
10290#endif
10291
10292#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10293 used += 1;
10294
10295 if( used <= buf_len )
10296 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
10297#endif
10298
10299#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10300 used += 1;
10301
10302 if( used <= buf_len )
10303 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
10304#endif
10305
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010306 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010307 *olen = used;
10308
10309 if( used > buf_len )
10310 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010311
10312 return( 0 );
10313}
10314
10315/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010316 * Public wrapper for ssl_session_save()
10317 */
10318int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
10319 unsigned char *buf,
10320 size_t buf_len,
10321 size_t *olen )
10322{
10323 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
10324}
10325
10326/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010327 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010328 *
10329 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010330 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010331 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010332static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010333 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010334 const unsigned char *buf,
10335 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010336{
10337 const unsigned char *p = buf;
10338 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +010010339 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010340#if defined(MBEDTLS_HAVE_TIME)
10341 uint64_t start;
10342#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010343#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010344#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010345 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +000010346#endif
10347#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010348
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010349 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010350 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010351 /*
10352 * Check version identifier
10353 */
10354
10355 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
10356 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10357
Teppo Järvelin0efac532019-10-04 13:21:08 +030010358 // use regular memcmp as session header is public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010359 if( memcmp( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010360 sizeof( ssl_serialized_session_header ) ) != 0 )
10361 {
10362 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10363 }
10364 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +010010365 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010366
10367 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010368 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010369 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010370#if defined(MBEDTLS_HAVE_TIME)
10371 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010372 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10373
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010374 start = ( (uint64_t) p[0] << 56 ) |
10375 ( (uint64_t) p[1] << 48 ) |
10376 ( (uint64_t) p[2] << 40 ) |
10377 ( (uint64_t) p[3] << 32 ) |
10378 ( (uint64_t) p[4] << 24 ) |
10379 ( (uint64_t) p[5] << 16 ) |
10380 ( (uint64_t) p[6] << 8 ) |
10381 ( (uint64_t) p[7] );
10382 p += 8;
10383
10384 session->start = (time_t) start;
10385#endif /* MBEDTLS_HAVE_TIME */
10386
10387 /*
10388 * Basic mandatory fields
10389 */
Hanno Becker88440552019-07-03 14:16:13 +010010390 {
10391 size_t const ciphersuite_len = 2;
10392#if defined(MBEDTLS_ZLIB_SUPPORT)
10393 size_t const compression_len = 1;
10394#else
10395 size_t const compression_len = 0;
10396#endif
10397 size_t const id_len_len = 1;
10398 size_t const id_len = 32;
10399 size_t const master_len = 48;
10400 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010401
Hanno Becker88440552019-07-03 14:16:13 +010010402 size_t const basic_len =
10403 ciphersuite_len +
10404 compression_len +
10405 id_len_len +
10406 id_len +
10407 master_len +
10408 verif_result_len;
10409
10410 if( basic_len > (size_t)( end - p ) )
10411 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10412 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010413
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010414 ciphersuite = (int)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010415 p += 2;
10416
Hanno Becker73f4cb12019-06-27 13:51:07 +010010417#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010418 session->ciphersuite = ciphersuite;
10419#else
10420 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010421 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010422 {
10423 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10424 }
10425#endif
10426
Hanno Becker88440552019-07-03 14:16:13 +010010427#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010428 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010429#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010430
10431 session->id_len = *p++;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010432 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10433 memcpy( session->id, p, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010434 p += 32;
10435
Teppo Järvelin91d79382019-10-02 09:09:31 +030010436 mbedtls_platform_memcpy( session->master, p, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010437 p += 48;
10438
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010439 session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010440 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010441
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010442 /* Immediately clear invalid pointer values that have been read, in case
10443 * we exit early before we replaced them with valid ones. */
10444#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010445#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010446 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010447#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010448 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010449#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010450#endif
10451#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10452 session->ticket = NULL;
10453#endif
10454
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010455 /*
10456 * Peer certificate
10457 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010458#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010459#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010460 if( 3 > (size_t)( end - p ) )
10461 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10462
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010463 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10464
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010465 p += 3;
10466
10467 if( cert_len == 0 )
10468 {
10469 session->peer_cert = NULL;
10470 }
10471 else
10472 {
10473 int ret;
10474
10475 if( cert_len > (size_t)( end - p ) )
10476 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10477
10478 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10479
10480 if( session->peer_cert == NULL )
10481 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10482
10483 mbedtls_x509_crt_init( session->peer_cert );
10484
10485 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10486 p, cert_len ) ) != 0 )
10487 {
10488 mbedtls_x509_crt_free( session->peer_cert );
10489 mbedtls_free( session->peer_cert );
10490 session->peer_cert = NULL;
10491 return( ret );
10492 }
10493
10494 p += cert_len;
10495 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010496#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010497 /* Deserialize CRT digest from the end of the ticket. */
10498 if( 2 > (size_t)( end - p ) )
10499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10500
10501 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10502 session->peer_cert_digest_len = (size_t) *p++;
10503
10504 if( session->peer_cert_digest_len != 0 )
10505 {
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010506 mbedtls_md_handle_t md_info =
Hanno Becker2326d202019-06-06 14:54:55 +010010507 mbedtls_md_info_from_type( session->peer_cert_digest_type );
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010508 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Hanno Becker2326d202019-06-06 14:54:55 +010010509 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10510 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10511 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10512
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010513 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10515
10516 session->peer_cert_digest =
10517 mbedtls_calloc( 1, session->peer_cert_digest_len );
10518 if( session->peer_cert_digest == NULL )
10519 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10520
Teppo Järvelin91d79382019-10-02 09:09:31 +030010521 mbedtls_platform_memcpy( session->peer_cert_digest, p,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010522 session->peer_cert_digest_len );
10523 p += session->peer_cert_digest_len;
10524 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010525#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010526#endif /* MBEDTLS_X509_CRT_PARSE_C */
10527
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010528 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010529 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010530 */
10531#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10532 if( 3 > (size_t)( end - p ) )
10533 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10534
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010535 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010536 p += 3;
10537
10538 if( session->ticket_len != 0 )
10539 {
10540 if( session->ticket_len > (size_t)( end - p ) )
10541 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10542
10543 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10544 if( session->ticket == NULL )
10545 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10546
Teppo Järvelin91d79382019-10-02 09:09:31 +030010547 mbedtls_platform_memcpy( session->ticket, p, session->ticket_len );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010548 p += session->ticket_len;
10549 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010550
10551 if( 4 > (size_t)( end - p ) )
10552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10553
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010554 session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010555 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010556#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10557
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010558 /*
10559 * Misc extension-related info
10560 */
10561#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10562 if( 1 > (size_t)( end - p ) )
10563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10564
10565 session->mfl_code = *p++;
10566#endif
10567
10568#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10569 if( 1 > (size_t)( end - p ) )
10570 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10571
10572 session->trunc_hmac = *p++;
10573#endif
10574
10575#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10576 if( 1 > (size_t)( end - p ) )
10577 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10578
10579 session->encrypt_then_mac = *p++;
10580#endif
10581
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010582 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010583 if( p != end )
10584 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10585
10586 return( 0 );
10587}
10588
10589/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010590 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010591 */
10592int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10593 const unsigned char *buf,
10594 size_t len )
10595{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010596 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010597
10598 if( ret != 0 )
10599 mbedtls_ssl_session_free( session );
10600
10601 return( ret );
10602}
10603
10604/*
Paul Bakker1961b702013-01-25 14:49:24 +010010605 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010607int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010608{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010609 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010610
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010611 if( ssl == NULL || ssl->conf == NULL )
10612 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010614#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010615 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010616 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010617#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010618#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010619 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010620 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010621#endif
10622
Hanno Beckerb82350b2019-07-26 07:24:05 +010010623 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010624 return( ret );
10625}
10626
10627/*
10628 * Perform the SSL handshake
10629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010630int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010631{
10632 int ret = 0;
10633
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010634 if( ssl == NULL || ssl->conf == NULL )
10635 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010639 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010641 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010642
10643 if( ret != 0 )
10644 break;
10645 }
10646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010648
10649 return( ret );
10650}
10651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010652#if defined(MBEDTLS_SSL_RENEGOTIATION)
10653#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010654/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010655 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010657static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010658{
10659 int ret;
10660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010662
10663 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010664 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10665 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010666
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010667 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010668 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010669 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010670 return( ret );
10671 }
10672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010674
10675 return( 0 );
10676}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010677#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010678
10679/*
10680 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010681 * - any side: calling mbedtls_ssl_renegotiate(),
10682 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10683 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010684 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010685 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010686 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010687 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010688static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010689{
10690 int ret;
10691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010693
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010694 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10695 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010696
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010697 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10698 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010699#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010700 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010701 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010702 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010703 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10704 MBEDTLS_SSL_IS_SERVER )
10705 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010706 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010707 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010708 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010709 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010710 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010711 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010712 }
10713#endif
10714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010715 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10716 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010718 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010720 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010721 return( ret );
10722 }
10723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010725
10726 return( 0 );
10727}
10728
10729/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010730 * Renegotiate current connection on client,
10731 * or request renegotiation on server
10732 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010733int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010734{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010735 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010736
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010737 if( ssl == NULL || ssl->conf == NULL )
10738 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010740#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010741 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010742 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010744 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10745 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010747 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010748
10749 /* Did we already try/start sending HelloRequest? */
10750 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010751 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010752
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010753 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010754 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010755#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010757#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010758 /*
10759 * On client, either start the renegotiation process or,
10760 * if already in progress, continue the handshake
10761 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010762 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010764 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10765 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010766
10767 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010769 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010770 return( ret );
10771 }
10772 }
10773 else
10774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010775 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010777 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010778 return( ret );
10779 }
10780 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010781#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010782
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010783 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010784}
10785
10786/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010787 * Check record counters and renegotiate if they're above the limit.
10788 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010789static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010790{
Andres AG2196c7f2016-12-15 17:01:16 +000010791 size_t ep_len = ssl_ep_len( ssl );
10792 int in_ctr_cmp;
10793 int out_ctr_cmp;
10794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010795 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10796 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010797 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010798 {
10799 return( 0 );
10800 }
10801
Teppo Järvelin0efac532019-10-04 13:21:08 +030010802 // use regular memcmp as counters are public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010803 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010804 ssl->conf->renego_period + ep_len, 8 - ep_len );
Teppo Järvelin650343c2019-10-03 15:36:59 +030010805 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010806 ssl->conf->renego_period + ep_len, 8 - ep_len );
10807
10808 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010809 {
10810 return( 0 );
10811 }
10812
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010814 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010815}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010816#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010817
10818/*
10819 * Receive application data decrypted from the SSL layer
10820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010821int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010822{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010823 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010824 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010825
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010826 if( ssl == NULL || ssl->conf == NULL )
10827 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010831#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010832 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010834 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010835 return( ret );
10836
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010837 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010838 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010839 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010840 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010841 return( ret );
10842 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010843 }
10844#endif
10845
Hanno Becker4a810fb2017-05-24 16:27:30 +010010846 /*
10847 * Check if renegotiation is necessary and/or handshake is
10848 * in process. If yes, perform/continue, and fall through
10849 * if an unexpected packet is received while the client
10850 * is waiting for the ServerHello.
10851 *
10852 * (There is no equivalent to the last condition on
10853 * the server-side as it is not treated as within
10854 * a handshake while waiting for the ClientHello
10855 * after a renegotiation request.)
10856 */
10857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010858#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010859 ret = ssl_check_ctr_renegotiate( ssl );
10860 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10861 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010863 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010864 return( ret );
10865 }
10866#endif
10867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010868 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010870 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010871 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10872 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010874 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010875 return( ret );
10876 }
10877 }
10878
Hanno Beckere41158b2017-10-23 13:30:32 +010010879 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010880 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010881 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010882 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010883 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10884 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010885 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010886 ssl_set_timer( ssl,
10887 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010888 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010889
Hanno Becker327c93b2018-08-15 13:56:18 +010010890 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010891 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010892 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10893 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010894
Hanno Becker4a810fb2017-05-24 16:27:30 +010010895 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10896 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010897 }
10898
10899 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010900 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010901 {
10902 /*
10903 * OpenSSL sends empty messages to randomize the IV
10904 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010905 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010907 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010908 return( 0 );
10909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010910 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010911 return( ret );
10912 }
10913 }
10914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010915 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010918
Hanno Becker4a810fb2017-05-24 16:27:30 +010010919 /*
10920 * - For client-side, expect SERVER_HELLO_REQUEST.
10921 * - For server-side, expect CLIENT_HELLO.
10922 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10923 */
10924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010925#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010926 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10927 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010928 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010929 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010932
10933 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010934#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010935 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010936 {
10937 continue;
10938 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010939 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010940#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010941#if defined(MBEDTLS_SSL_PROTO_TLS)
10942 {
10943 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10944 }
10945#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010946 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010947#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010948
Hanno Becker4a810fb2017-05-24 16:27:30 +010010949#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010950 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10951 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010952 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010955
10956 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010957#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010958 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010959 {
10960 continue;
10961 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010962 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010963#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010964#if defined(MBEDTLS_SSL_PROTO_TLS)
10965 {
10966 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10967 }
10968#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010969 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010970#endif /* MBEDTLS_SSL_SRV_C */
10971
Hanno Becker21df7f92017-10-17 11:03:26 +010010972#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010973 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010974 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10975 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010976 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010977 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10978 {
10979 /*
10980 * Accept renegotiation request
10981 */
Paul Bakker48916f92012-09-16 19:57:18 +000010982
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010983 /* DTLS clients need to know renego is server-initiated */
10984#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010985 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010010986 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10987 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010988 {
10989 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10990 }
10991#endif
10992 ret = ssl_start_renegotiation( ssl );
10993 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10994 ret != 0 )
10995 {
10996 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10997 return( ret );
10998 }
10999 }
11000 else
Hanno Becker21df7f92017-10-17 11:03:26 +010011001#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000011002 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010011003 /*
11004 * Refuse renegotiation
11005 */
11006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011007 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011009#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010011010 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000011011 {
Gilles Peskine92e44262017-05-10 17:27:49 +020011012 /* SSLv3 does not have a "no_renegotiation" warning, so
11013 we send a fatal alert and abort the connection. */
11014 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11015 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
11016 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011017 }
11018 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011019#endif /* MBEDTLS_SSL_PROTO_SSL3 */
11020#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11021 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011022 if( mbedtls_ssl_ver_geq(
11023 mbedtls_ssl_get_minor_ver( ssl ),
11024 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011025 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010011026 ret = mbedtls_ssl_send_alert_message( ssl,
11027 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11028 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
11029 if( ret != 0 )
11030 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000011031 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020011032 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011033#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
11034 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020011035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
11037 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020011038 }
Paul Bakker48916f92012-09-16 19:57:18 +000011039 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011040
Hanno Becker90333da2017-10-10 11:27:13 +010011041 /* At this point, we don't know whether the renegotiation has been
11042 * completed or not. The cases to consider are the following:
11043 * 1) The renegotiation is complete. In this case, no new record
11044 * has been read yet.
11045 * 2) The renegotiation is incomplete because the client received
11046 * an application data record while awaiting the ServerHello.
11047 * 3) The renegotiation is incomplete because the client received
11048 * a non-handshake, non-application data message while awaiting
11049 * the ServerHello.
11050 * In each of these case, looping will be the proper action:
11051 * - For 1), the next iteration will read a new record and check
11052 * if it's application data.
11053 * - For 2), the loop condition isn't satisfied as application data
11054 * is present, hence continue is the same as break
11055 * - For 3), the loop condition is satisfied and read_record
11056 * will re-deliver the message that was held back by the client
11057 * when expecting the ServerHello.
11058 */
11059 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000011060 }
Hanno Becker21df7f92017-10-17 11:03:26 +010011061#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011062 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011063 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011064 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011065 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011066 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011069 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011070 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011071 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011072 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011073 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011074#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011076 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
11077 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010011080 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011081 }
11082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011083 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000011084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
11086 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000011087 }
11088
11089 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020011090
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011091 /* We're going to return something now, cancel timer,
11092 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011093 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011094 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011095
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020011096#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011097 /* If we requested renego but received AppData, resend HelloRequest.
11098 * Do it now, after setting in_offt, to avoid taking this branch
11099 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011100#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010011101 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11102 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011103 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011104 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011105 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011107 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011108 return( ret );
11109 }
11110 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011111#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010011112#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000011113 }
11114
11115 n = ( len < ssl->in_msglen )
11116 ? len : ssl->in_msglen;
11117
Teppo Järvelin91d79382019-10-02 09:09:31 +030011118 mbedtls_platform_memcpy( buf, ssl->in_offt, n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011119 ssl->in_msglen -= n;
11120
11121 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010011122 {
11123 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000011124 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010011125 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011126 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011127 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010011128 {
Paul Bakker5121ce52009-01-03 21:22:43 +000011129 /* more data available */
11130 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011131 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011134
Paul Bakker23986e52011-04-24 08:57:21 +000011135 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011136}
11137
11138/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011139 * Send application data to be encrypted by the SSL layer, taking care of max
11140 * fragment length and buffer size.
11141 *
11142 * According to RFC 5246 Section 6.2.1:
11143 *
11144 * Zero-length fragments of Application data MAY be sent as they are
11145 * potentially useful as a traffic analysis countermeasure.
11146 *
11147 * Therefore, it is possible that the input message length is 0 and the
11148 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000011149 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011150static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011151 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000011152{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020011153 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
11154 const size_t max_len = (size_t) ret;
11155
11156 if( ret < 0 )
11157 {
11158 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
11159 return( ret );
11160 }
11161
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011162 if( len > max_len )
11163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011164#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011165 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011168 "maximum fragment length: %d > %d",
11169 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011170 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011171 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011172 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011173#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011174#if defined(MBEDTLS_SSL_PROTO_TLS)
11175 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011176 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011177 }
11178#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011179 }
Paul Bakker887bd502011-06-08 13:10:54 +000011180
Paul Bakker5121ce52009-01-03 21:22:43 +000011181 if( ssl->out_left != 0 )
11182 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011183 /*
11184 * The user has previously tried to send the data and
11185 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
11186 * written. In this case, we expect the high-level write function
11187 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
11188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011189 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011191 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011192 return( ret );
11193 }
11194 }
Paul Bakker887bd502011-06-08 13:10:54 +000011195 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000011196 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011197 /*
11198 * The user is trying to send a message the first time, so we need to
11199 * copy the data into the internal buffers and setup the data structure
11200 * to keep track of partial writes
11201 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011202 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011203 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Teppo Järvelin91d79382019-10-02 09:09:31 +030011204 mbedtls_platform_memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000011205
Hanno Becker67bc7c32018-08-06 11:33:50 +010011206 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000011207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011208 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000011209 return( ret );
11210 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011211 }
11212
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011213 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000011214}
11215
11216/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011217 * Write application data, doing 1/n-1 splitting if necessary.
11218 *
11219 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011220 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010011221 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011223#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011224static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011225 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011226{
11227 int ret;
11228
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011229 if( ssl->conf->cbc_record_splitting ==
11230 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011231 len <= 1 ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011232 mbedtls_ssl_ver_gt(
11233 mbedtls_ssl_get_minor_ver( ssl ),
11234 MBEDTLS_SSL_MINOR_VERSION_1 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011235 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
11236 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011237 {
11238 return( ssl_write_real( ssl, buf, len ) );
11239 }
11240
11241 if( ssl->split_done == 0 )
11242 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011243 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011244 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011245 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011246 }
11247
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011248 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
11249 return( ret );
11250 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011251
11252 return( ret + 1 );
11253}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011254#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011255
11256/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011257 * Write application data (public-facing wrapper)
11258 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011259int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011260{
11261 int ret;
11262
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011264
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011265 if( ssl == NULL || ssl->conf == NULL )
11266 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11267
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011268#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011269 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
11270 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011271 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011272 return( ret );
11273 }
11274#endif
11275
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011276 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011277 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011278 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011279 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020011280 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011281 return( ret );
11282 }
11283 }
11284
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011285#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011286 ret = ssl_write_split( ssl, buf, len );
11287#else
11288 ret = ssl_write_real( ssl, buf, len );
11289#endif
11290
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011292
11293 return( ret );
11294}
11295
11296/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011297 * Notify the peer that the connection is being closed
11298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011299int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011300{
11301 int ret;
11302
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011303 if( ssl == NULL || ssl->conf == NULL )
11304 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011307
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011308 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011309 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011311 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000011312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011313 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
11314 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11315 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011318 return( ret );
11319 }
11320 }
11321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011323
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011324 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000011325}
11326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011327void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000011328{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011329 if( transform == NULL )
11330 return;
11331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011332#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000011333 deflateEnd( &transform->ctx_deflate );
11334 inflateEnd( &transform->ctx_inflate );
11335#endif
11336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011337 mbedtls_cipher_free( &transform->cipher_ctx_enc );
11338 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020011339
Hanno Becker92231322018-01-03 15:32:51 +000011340#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011341 mbedtls_md_free( &transform->md_ctx_enc );
11342 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000011343#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011344
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011345 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011346}
11347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011348#if defined(MBEDTLS_X509_CRT_PARSE_C)
11349static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011350{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011351 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011352
11353 while( cur != NULL )
11354 {
11355 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011356 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011357 cur = next;
11358 }
11359}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011360#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011361
Hanno Becker0271f962018-08-16 13:23:47 +010011362#if defined(MBEDTLS_SSL_PROTO_DTLS)
11363
11364static void ssl_buffering_free( mbedtls_ssl_context *ssl )
11365{
11366 unsigned offset;
11367 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11368
11369 if( hs == NULL )
11370 return;
11371
Hanno Becker283f5ef2018-08-24 09:34:47 +010011372 ssl_free_buffered_record( ssl );
11373
Hanno Becker0271f962018-08-16 13:23:47 +010011374 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010011375 ssl_buffering_free_slot( ssl, offset );
11376}
11377
11378static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
11379 uint8_t slot )
11380{
11381 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11382 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010011383
11384 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
11385 return;
11386
Hanno Beckere605b192018-08-21 15:59:07 +010011387 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010011388 {
Hanno Beckere605b192018-08-21 15:59:07 +010011389 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010011390 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010011391 mbedtls_free( hs_buf->data );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020011392 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010011393 }
11394}
11395
11396#endif /* MBEDTLS_SSL_PROTO_DTLS */
11397
Gilles Peskine9b562d52018-04-25 20:32:43 +020011398void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011399{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011400 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11401
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011402 if( handshake == NULL )
11403 return;
11404
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011405#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11406 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11407 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011408 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011409 handshake->async_in_progress = 0;
11410 }
11411#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11412
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011413#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11414 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11415 mbedtls_md5_free( &handshake->fin_md5 );
11416 mbedtls_sha1_free( &handshake->fin_sha1 );
11417#endif
11418#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11419#if defined(MBEDTLS_SHA256_C)
11420 mbedtls_sha256_free( &handshake->fin_sha256 );
11421#endif
11422#if defined(MBEDTLS_SHA512_C)
11423 mbedtls_sha512_free( &handshake->fin_sha512 );
11424#endif
11425#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011427#if defined(MBEDTLS_DHM_C)
11428 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011429#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011430#if defined(MBEDTLS_ECDH_C)
11431 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011432#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011433#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011434 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011435#if defined(MBEDTLS_SSL_CLI_C)
11436 mbedtls_free( handshake->ecjpake_cache );
11437 handshake->ecjpake_cache = NULL;
11438 handshake->ecjpake_cache_len = 0;
11439#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011440#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011441
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011442#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11443 if( handshake->psk != NULL )
11444 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011445 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011446 mbedtls_free( handshake->psk );
11447 }
11448#endif
11449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011450#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11451 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011452 /*
11453 * Free only the linked list wrapper, not the keys themselves
11454 * since the belong to the SNI callback
11455 */
11456 if( handshake->sni_key_cert != NULL )
11457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011458 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011459
11460 while( cur != NULL )
11461 {
11462 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011463 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011464 cur = next;
11465 }
11466 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011467#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011468
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011469#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011470 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011471 if( handshake->ecrs_peer_cert != NULL )
11472 {
11473 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11474 mbedtls_free( handshake->ecrs_peer_cert );
11475 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011476#endif
11477
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011478#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11479 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11480 mbedtls_pk_free( &handshake->peer_pubkey );
11481#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011483#if defined(MBEDTLS_SSL_PROTO_DTLS)
11484 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011485 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011486 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011487#endif
11488
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011489 mbedtls_platform_zeroize( handshake,
11490 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011491}
11492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011493void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011494{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011495 if( session == NULL )
11496 return;
11497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011498#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011499 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011500#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011501
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011502#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011503 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011504#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011505
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011506 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011507}
11508
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011509#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011510
11511#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11512#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11513#else
11514#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11515#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11516
11517#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11518#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11519#else
11520#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11521#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11522
11523#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11524#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11525#else
11526#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11527#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11528
11529#if defined(MBEDTLS_SSL_ALPN)
11530#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11531#else
11532#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11533#endif /* MBEDTLS_SSL_ALPN */
11534
11535#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11536#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11537#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11538#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11539
11540#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11541 ( (uint32_t) ( \
11542 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11543 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11544 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11545 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11546 0u ) )
11547
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011548static unsigned char ssl_serialized_context_header[] = {
11549 MBEDTLS_VERSION_MAJOR,
11550 MBEDTLS_VERSION_MINOR,
11551 MBEDTLS_VERSION_PATCH,
11552 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11553 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011554 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11555 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11556 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011557};
11558
Paul Bakker5121ce52009-01-03 21:22:43 +000011559/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011560 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011561 *
11562 * The format of the serialized data is:
11563 * (in the presentation language of TLS, RFC 8446 section 3)
11564 *
11565 * // header
11566 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011567 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011568 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011569 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011570 * Note: When updating the format, remember to keep these
11571 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011572 *
11573 * // session sub-structure
11574 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11575 * // transform sub-structure
11576 * uint8 random[64]; // ServerHello.random+ClientHello.random
11577 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11578 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11579 * // fields from ssl_context
11580 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11581 * uint64 in_window_top; // DTLS: last validated record seq_num
11582 * uint64 in_window; // DTLS: bitmask for replay protection
11583 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11584 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11585 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11586 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11587 *
11588 * Note that many fields of the ssl_context or sub-structures are not
11589 * serialized, as they fall in one of the following categories:
11590 *
11591 * 1. forced value (eg in_left must be 0)
11592 * 2. pointer to dynamically-allocated memory (eg session, transform)
11593 * 3. value can be re-derived from other data (eg session keys from MS)
11594 * 4. value was temporary (eg content of input buffer)
11595 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011596 */
11597int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11598 unsigned char *buf,
11599 size_t buf_len,
11600 size_t *olen )
11601{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011602 unsigned char *p = buf;
11603 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011604 size_t session_len;
11605 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011606
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011607 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011608 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11609 * this function's documentation.
11610 *
11611 * These are due to assumptions/limitations in the implementation. Some of
11612 * them are likely to stay (no handshake in progress) some might go away
11613 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011614 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011615 /* The initial handshake must be over */
11616 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011617 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011618 if( ssl->handshake != NULL )
11619 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11620 /* Double-check that sub-structures are indeed ready */
11621 if( ssl->transform == NULL || ssl->session == NULL )
11622 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11623 /* There must be no pending incoming or outgoing data */
11624 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11625 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11626 if( ssl->out_left != 0 )
11627 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11628 /* Protocol must be DLTS, not TLS */
11629 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11631 /* Version must be 1.2 */
11632 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11633 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11634 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11635 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11636 /* We must be using an AEAD ciphersuite */
11637 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11638 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11639 /* Renegotiation must not be enabled */
11640 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11641 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011642
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011643 /*
11644 * Version and format identifier
11645 */
11646 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011647
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011648 if( used <= buf_len )
11649 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011650 mbedtls_platform_memcpy( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011651 sizeof( ssl_serialized_context_header ) );
11652 p += sizeof( ssl_serialized_context_header );
11653 }
11654
11655 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011656 * Session (length + data)
11657 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011658 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011659 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11660 return( ret );
11661
11662 used += 4 + session_len;
11663 if( used <= buf_len )
11664 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011665 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011666
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011667 ret = ssl_session_save( ssl->session, 1,
11668 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011669 if( ret != 0 )
11670 return( ret );
11671
11672 p += session_len;
11673 }
11674
11675 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011676 * Transform
11677 */
11678 used += sizeof( ssl->transform->randbytes );
11679 if( used <= buf_len )
11680 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011681 mbedtls_platform_memcpy( p, ssl->transform->randbytes,
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011682 sizeof( ssl->transform->randbytes ) );
11683 p += sizeof( ssl->transform->randbytes );
11684 }
11685
11686#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11687 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11688 if( used <= buf_len )
11689 {
11690 *p++ = ssl->transform->in_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011691 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11692 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011693 p += ssl->transform->in_cid_len;
11694
11695 *p++ = ssl->transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011696 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11697 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011698 p += ssl->transform->out_cid_len;
11699 }
11700#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11701
11702 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011703 * Saved fields from top-level ssl_context structure
11704 */
11705#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11706 used += 4;
11707 if( used <= buf_len )
11708 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011709 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011710 }
11711#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11712
11713#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11714 used += 16;
11715 if( used <= buf_len )
11716 {
11717 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11718 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11719 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11720 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11721 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11722 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11723 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11724 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11725
11726 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11727 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11728 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11729 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11730 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11731 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11732 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11733 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11734 }
11735#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11736
11737#if defined(MBEDTLS_SSL_PROTO_DTLS)
11738 used += 1;
11739 if( used <= buf_len )
11740 {
11741 *p++ = ssl->disable_datagram_packing;
11742 }
11743#endif /* MBEDTLS_SSL_PROTO_DTLS */
11744
11745 used += 8;
11746 if( used <= buf_len )
11747 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011748 mbedtls_platform_memcpy( p, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011749 p += 8;
11750 }
11751
11752#if defined(MBEDTLS_SSL_PROTO_DTLS)
11753 used += 2;
11754 if( used <= buf_len )
11755 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011756 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011757 }
11758#endif /* MBEDTLS_SSL_PROTO_DTLS */
11759
11760#if defined(MBEDTLS_SSL_ALPN)
11761 {
11762 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011763 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011764 : 0;
11765
11766 used += 1 + alpn_len;
11767 if( used <= buf_len )
11768 {
11769 *p++ = alpn_len;
11770
11771 if( ssl->alpn_chosen != NULL )
11772 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011773 mbedtls_platform_memcpy( p, ssl->alpn_chosen, alpn_len );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011774 p += alpn_len;
11775 }
11776 }
11777 }
11778#endif /* MBEDTLS_SSL_ALPN */
11779
11780 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011781 * Done
11782 */
11783 *olen = used;
11784
11785 if( used > buf_len )
11786 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011787
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011788 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11789
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011790 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011791}
11792
11793/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011794 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011795 *
11796 * This internal version is wrapped by a public function that cleans up in
11797 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011798 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011799static int ssl_context_load( mbedtls_ssl_context *ssl,
11800 const unsigned char *buf,
11801 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011802{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011803 const unsigned char *p = buf;
11804 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011805 size_t session_len;
11806 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011807
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011808 /*
11809 * The context should have been freshly setup or reset.
11810 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011811 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011812 * renegotiating, or if the user mistakenly loaded a session first.)
11813 */
11814 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11815 ssl->session != NULL )
11816 {
11817 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11818 }
11819
11820 /*
11821 * We can't check that the config matches the initial one, but we can at
11822 * least check it matches the requirements for serializing.
11823 */
11824 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011825 mbedtls_ssl_ver_lt(
11826 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ),
11827 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11828 mbedtls_ssl_ver_gt(
11829 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ),
11830 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11831 mbedtls_ssl_ver_lt(
11832 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ),
11833 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
11834 mbedtls_ssl_ver_gt(
11835 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ),
11836 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011837 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011838 {
11839 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11840 }
11841
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011842 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11843
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011844 /*
11845 * Check version identifier
11846 */
11847 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11848 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11849
Teppo Järvelin650343c2019-10-03 15:36:59 +030011850 // use regular memcmp as header is not that critical
11851 if( memcmp( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011852 sizeof( ssl_serialized_context_header ) ) != 0 )
11853 {
11854 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11855 }
11856 p += sizeof( ssl_serialized_context_header );
11857
11858 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011859 * Session
11860 */
11861 if( (size_t)( end - p ) < 4 )
11862 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11863
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011864 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011865 p += 4;
11866
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011867 /* This has been allocated by ssl_handshake_init(), called by
11868 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11869 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011870 ssl->session_in = ssl->session;
11871 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011872 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011873
11874 if( (size_t)( end - p ) < session_len )
11875 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11876
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011877 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011878 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011879 {
11880 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011881 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011882 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011883
11884 p += session_len;
11885
11886 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011887 * Transform
11888 */
11889
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011890 /* This has been allocated by ssl_handshake_init(), called by
11891 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11892 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011893 ssl->transform_in = ssl->transform;
11894 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011895 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011896
11897 /* Read random bytes and populate structure */
11898 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11899 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11900
11901 ret = ssl_populate_transform( ssl->transform,
11902 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11903 ssl->session->master,
11904#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11905#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11906 ssl->session->encrypt_then_mac,
11907#endif
11908#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11909 ssl->session->trunc_hmac,
11910#endif
11911#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11912#if defined(MBEDTLS_ZLIB_SUPPORT)
11913 ssl->session->compression,
11914#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011915 p, /* currently pointing to randbytes */
11916 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11917 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11918 ssl );
11919 if( ret != 0 )
11920 return( ret );
11921
11922 p += sizeof( ssl->transform->randbytes );
11923
11924#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11925 /* Read connection IDs and store them */
11926 if( (size_t)( end - p ) < 1 )
11927 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11928
11929 ssl->transform->in_cid_len = *p++;
11930
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011931 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011932 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11933
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011934 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11935 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011936 p += ssl->transform->in_cid_len;
11937
11938 ssl->transform->out_cid_len = *p++;
11939
11940 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11941 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11942
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011943 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11944 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011945 p += ssl->transform->out_cid_len;
11946#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11947
11948 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011949 * Saved fields from top-level ssl_context structure
11950 */
11951#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11952 if( (size_t)( end - p ) < 4 )
11953 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11954
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011955 ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011956 p += 4;
11957#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11958
11959#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11960 if( (size_t)( end - p ) < 16 )
11961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11962
11963 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11964 ( (uint64_t) p[1] << 48 ) |
11965 ( (uint64_t) p[2] << 40 ) |
11966 ( (uint64_t) p[3] << 32 ) |
11967 ( (uint64_t) p[4] << 24 ) |
11968 ( (uint64_t) p[5] << 16 ) |
11969 ( (uint64_t) p[6] << 8 ) |
11970 ( (uint64_t) p[7] );
11971 p += 8;
11972
11973 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11974 ( (uint64_t) p[1] << 48 ) |
11975 ( (uint64_t) p[2] << 40 ) |
11976 ( (uint64_t) p[3] << 32 ) |
11977 ( (uint64_t) p[4] << 24 ) |
11978 ( (uint64_t) p[5] << 16 ) |
11979 ( (uint64_t) p[6] << 8 ) |
11980 ( (uint64_t) p[7] );
11981 p += 8;
11982#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11983
11984#if defined(MBEDTLS_SSL_PROTO_DTLS)
11985 if( (size_t)( end - p ) < 1 )
11986 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11987
11988 ssl->disable_datagram_packing = *p++;
11989#endif /* MBEDTLS_SSL_PROTO_DTLS */
11990
11991 if( (size_t)( end - p ) < 8 )
11992 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11993
Teppo Järvelin91d79382019-10-02 09:09:31 +030011994 mbedtls_platform_memcpy( ssl->cur_out_ctr, p, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011995 p += 8;
11996
11997#if defined(MBEDTLS_SSL_PROTO_DTLS)
11998 if( (size_t)( end - p ) < 2 )
11999 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030012000 ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012001 p += 2;
12002#endif /* MBEDTLS_SSL_PROTO_DTLS */
12003
12004#if defined(MBEDTLS_SSL_ALPN)
12005 {
12006 uint8_t alpn_len;
12007 const char **cur;
12008
12009 if( (size_t)( end - p ) < 1 )
12010 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12011
12012 alpn_len = *p++;
12013
12014 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
12015 {
12016 /* alpn_chosen should point to an item in the configured list */
12017 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
12018 {
12019 if( strlen( *cur ) == alpn_len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +030012020 mbedtls_platform_memcmp( p, cur, alpn_len ) == 0 )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012021 {
12022 ssl->alpn_chosen = *cur;
12023 break;
12024 }
12025 }
12026 }
12027
12028 /* can only happen on conf mismatch */
12029 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
12030 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12031
12032 p += alpn_len;
12033 }
12034#endif /* MBEDTLS_SSL_ALPN */
12035
12036 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012037 * Forced fields from top-level ssl_context structure
12038 *
12039 * Most of them already set to the correct value by mbedtls_ssl_init() and
12040 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
12041 */
12042 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
12043
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012044#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012045 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012046#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
12047#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012048 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012049#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012050
Hanno Becker83985822019-08-30 10:42:49 +010012051 /* Adjust pointers for header fields of outgoing records to
12052 * the given transform, accounting for explicit IV and CID. */
12053 ssl_update_out_pointers( ssl, ssl->transform );
12054
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012055#if defined(MBEDTLS_SSL_PROTO_DTLS)
12056 ssl->in_epoch = 1;
12057#endif
12058
12059 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
12060 * which we don't want - otherwise we'd end up freeing the wrong transform
12061 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
12062 if( ssl->handshake != NULL )
12063 {
12064 mbedtls_ssl_handshake_free( ssl );
12065 mbedtls_free( ssl->handshake );
12066 ssl->handshake = NULL;
12067 }
12068
12069 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020012070 * Done - should have consumed entire buffer
12071 */
12072 if( p != end )
12073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012074
12075 return( 0 );
12076}
12077
12078/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020012079 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012080 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012081int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012082 const unsigned char *buf,
12083 size_t len )
12084{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012085 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012086
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012087 if( ret != 0 )
12088 mbedtls_ssl_free( context );
12089
12090 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012091}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020012092#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012093
12094/*
Paul Bakker5121ce52009-01-03 21:22:43 +000012095 * Free an SSL context
12096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012097void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000012098{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020012099 if( ssl == NULL )
12100 return;
12101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012103
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012104 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012105 {
Angus Grattond8213d02016-05-25 20:56:48 +100012106 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012107 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012108 }
12109
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012110 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012111 {
Angus Grattond8213d02016-05-25 20:56:48 +100012112 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012113 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012114 }
12115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012116#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020012117 if( ssl->compress_buf != NULL )
12118 {
Angus Grattond8213d02016-05-25 20:56:48 +100012119 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012120 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020012121 }
12122#endif
12123
Paul Bakker48916f92012-09-16 19:57:18 +000012124 if( ssl->transform )
12125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012126 mbedtls_ssl_transform_free( ssl->transform );
12127 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000012128 }
12129
12130 if( ssl->handshake )
12131 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020012132 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012133 mbedtls_ssl_transform_free( ssl->transform_negotiate );
12134 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012136 mbedtls_free( ssl->handshake );
12137 mbedtls_free( ssl->transform_negotiate );
12138 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012139 }
12140
Paul Bakkerc0463502013-02-14 11:19:38 +010012141 if( ssl->session )
12142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012143 mbedtls_ssl_session_free( ssl->session );
12144 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010012145 }
12146
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030012147#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020012148 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012149 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012150 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012151 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000012152 }
Paul Bakker0be444a2013-08-27 21:55:01 +020012153#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000012154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012155#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
12156 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000012157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
12159 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000012160 }
12161#endif
12162
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012163#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012164 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020012165#endif
12166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000012168
Paul Bakker86f04f42013-02-14 11:20:09 +010012169 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012170 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012171}
12172
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012173/*
12174 * Initialze mbedtls_ssl_config
12175 */
12176void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
12177{
12178 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010012179
12180#if !defined(MBEDTLS_SSL_PROTO_TLS)
12181 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
12182#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012183}
12184
Simon Butcherc97b6972015-12-27 23:48:17 +000012185#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012186#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012187static int ssl_preset_default_hashes[] = {
12188#if defined(MBEDTLS_SHA512_C)
12189 MBEDTLS_MD_SHA512,
12190 MBEDTLS_MD_SHA384,
12191#endif
12192#if defined(MBEDTLS_SHA256_C)
12193 MBEDTLS_MD_SHA256,
12194 MBEDTLS_MD_SHA224,
12195#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020012196#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012197 MBEDTLS_MD_SHA1,
12198#endif
12199 MBEDTLS_MD_NONE
12200};
Simon Butcherc97b6972015-12-27 23:48:17 +000012201#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012202#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012203
Hanno Becker73f4cb12019-06-27 13:51:07 +010012204#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012205static int ssl_preset_suiteb_ciphersuites[] = {
12206 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
12207 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
12208 0
12209};
Hanno Becker73f4cb12019-06-27 13:51:07 +010012210#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012211
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012212#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012213#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012214static int ssl_preset_suiteb_hashes[] = {
12215 MBEDTLS_MD_SHA256,
12216 MBEDTLS_MD_SHA384,
12217 MBEDTLS_MD_NONE
12218};
12219#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012220#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012221
Hanno Beckerc1096e72019-06-19 12:30:41 +010012222#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012223static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010012224#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012225 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012226#endif
12227#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012228 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012229#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012230 MBEDTLS_ECP_DP_NONE
12231};
12232#endif
12233
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012234/*
Tillmann Karras588ad502015-09-25 04:27:22 +020012235 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012236 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012237int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012238 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012239{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012240#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012241 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012242#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012243
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020012244 /* Use the functions here so that they are covered in tests,
12245 * but otherwise access member directly for efficiency */
12246 mbedtls_ssl_conf_endpoint( conf, endpoint );
12247 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012248
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012249 /*
12250 * Things that are common to all presets
12251 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012252#if defined(MBEDTLS_SSL_CLI_C)
12253 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
12254 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012255#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012256 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012257#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012258#if defined(MBEDTLS_SSL_SESSION_TICKETS)
12259 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
12260#endif
12261 }
12262#endif
12263
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012264#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012265 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012266#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012267
12268#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
12269 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
12270#endif
12271
12272#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010012273#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012274 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012275#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
12276#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030012277 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030012278 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012279#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012280#endif
12281
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010012282#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
12283 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
12284#endif
12285
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012286#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012287 conf->f_cookie_write = ssl_cookie_write_dummy;
12288 conf->f_cookie_check = ssl_cookie_check_dummy;
12289#endif
12290
Hanno Becker7f376f42019-06-12 16:20:48 +010012291#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
12292 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012293 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
12294#endif
12295
Janos Follath088ce432017-04-10 12:42:31 +010012296#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012297#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010012298 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012299#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
12300#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010012301
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012302#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010012303#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012304 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012305#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
12306#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012307 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012308#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
12309#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012310
12311#if defined(MBEDTLS_SSL_RENEGOTIATION)
12312 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020012313 mbedtls_platform_memset( conf->renego_period, 0x00, 2 );
12314 mbedtls_platform_memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012315#endif
12316
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012317#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
12318 if( endpoint == MBEDTLS_SSL_IS_SERVER )
12319 {
Hanno Becker00d0a682017-10-04 13:14:29 +010012320 const unsigned char dhm_p[] =
12321 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
12322 const unsigned char dhm_g[] =
12323 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
12324
Hanno Beckera90658f2017-10-04 15:29:08 +010012325 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
12326 dhm_p, sizeof( dhm_p ),
12327 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012328 {
12329 return( ret );
12330 }
12331 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020012332#endif
12333
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012334 /*
12335 * Preset-specific defaults
12336 */
12337 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012338 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012339 /*
12340 * NSA Suite B
12341 */
12342 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010012343#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012344 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010012345#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12346#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012347 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010012348#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12349#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012350 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012351#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12352#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012353 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012354#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012355
Hanno Becker73f4cb12019-06-27 13:51:07 +010012356#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012357 conf->ciphersuite_list[0] =
12358 conf->ciphersuite_list[1] =
12359 conf->ciphersuite_list[2] =
12360 conf->ciphersuite_list[3] =
12361 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010012362#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012363
12364#if defined(MBEDTLS_X509_CRT_PARSE_C)
12365 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012366#endif
12367
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012368#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012369#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012370 conf->sig_hashes = ssl_preset_suiteb_hashes;
12371#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012372#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012373
12374#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012375#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012376 conf->curve_list = ssl_preset_suiteb_curves;
12377#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012378#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020012379 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012380
12381 /*
12382 * Default
12383 */
12384 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010012385#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012386 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
12387 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
12388 MBEDTLS_SSL_MIN_MAJOR_VERSION :
12389 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012390#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12391#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012392 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
12393 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
12394 MBEDTLS_SSL_MIN_MINOR_VERSION :
12395 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012396#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020012397 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012398 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
12399#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010012400#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12401#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
12402 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12403#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12404#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12405 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12406#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012407
Hanno Becker73f4cb12019-06-27 13:51:07 +010012408#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012409 conf->ciphersuite_list[0] =
12410 conf->ciphersuite_list[1] =
12411 conf->ciphersuite_list[2] =
12412 conf->ciphersuite_list[3] =
12413 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012414#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012415
12416#if defined(MBEDTLS_X509_CRT_PARSE_C)
12417 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12418#endif
12419
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012420#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012421#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012422 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012423#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012424#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012425
12426#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012427#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012428 conf->curve_list = mbedtls_ecp_grp_id_list();
12429#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012430#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012431
12432#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12433 conf->dhm_min_bitlen = 1024;
12434#endif
12435 }
12436
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012437 return( 0 );
12438}
12439
12440/*
12441 * Free mbedtls_ssl_config
12442 */
12443void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12444{
12445#if defined(MBEDTLS_DHM_C)
12446 mbedtls_mpi_free( &conf->dhm_P );
12447 mbedtls_mpi_free( &conf->dhm_G );
12448#endif
12449
12450#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12451 if( conf->psk != NULL )
12452 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012453 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012454 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012455 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012456 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012457 }
12458
12459 if( conf->psk_identity != NULL )
12460 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012461 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012462 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012463 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012464 conf->psk_identity_len = 0;
12465 }
12466#endif
12467
12468#if defined(MBEDTLS_X509_CRT_PARSE_C)
12469 ssl_key_cert_free( conf->key_cert );
12470#endif
12471
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012472 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012473}
12474
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012475#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012476 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12477 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012478/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012479 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012481unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012482{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012483#if defined(MBEDTLS_RSA_C)
12484 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12485 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012486#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012487#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012488 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12489 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012490#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012491 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012492}
12493
Hanno Becker7e5437a2017-04-28 17:15:26 +010012494unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12495{
12496 switch( type ) {
12497 case MBEDTLS_PK_RSA:
12498 return( MBEDTLS_SSL_SIG_RSA );
12499 case MBEDTLS_PK_ECDSA:
12500 case MBEDTLS_PK_ECKEY:
12501 return( MBEDTLS_SSL_SIG_ECDSA );
12502 default:
12503 return( MBEDTLS_SSL_SIG_ANON );
12504 }
12505}
12506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012507mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012508{
12509 switch( sig )
12510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012511#if defined(MBEDTLS_RSA_C)
12512 case MBEDTLS_SSL_SIG_RSA:
12513 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012514#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012515#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012516 case MBEDTLS_SSL_SIG_ECDSA:
12517 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012518#endif
12519 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012520 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012521 }
12522}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012523#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012524
Hanno Becker7e5437a2017-04-28 17:15:26 +010012525#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12526 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12527
12528/* Find an entry in a signature-hash set matching a given hash algorithm. */
12529mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12530 mbedtls_pk_type_t sig_alg )
12531{
12532 switch( sig_alg )
12533 {
12534 case MBEDTLS_PK_RSA:
12535 return( set->rsa );
12536 case MBEDTLS_PK_ECDSA:
12537 return( set->ecdsa );
12538 default:
12539 return( MBEDTLS_MD_NONE );
12540 }
12541}
12542
12543/* Add a signature-hash-pair to a signature-hash set */
12544void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12545 mbedtls_pk_type_t sig_alg,
12546 mbedtls_md_type_t md_alg )
12547{
12548 switch( sig_alg )
12549 {
12550 case MBEDTLS_PK_RSA:
12551 if( set->rsa == MBEDTLS_MD_NONE )
12552 set->rsa = md_alg;
12553 break;
12554
12555 case MBEDTLS_PK_ECDSA:
12556 if( set->ecdsa == MBEDTLS_MD_NONE )
12557 set->ecdsa = md_alg;
12558 break;
12559
12560 default:
12561 break;
12562 }
12563}
12564
12565/* Allow exactly one hash algorithm for each signature. */
12566void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12567 mbedtls_md_type_t md_alg )
12568{
12569 set->rsa = md_alg;
12570 set->ecdsa = md_alg;
12571}
12572
12573#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12574 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12575
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012576/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012577 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012579mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012580{
12581 switch( hash )
12582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012583#if defined(MBEDTLS_MD5_C)
12584 case MBEDTLS_SSL_HASH_MD5:
12585 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012586#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012587#if defined(MBEDTLS_SHA1_C)
12588 case MBEDTLS_SSL_HASH_SHA1:
12589 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012590#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012591#if defined(MBEDTLS_SHA256_C)
12592 case MBEDTLS_SSL_HASH_SHA224:
12593 return( MBEDTLS_MD_SHA224 );
12594 case MBEDTLS_SSL_HASH_SHA256:
12595 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012596#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012597#if defined(MBEDTLS_SHA512_C)
12598 case MBEDTLS_SSL_HASH_SHA384:
12599 return( MBEDTLS_MD_SHA384 );
12600 case MBEDTLS_SSL_HASH_SHA512:
12601 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012602#endif
12603 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012604 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012605 }
12606}
12607
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012608/*
12609 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12610 */
12611unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12612{
12613 switch( md )
12614 {
12615#if defined(MBEDTLS_MD5_C)
12616 case MBEDTLS_MD_MD5:
12617 return( MBEDTLS_SSL_HASH_MD5 );
12618#endif
12619#if defined(MBEDTLS_SHA1_C)
12620 case MBEDTLS_MD_SHA1:
12621 return( MBEDTLS_SSL_HASH_SHA1 );
12622#endif
12623#if defined(MBEDTLS_SHA256_C)
12624 case MBEDTLS_MD_SHA224:
12625 return( MBEDTLS_SSL_HASH_SHA224 );
12626 case MBEDTLS_MD_SHA256:
12627 return( MBEDTLS_SSL_HASH_SHA256 );
12628#endif
12629#if defined(MBEDTLS_SHA512_C)
12630 case MBEDTLS_MD_SHA384:
12631 return( MBEDTLS_SSL_HASH_SHA384 );
12632 case MBEDTLS_MD_SHA512:
12633 return( MBEDTLS_SSL_HASH_SHA512 );
12634#endif
12635 default:
12636 return( MBEDTLS_SSL_HASH_NONE );
12637 }
12638}
12639
Hanno Beckeree902df2019-08-23 13:47:47 +010012640#if defined(MBEDTLS_USE_TINYCRYPT)
12641/*
12642 * Check if a curve proposed by the peer is in our list.
12643 * Return 0 if we're willing to use it, -1 otherwise.
12644 */
12645int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12646 mbedtls_uecc_group_id grp_id )
12647{
12648 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12649 if( own_ec_id == grp_id )
12650 return( 0 );
12651 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12652
12653 return( -1 );
12654}
12655#endif /* MBEDTLS_USE_TINYCRYPT */
12656
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012657#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012658/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012659 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012660 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012661 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012662int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12663 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012664{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012665 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12666 if( own_ec_id == grp_id )
12667 return( 0 );
12668 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012669
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012670 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012671}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012672#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012673
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012674#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012675/*
12676 * Check if a hash proposed by the peer is in our list.
12677 * Return 0 if we're willing to use it, -1 otherwise.
12678 */
12679int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12680 mbedtls_md_type_t md )
12681{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012682 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12683 if( md_alg == md )
12684 return( 0 );
12685 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012686
12687 return( -1 );
12688}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012689#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012691#if defined(MBEDTLS_X509_CRT_PARSE_C)
12692int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012693 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012694 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012695 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012696{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012697 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012698#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012699 int usage = 0;
12700#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012701#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012702 const char *ext_oid;
12703 size_t ext_len;
12704#endif
12705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012706#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12707 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012708 ((void) cert);
12709 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012710 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012711#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012713#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12714 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012715 {
12716 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012717 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012719 case MBEDTLS_KEY_EXCHANGE_RSA:
12720 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012721 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012722 break;
12723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012724 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12725 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12726 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12727 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012728 break;
12729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012730 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12731 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012732 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012733 break;
12734
12735 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012736 case MBEDTLS_KEY_EXCHANGE_NONE:
12737 case MBEDTLS_KEY_EXCHANGE_PSK:
12738 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12739 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012740 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012741 usage = 0;
12742 }
12743 }
12744 else
12745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012746 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12747 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012748 }
12749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012750 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012751 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012752 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012753 ret = -1;
12754 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012755#else
12756 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012757#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012759#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12760 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012762 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12763 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012764 }
12765 else
12766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012767 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12768 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012769 }
12770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012771 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012772 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012773 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012774 ret = -1;
12775 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012776#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012777
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012778 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012779}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012780#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012781
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012782#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12783 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12784int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12785 unsigned char *output,
12786 unsigned char *data, size_t data_len )
12787{
12788 int ret = 0;
12789 mbedtls_md5_context mbedtls_md5;
12790 mbedtls_sha1_context mbedtls_sha1;
12791
12792 mbedtls_md5_init( &mbedtls_md5 );
12793 mbedtls_sha1_init( &mbedtls_sha1 );
12794
12795 /*
12796 * digitally-signed struct {
12797 * opaque md5_hash[16];
12798 * opaque sha_hash[20];
12799 * };
12800 *
12801 * md5_hash
12802 * MD5(ClientHello.random + ServerHello.random
12803 * + ServerParams);
12804 * sha_hash
12805 * SHA(ClientHello.random + ServerHello.random
12806 * + ServerParams);
12807 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012808 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012809 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012810 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012811 goto exit;
12812 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012813 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012814 ssl->handshake->randbytes, 64 ) ) != 0 )
12815 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012817 goto exit;
12818 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012819 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012820 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012822 goto exit;
12823 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012824 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012825 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012826 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012827 goto exit;
12828 }
12829
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012830 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012831 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012832 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012833 goto exit;
12834 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012835 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012836 ssl->handshake->randbytes, 64 ) ) != 0 )
12837 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012838 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012839 goto exit;
12840 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012841 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012842 data_len ) ) != 0 )
12843 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012844 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012845 goto exit;
12846 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012847 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012848 output + 16 ) ) != 0 )
12849 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012850 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012851 goto exit;
12852 }
12853
12854exit:
12855 mbedtls_md5_free( &mbedtls_md5 );
12856 mbedtls_sha1_free( &mbedtls_sha1 );
12857
12858 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012859 mbedtls_ssl_pend_fatal_alert( ssl,
12860 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012861
12862 return( ret );
12863
12864}
12865#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12866 MBEDTLS_SSL_PROTO_TLS1_1 */
12867
12868#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12869 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12870int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012871 unsigned char *hash, size_t *hashlen,
12872 unsigned char *data, size_t data_len,
12873 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012874{
12875 int ret = 0;
12876 mbedtls_md_context_t ctx;
Hanno Beckera5cedbc2019-07-17 11:21:02 +010012877 mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012878 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012879
12880 mbedtls_md_init( &ctx );
12881
12882 /*
12883 * digitally-signed struct {
12884 * opaque client_random[32];
12885 * opaque server_random[32];
12886 * ServerDHParams params;
12887 * };
12888 */
12889 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12890 {
12891 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12892 goto exit;
12893 }
12894 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12895 {
12896 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12897 goto exit;
12898 }
12899 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12900 {
12901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12902 goto exit;
12903 }
12904 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12905 {
12906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12907 goto exit;
12908 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012909 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012910 {
12911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12912 goto exit;
12913 }
12914
12915exit:
12916 mbedtls_md_free( &ctx );
12917
12918 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012919 mbedtls_ssl_pend_fatal_alert( ssl,
12920 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012921
12922 return( ret );
12923}
12924#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12925 MBEDTLS_SSL_PROTO_TLS1_2 */
12926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012927#endif /* MBEDTLS_SSL_TLS_C */