blob: bfcfcbbc60163234b017b87912ef8e8dbeae3a0e [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ärvelin8f7e36f2020-01-02 10:40:19 +0200794 (void)mbedtls_platform_memcpy( tmp + md_len, label, nb );
795 (void)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
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200804 if ( ( ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ) ) != 0 )
805 return( ret );
806 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ) ) != 0 )
807 return( ret );
808 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, tmp ) ) != 0 )
809 return( ret );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100810
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100811 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000812 {
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200813 if ( ( ret = mbedtls_md_hmac_reset ( &md_ctx ) ) != 0 )
814 return( ret );
815 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ) ) != 0 )
816 return( ret );
817 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, h_i ) ) != 0 )
818 return( ret );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100819
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200820 if ( ( ret = mbedtls_md_hmac_reset ( &md_ctx ) ) != 0 )
821 return( ret );
822 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ) ) != 0 )
823 return( ret );
824 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, tmp ) ) != 0 )
825 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000826
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100827 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000828
829 for( j = 0; j < k; j++ )
830 dstbuf[i + j] = h_i[j];
831 }
832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100834
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200835 (void)mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
836 (void)mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000837
838 return( 0 );
839}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100842MBEDTLS_NO_INLINE static int tls_prf_sha256(
843 const unsigned char *secret, size_t slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100844 const char *label,
845 const unsigned char *random, size_t rlen,
846 unsigned char *dstbuf, size_t dlen )
847{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100849 label, random, rlen, dstbuf, dlen ) );
850}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100854MBEDTLS_NO_INLINE static int tls_prf_sha384(
855 const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200856 const char *label,
857 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000858 unsigned char *dstbuf, size_t dlen )
859{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100861 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000862}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863#endif /* MBEDTLS_SHA512_C */
864#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000865
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100866/*
867 * Call the appropriate PRF function
868 */
Hanno Becker2793f742019-08-16 14:28:43 +0100869MBEDTLS_ALWAYS_INLINE static inline int ssl_prf( int minor_ver,
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100870 mbedtls_md_type_t hash,
871 const unsigned char *secret, size_t slen,
872 const char *label,
873 const unsigned char *random, size_t rlen,
874 unsigned char *dstbuf, size_t dlen )
875{
876#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
877 (void) hash;
878#endif
879
880#if defined(MBEDTLS_SSL_PROTO_SSL3)
881 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
882 return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
883 else
884#endif
885#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +0100886 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100887 return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
888 else
889#endif
890#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
891#if defined(MBEDTLS_SHA512_C)
892 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
893 hash == MBEDTLS_MD_SHA384 )
894 {
895 return( tls_prf_sha384( secret, slen, label, random, rlen,
896 dstbuf, dlen ) );
897 }
898 else
899#endif
900#if defined(MBEDTLS_SHA256_C)
901 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
902 {
903 return( tls_prf_sha256( secret, slen, label, random, rlen,
904 dstbuf, dlen ) );
905 }
906#endif
907#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
908
909 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
910}
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200911
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100912#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100913MBEDTLS_NO_INLINE static void ssl_calc_finished_ssl(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100914 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
915{
916 const char *sender;
917 mbedtls_md5_context md5;
918 mbedtls_sha1_context sha1;
919
920 unsigned char padbuf[48];
921 unsigned char md5sum[16];
922 unsigned char sha1sum[20];
923
924 mbedtls_ssl_session *session = ssl->session_negotiate;
925 if( !session )
926 session = ssl->session;
927
928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
929
930 mbedtls_md5_init( &md5 );
931 mbedtls_sha1_init( &sha1 );
932
933 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
934 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
935
936 /*
937 * SSLv3:
938 * hash =
939 * MD5( master + pad2 +
940 * MD5( handshake + sender + master + pad1 ) )
941 * + SHA1( master + pad2 +
942 * SHA1( handshake + sender + master + pad1 ) )
943 */
944
945#if !defined(MBEDTLS_MD5_ALT)
946 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
947 md5.state, sizeof( md5.state ) );
948#endif
949
950#if !defined(MBEDTLS_SHA1_ALT)
951 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
952 sha1.state, sizeof( sha1.state ) );
953#endif
954
955 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
956 : "SRVR";
957
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200958 mbedtls_platform_memset( padbuf, 0x36, 48 );
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100959
960 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
961 mbedtls_md5_update_ret( &md5, session->master, 48 );
962 mbedtls_md5_update_ret( &md5, padbuf, 48 );
963 mbedtls_md5_finish_ret( &md5, md5sum );
964
965 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
966 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
967 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
968 mbedtls_sha1_finish_ret( &sha1, sha1sum );
969
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200970 mbedtls_platform_memset( padbuf, 0x5C, 48 );
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100971
972 mbedtls_md5_starts_ret( &md5 );
973 mbedtls_md5_update_ret( &md5, session->master, 48 );
974 mbedtls_md5_update_ret( &md5, padbuf, 48 );
975 mbedtls_md5_update_ret( &md5, md5sum, 16 );
976 mbedtls_md5_finish_ret( &md5, buf );
977
978 mbedtls_sha1_starts_ret( &sha1 );
979 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
980 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
981 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
982 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
983
984 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
985
986 mbedtls_md5_free( &md5 );
987 mbedtls_sha1_free( &sha1 );
988
989 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
990 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
991 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
992
993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
994}
995#endif /* MBEDTLS_SSL_PROTO_SSL3 */
996
997#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100998MBEDTLS_NO_INLINE static void ssl_calc_finished_tls(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100999 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1000{
1001 int len = 12;
1002 const char *sender;
1003 mbedtls_md5_context md5;
1004 mbedtls_sha1_context sha1;
1005 unsigned char padbuf[36];
1006
1007 mbedtls_ssl_session *session = ssl->session_negotiate;
1008 if( !session )
1009 session = ssl->session;
1010
1011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
1012
1013 mbedtls_md5_init( &md5 );
1014 mbedtls_sha1_init( &sha1 );
1015
1016 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1017 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1018
1019 /*
1020 * TLSv1:
1021 * hash = PRF( master, finished_label,
1022 * MD5( handshake ) + SHA1( handshake ) )[0..11]
1023 */
1024
1025#if !defined(MBEDTLS_MD5_ALT)
1026 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1027 md5.state, sizeof( md5.state ) );
1028#endif
1029
1030#if !defined(MBEDTLS_SHA1_ALT)
1031 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1032 sha1.state, sizeof( sha1.state ) );
1033#endif
1034
1035 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1036 ? "client finished"
1037 : "server finished";
1038
1039 mbedtls_md5_finish_ret( &md5, padbuf );
1040 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
1041
1042 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1043 mbedtls_ssl_suite_get_mac(
1044 mbedtls_ssl_ciphersuite_from_id(
1045 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1046 session->master, 48, sender,
1047 padbuf, 36, buf, len );
1048
1049 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1050
1051 mbedtls_md5_free( &md5 );
1052 mbedtls_sha1_free( &sha1 );
1053
1054 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1055
1056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1057}
1058#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1059
1060#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1061#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001062MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha256(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001063 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1064{
1065 int len = 12;
1066 const char *sender;
1067 mbedtls_sha256_context sha256;
1068 unsigned char padbuf[32];
1069
1070 mbedtls_ssl_session *session = ssl->session_negotiate;
1071 if( !session )
1072 session = ssl->session;
1073
1074 mbedtls_sha256_init( &sha256 );
1075
1076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
1077
1078 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1079
1080 /*
1081 * TLSv1.2:
1082 * hash = PRF( master, finished_label,
1083 * Hash( handshake ) )[0.11]
1084 */
1085
1086#if !defined(MBEDTLS_SHA256_ALT)
1087 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
1088 sha256.state, sizeof( sha256.state ) );
1089#endif
1090
1091 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1092 ? "client finished"
1093 : "server finished";
1094
1095 mbedtls_sha256_finish_ret( &sha256, padbuf );
1096
1097 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1098 mbedtls_ssl_suite_get_mac(
1099 mbedtls_ssl_ciphersuite_from_id(
1100 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1101 session->master, 48, sender,
1102 padbuf, 32, buf, len );
1103
1104 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1105
1106 mbedtls_sha256_free( &sha256 );
1107
1108 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1109
1110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1111}
1112#endif /* MBEDTLS_SHA256_C */
1113
1114#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001115MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha384(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001116 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1117{
1118 int len = 12;
1119 const char *sender;
1120 mbedtls_sha512_context sha512;
1121 unsigned char padbuf[48];
1122
1123 mbedtls_ssl_session *session = ssl->session_negotiate;
1124 if( !session )
1125 session = ssl->session;
1126
1127 mbedtls_sha512_init( &sha512 );
1128
1129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
1130
1131 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1132
1133 /*
1134 * TLSv1.2:
1135 * hash = PRF( master, finished_label,
1136 * Hash( handshake ) )[0.11]
1137 */
1138
1139#if !defined(MBEDTLS_SHA512_ALT)
1140 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
1141 sha512.state, sizeof( sha512.state ) );
1142#endif
1143
1144 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1145 ? "client finished"
1146 : "server finished";
1147
1148 mbedtls_sha512_finish_ret( &sha512, padbuf );
1149
1150 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1151 mbedtls_ssl_suite_get_mac(
1152 mbedtls_ssl_ciphersuite_from_id(
1153 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1154 session->master, 48, sender,
1155 padbuf, 48, buf, len );
1156
1157 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1158
1159 mbedtls_sha512_free( &sha512 );
1160
1161 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1162
1163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1164}
1165#endif /* MBEDTLS_SHA512_C */
1166#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1167
Hanno Becker2793f742019-08-16 14:28:43 +01001168MBEDTLS_ALWAYS_INLINE static inline int ssl_calc_finished(
1169 int minor_ver,
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001170 mbedtls_md_type_t hash,
1171 mbedtls_ssl_context *ssl,
1172 unsigned char *buf,
1173 int from )
1174{
1175#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1176 (void) hash;
1177#endif
1178
1179#if defined(MBEDTLS_SSL_PROTO_SSL3)
1180 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1181 ssl_calc_finished_ssl( ssl, buf, from );
1182 else
1183#endif
1184#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001185 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001186 ssl_calc_finished_tls( ssl, buf, from );
1187 else
1188#endif
1189#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1190#if defined(MBEDTLS_SHA512_C)
1191 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1192 hash == MBEDTLS_MD_SHA384 )
1193 {
1194 ssl_calc_finished_tls_sha384( ssl, buf, from );
1195 }
1196 else
1197#endif
1198#if defined(MBEDTLS_SHA256_C)
1199 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1200 ssl_calc_finished_tls_sha256( ssl, buf, from );
1201 else
1202#endif
1203#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1204 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1205
1206 return( 0 );
1207}
1208
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001209/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001210 * Populate a transform structure with session keys and all the other
1211 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001212 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001213 * Parameters:
1214 * - [in/out]: transform: structure to populate
1215 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001216 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001217 * - [in] ciphersuite
1218 * - [in] master
1219 * - [in] encrypt_then_mac
1220 * - [in] trunc_hmac
1221 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001222 * - [in] tls_prf: pointer to PRF to use for key derivation
1223 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001224 * - [in] minor_ver: SSL/TLS minor version
1225 * - [in] endpoint: client or server
1226 * - [in] ssl: optionally used for:
1227 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1228 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1229 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001230 */
Hanno Becker298a4702019-08-16 10:21:32 +01001231/* Force compilers to inline this function if it's used only
1232 * from one place, because at least ARMC5 doesn't do that
1233 * automatically. */
1234#if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1235MBEDTLS_ALWAYS_INLINE static inline
1236#else
1237static
1238#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1239int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001240 int ciphersuite,
1241 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001242#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001243#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1244 int encrypt_then_mac,
1245#endif
1246#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1247 int trunc_hmac,
1248#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001249#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001250#if defined(MBEDTLS_ZLIB_SUPPORT)
1251 int compression,
1252#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001253 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001254 int minor_ver,
1255 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001256 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001257{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001258 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 unsigned char keyblk[256];
1260 unsigned char *key1;
1261 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001262 unsigned char *mac_enc;
1263 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001264 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001265 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001266 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001267 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 const mbedtls_cipher_info_t *cipher_info;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001269 mbedtls_md_handle_t md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001270
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001271#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1272 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1273 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001274 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001275 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001276#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001277
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001278 /*
1279 * Some data just needs copying into the structure
1280 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001281#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1282 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001283 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001284#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001285
1286#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001287 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001288#else
1289 ((void) minor_ver);
1290#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001292#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Teppo Järvelin91d79382019-10-02 09:09:31 +03001293 mbedtls_platform_memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001294#endif
1295
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001296 /*
1297 * Get various info structures
1298 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001299 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001300 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001301 {
1302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001303 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001304 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1305 }
1306
Hanno Becker473f98f2019-06-26 10:27:32 +01001307 cipher_info = mbedtls_cipher_info_from_type(
1308 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001309 if( cipher_info == NULL )
1310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001312 mbedtls_ssl_suite_get_cipher( 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 Becker473f98f2019-06-26 10:27:32 +01001316 md_info = mbedtls_md_info_from_type(
1317 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001318 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Paul Bakker68884e32013-01-07 18:20:04 +01001319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001321 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001323 }
1324
Hanno Beckera5a2b082019-05-15 14:03:01 +01001325#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001326 /* Copy own and peer's CID if the use of the CID
1327 * extension has been negotiated. */
1328 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1329 {
1330 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001331
Hanno Becker4932f9f2019-05-03 15:23:51 +01001332 transform->in_cid_len = ssl->own_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001333 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1334 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001335 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001336 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001337
1338 transform->out_cid_len = ssl->handshake->peer_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001339 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1340 memcpy( transform->out_cid, ssl->handshake->peer_cid,
Hanno Beckere582d122019-05-15 10:21:55 +01001341 ssl->handshake->peer_cid_len );
1342 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1343 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001344 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001345#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001346
Paul Bakker5121ce52009-01-03 21:22:43 +00001347 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001348 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001349 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001350 ret = ssl_prf( minor_ver,
1351 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1352 master, 48, "key expansion", randbytes, 64,
1353 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001354 if( ret != 0 )
1355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001357 return( ret );
1358 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001361 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001362 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001363 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001365
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 /*
1367 * Determine the appropriate key, IV and MAC length.
1368 */
Paul Bakker68884e32013-01-07 18:20:04 +01001369
Hanno Beckere7f2df02017-12-27 08:17:40 +00001370 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001371
Hanno Beckerf1229442018-01-03 15:32:31 +00001372#if defined(MBEDTLS_GCM_C) || \
1373 defined(MBEDTLS_CCM_C) || \
1374 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001376 cipher_info->mode == MBEDTLS_MODE_CCM ||
1377 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001379 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001380 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001381 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1382 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001383
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001384 /* All modes haves 96-bit IVs;
1385 * GCM and CCM has 4 implicit and 8 explicit bytes
1386 * ChachaPoly has all 12 bytes implicit
1387 */
Paul Bakker68884e32013-01-07 18:20:04 +01001388 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001389 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1390 transform->fixed_ivlen = 12;
1391 else
1392 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001393 }
1394 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001395#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1396#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1397 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1398 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001399 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001400 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1402 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001405 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001406 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001407
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001408 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001409 mac_key_len = mbedtls_md_get_size( md_info );
1410 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001413 /*
1414 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1415 * (rfc 6066 page 13 or rfc 2104 section 4),
1416 * so we only need to adjust the length here.
1417 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001418 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001421
1422#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1423 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001424 * HMAC implementation which also truncates the key
1425 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001426 mac_key_len = transform->maclen;
1427#endif
1428 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001430
1431 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001432 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001434 else
1435#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1436 {
1437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1438 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1439 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001440
Hanno Beckera9d5c452019-07-25 16:47:12 +01001441 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001442 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001443 (unsigned) transform->ivlen,
1444 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001445
1446 /*
1447 * Finally setup the cipher contexts, IVs and MAC secrets.
1448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001450 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001451 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001452 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001453 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001454
Paul Bakker68884e32013-01-07 18:20:04 +01001455 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001456 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001457
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001458 /*
1459 * This is not used in TLS v1.1.
1460 */
Paul Bakker48916f92012-09-16 19:57:18 +00001461 iv_copy_len = ( transform->fixed_ivlen ) ?
1462 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001463 mbedtls_platform_memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1464 mbedtls_platform_memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001465 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001466 }
1467 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468#endif /* MBEDTLS_SSL_CLI_C */
1469#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001470 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001471 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001472 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001473 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001474
Hanno Becker81c7b182017-11-09 18:39:33 +00001475 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001476 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001477
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001478 /*
1479 * This is not used in TLS v1.1.
1480 */
Paul Bakker48916f92012-09-16 19:57:18 +00001481 iv_copy_len = ( transform->fixed_ivlen ) ?
1482 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001483 mbedtls_platform_memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1484 mbedtls_platform_memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001485 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001486 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001487 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1491 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001492 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001493
Hanno Becker92231322018-01-03 15:32:51 +00001494#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001496 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001497 {
Hanno Becker92231322018-01-03 15:32:51 +00001498 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1501 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001502 }
1503
Teppo Järvelin91d79382019-10-02 09:09:31 +03001504 mbedtls_platform_memcpy( transform->mac_enc, mac_enc, mac_key_len );
1505 mbedtls_platform_memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001506 }
1507 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1509#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1510 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001511 if( mbedtls_ssl_ver_geq( minor_ver, MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakker68884e32013-01-07 18:20:04 +01001512 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001513 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1514 For AEAD-based ciphersuites, there is nothing to do here. */
1515 if( mac_key_len != 0 )
1516 {
1517 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1518 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1519 }
Paul Bakker68884e32013-01-07 18:20:04 +01001520 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001521 else
1522#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1525 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001526 }
Hanno Becker92231322018-01-03 15:32:51 +00001527#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1530 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001533
Hanno Beckere7f2df02017-12-27 08:17:40 +00001534 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001535 transform->iv_enc, transform->iv_dec,
1536 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001537 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001538 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1541 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001542 }
1543 }
Hanno Becker92231322018-01-03 15:32:51 +00001544#else
1545 ((void) mac_dec);
1546 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001548
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001549#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1550 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001551 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001552 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001553 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001554 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001555 iv_copy_len );
1556 }
1557#endif
1558
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001559 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001560 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001561 {
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é-Gonnard8473f872015-05-14 13:51:45 +02001566 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001567 cipher_info ) ) != 0 )
1568 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001569 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001570 return( ret );
1571 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001574 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001578 return( ret );
1579 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001582 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001586 return( ret );
1587 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589#if defined(MBEDTLS_CIPHER_MODE_CBC)
1590 if( cipher_info->mode == MBEDTLS_MODE_CBC )
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_enc,
1593 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +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 );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001597 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1600 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001603 return( ret );
1604 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001605 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001607
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001608 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001609
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001610 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001612 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001615
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001616 mbedtls_platform_memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1617 mbedtls_platform_memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001618
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001619 if( deflateInit( &transform->ctx_deflate,
1620 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001621 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1624 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001625 }
1626 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001628
Paul Bakker5121ce52009-01-03 21:22:43 +00001629 return( 0 );
1630}
1631
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001632#if defined(MBEDTLS_SSL_PROTO_SSL3)
1633static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1634 unsigned char hash[36],
1635 size_t *hlen )
1636{
1637 mbedtls_md5_context md5;
1638 mbedtls_sha1_context sha1;
1639 unsigned char pad_1[48];
1640 unsigned char pad_2[48];
1641
1642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1643
1644 mbedtls_md5_init( &md5 );
1645 mbedtls_sha1_init( &sha1 );
1646
1647 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1648 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1649
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001650 mbedtls_platform_memset( pad_1, 0x36, 48 );
1651 mbedtls_platform_memset( pad_2, 0x5C, 48 );
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001652
1653 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1654 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1655 mbedtls_md5_finish_ret( &md5, hash );
1656
1657 mbedtls_md5_starts_ret( &md5 );
1658 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1659 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1660 mbedtls_md5_update_ret( &md5, hash, 16 );
1661 mbedtls_md5_finish_ret( &md5, hash );
1662
1663 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1664 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1665 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1666
1667 mbedtls_sha1_starts_ret( &sha1 );
1668 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1669 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1670 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1671 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1672
1673 *hlen = 36;
1674
1675 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1677
1678 mbedtls_md5_free( &md5 );
1679 mbedtls_sha1_free( &sha1 );
1680
1681 return;
1682}
1683#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1684
1685#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1686static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1687 unsigned char hash[36],
1688 size_t *hlen )
1689{
1690 mbedtls_md5_context md5;
1691 mbedtls_sha1_context sha1;
1692
1693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1694
1695 mbedtls_md5_init( &md5 );
1696 mbedtls_sha1_init( &sha1 );
1697
1698 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1699 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1700
1701 mbedtls_md5_finish_ret( &md5, hash );
1702 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1703
1704 *hlen = 36;
1705
1706 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1708
1709 mbedtls_md5_free( &md5 );
1710 mbedtls_sha1_free( &sha1 );
1711
1712 return;
1713}
1714#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1715
1716#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1717#if defined(MBEDTLS_SHA256_C)
1718static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1719 unsigned char hash[32],
1720 size_t *hlen )
1721{
1722 mbedtls_sha256_context sha256;
1723
1724 mbedtls_sha256_init( &sha256 );
1725
1726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1727
1728 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1729 mbedtls_sha256_finish_ret( &sha256, hash );
1730
1731 *hlen = 32;
1732
1733 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1735
1736 mbedtls_sha256_free( &sha256 );
1737
1738 return;
1739}
1740#endif /* MBEDTLS_SHA256_C */
1741
1742#if defined(MBEDTLS_SHA512_C)
1743static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1744 unsigned char hash[48],
1745 size_t *hlen )
1746{
1747 mbedtls_sha512_context sha512;
1748
1749 mbedtls_sha512_init( &sha512 );
1750
1751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1752
1753 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1754 mbedtls_sha512_finish_ret( &sha512, hash );
1755
1756 *hlen = 48;
1757
1758 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1760
1761 mbedtls_sha512_free( &sha512 );
1762
1763 return;
1764}
1765#endif /* MBEDTLS_SHA512_C */
1766#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1767
Hanno Becker2f41b242019-08-15 17:29:43 +01001768int mbedtls_ssl_calc_verify( int minor_ver,
1769 mbedtls_md_type_t hash,
1770 mbedtls_ssl_context const *ssl,
1771 unsigned char *dst,
1772 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001773{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001774#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1775 (void) hash;
1776#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001777
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001778#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001779 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001780 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001781 else
1782#endif
1783#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001784 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker2f41b242019-08-15 17:29:43 +01001785 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001786 else
1787#endif
1788#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1789#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001790 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1791 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001792 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001793 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001794 }
1795 else
1796#endif
1797#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001798 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001799 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001800 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001801 }
1802 else
1803#endif
1804#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1805 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001806 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1807 }
1808
1809 return( 0 );
1810}
1811
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001812/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001813 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001814 *
1815 * Parameters:
1816 * [in/out] handshake
1817 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1818 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001819 * [out] master
1820 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001821 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001822static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001823 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001824 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001825{
1826 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001827
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001828/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1829/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1830/* (void) ssl; */
1831/* #endif */
1832
1833 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1834 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001835
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001836#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02001837 if( handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001838 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001839 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1840 return( 0 );
1841 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001842#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001843
1844 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1845 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001846
1847#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001848 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1849 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001850 {
1851 unsigned char session_hash[48];
1852 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001853
Hanno Becker2f41b242019-08-15 17:29:43 +01001854 mbedtls_ssl_calc_verify(
1855 mbedtls_ssl_get_minor_ver( ssl ),
1856 mbedtls_ssl_suite_get_mac( ciphersuite ),
1857 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001858
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001859 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1860 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001861
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001862 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1863 mbedtls_ssl_suite_get_mac( ciphersuite ),
1864 handshake->premaster, handshake->pmslen,
1865 "extended master secret",
1866 session_hash, hash_len,
1867 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001868 }
1869 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001870#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001871 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001872 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1873 mbedtls_ssl_suite_get_mac( ciphersuite ),
1874 handshake->premaster, handshake->pmslen,
1875 "master secret",
1876 handshake->randbytes, 64,
1877 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001878 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001879 if( ret != 0 )
1880 {
1881 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1882 return( ret );
1883 }
1884
1885 mbedtls_platform_zeroize( handshake->premaster,
1886 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001887
1888 return( 0 );
1889}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001890
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001891int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1892{
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02001893 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001894
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001896 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_UNSET;
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001897 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001898 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001899 ssl->session_negotiate->master,
1900 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001901 if( ret != 0 )
1902 {
1903 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1904 return( ret );
1905 }
1906
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001907 /* Swap the client and server random values:
1908 * - MS derivation wanted client+server (RFC 5246 8.1)
1909 * - key derivation wants server+client (RFC 5246 6.3) */
1910 {
1911 unsigned char tmp[64];
Teppo Järvelin91d79382019-10-02 09:09:31 +03001912 mbedtls_platform_memcpy( tmp, ssl->handshake->randbytes, 64 );
1913 mbedtls_platform_memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1914 mbedtls_platform_memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001915 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1916 }
1917
1918 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001919 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001920 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1921 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001922#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001923#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001924 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001925#endif
1926#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001927 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001928#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001929#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001930#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001931 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001932#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001933 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001934 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001935 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1936 ssl );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001937 if( ret == 0 )
1938 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02001939 mbedtls_platform_random_delay();
Jarno Lamsa4031a452019-12-19 08:11:12 +02001940 if( ret == 0 )
1941 {
1942 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_SET;
1943 }
1944 else
1945 {
1946 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
1947 }
1948 }
1949 else
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001950 {
1951 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1952 return( ret );
1953 }
1954
1955 /* We no longer need Server/ClientHello.random values */
1956 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1957 sizeof( ssl->handshake->randbytes ) );
1958
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001959 /* Allocate compression buffer */
1960#if defined(MBEDTLS_ZLIB_SUPPORT)
jiblime92af9a92019-12-18 21:40:01 -08001961 if( ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001962 ssl->compress_buf == NULL )
1963 {
1964 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1965 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1966 if( ssl->compress_buf == NULL )
1967 {
1968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001969 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001970 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1971 }
1972 }
1973#endif
1974
Paul Bakker5121ce52009-01-03 21:22:43 +00001975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1976
1977 return( 0 );
1978}
1979
Hanno Becker09d23642019-07-22 17:18:18 +01001980int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1981{
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02001982 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker09d23642019-07-22 17:18:18 +01001983
1984 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1985 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Beckera3c2c172019-07-23 16:51:57 +01001986#if defined(MBEDTLS_USE_TINYCRYPT)
1987 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001988 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Hanno Beckera3c2c172019-07-23 16:51:57 +01001989 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001990 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1991 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1992 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1993 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1994 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001995 {
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01001996 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1997 ssl->handshake->ecdh_privkey,
1998 ssl->handshake->premaster );
1999 if( ret == UECC_FAULT_DETECTED )
2000 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2001 if( ret != UECC_SUCCESS )
Hanno Beckera3c2c172019-07-23 16:51:57 +01002002 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002003 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Hanno Beckera3c2c172019-07-23 16:51:57 +01002004
2005 ssl->handshake->pmslen = NUM_ECC_BYTES;
2006 }
2007 else
2008#endif
Hanno Becker09d23642019-07-22 17:18:18 +01002009#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2010 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2011 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
2012 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002013 ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002014 ssl->handshake->premaster,
2015 MBEDTLS_PREMASTER_SIZE,
2016 &ssl->handshake->pmslen,
2017 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002018 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2019 if( ret == 0 )
2020 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002021 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002022 if( ret == 0 )
2023 {
2024 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2025 }
2026 else
2027 {
2028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2029 return( ret );
2030 }
2031 }
2032 else
Hanno Becker09d23642019-07-22 17:18:18 +01002033 {
2034 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2035 return( ret );
2036 }
2037
2038 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2039 }
2040 else
2041#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01002042#if defined(MBEDTLS_ECDH_C) && \
2043 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2044 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2045 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2046 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002047 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2048 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2049 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2050 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2051 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2052 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2053 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2054 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2055 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002056 ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002057 &ssl->handshake->pmslen,
2058 ssl->handshake->premaster,
2059 MBEDTLS_MPI_MAX_SIZE,
2060 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002061 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2062 if( ret == 0 )
2063 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002064 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002065 if( ret == 0 )
2066 {
2067 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2068 }
2069 else
2070 {
2071 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002072 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002073 }
2074 }
2075 else
Hanno Becker09d23642019-07-22 17:18:18 +01002076 {
2077 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2078 return( ret );
2079 }
2080
2081 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2082 }
2083 else
2084#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2085 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2086 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2087 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2088#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2089 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2090 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002091 ret = mbedtls_ssl_psk_derive_premaster( ssl,
2092 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) );
2093 if( ret == 0 )
2094 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002095 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002096 if( ret == 0 )
2097 {
2098 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2099 }
2100 else
2101 {
2102 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002103 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002104 }
2105 }
2106 else
Hanno Becker09d23642019-07-22 17:18:18 +01002107 {
2108 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2109 return( ret );
2110 }
2111 }
2112 else
2113#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2114#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2115 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2116 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2117 {
2118 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2119 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2120 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002121 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002122 if( ret == 0 )
2123 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002124 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002125 if( ret == 0 )
2126 {
2127 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2128 }
2129 else
2130 {
2131 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002132 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002133 }
2134 }
2135 else
Hanno Becker09d23642019-07-22 17:18:18 +01002136 {
2137 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2138 return( ret );
2139 }
2140 }
2141 else
2142#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2143#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2144 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2145 == MBEDTLS_KEY_EXCHANGE_RSA )
2146 {
2147 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002148 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002149 * ssl_rsa_generate_partial_pms(). Only the
2150 * PMS length needs to be set. */
2151 ssl->handshake->pmslen = 48;
2152 }
2153 else
2154#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2155 {
2156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2157 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2158 }
2159
2160 return( 0 );
2161}
2162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2164int 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 +02002165{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002166 unsigned char *p = ssl->handshake->premaster;
2167 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002168 const unsigned char *psk = ssl->conf->psk;
2169 size_t psk_len = ssl->conf->psk_len;
2170
2171 /* If the psk callback was called, use its result */
2172 if( ssl->handshake->psk != NULL )
2173 {
2174 psk = ssl->handshake->psk;
2175 psk_len = ssl->handshake->psk_len;
2176 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002177
2178 /*
2179 * PMS = struct {
2180 * opaque other_secret<0..2^16-1>;
2181 * opaque psk<0..2^16-1>;
2182 * };
2183 * with "other_secret" depending on the particular key exchange
2184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2186 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002187 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002188 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002190
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002191 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002192
2193 if( end < p || (size_t)( end - p ) < psk_len )
2194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2195
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002196 mbedtls_platform_memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002197 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002198 }
2199 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2201#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2202 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002203 {
2204 /*
2205 * other_secret already set by the ClientKeyExchange message,
2206 * and is 48 bytes long
2207 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002208 if( end - p < 2 )
2209 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2210
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002211 *p++ = 0;
2212 *p++ = 48;
2213 p += 48;
2214 }
2215 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2217#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2218 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002219 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002220 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002221 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002222
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002223 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002225 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002226 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002227 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002230 return( ret );
2231 }
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002232 p = mbedtls_platform_put_uint16_be( p, len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002233 p += len;
2234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002236 }
2237 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2239#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2240 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002241 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002242 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002243 size_t zlen;
2244
Hanno Becker982da7e2019-09-02 09:47:39 +01002245#if defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01002246 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2247 ssl->handshake->ecdh_privkey,
2248 p + 2 );
2249 if( ret == UECC_FAULT_DETECTED )
2250 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2251 if( ret != UECC_SUCCESS )
Hanno Becker982da7e2019-09-02 09:47:39 +01002252 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Hanno Becker982da7e2019-09-02 09:47:39 +01002253
2254 zlen = NUM_ECC_BYTES;
2255#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002257 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002258 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002259 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002262 return( ret );
2263 }
2264
Hanno Becker982da7e2019-09-02 09:47:39 +01002265 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2266 MBEDTLS_DEBUG_ECDH_Z );
2267#endif /* MBEDTLS_USE_TINYCRYPT */
2268
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002269 p = mbedtls_platform_put_uint16_be( p, zlen );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002270 p += zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002271 }
2272 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2276 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002277 }
2278
2279 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002280 if( end - p < 2 )
2281 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002282
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002283 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002284
2285 if( end < p || (size_t)( end - p ) < psk_len )
2286 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2287
Teppo Järvelin91d79382019-10-02 09:09:31 +03002288 mbedtls_platform_memcpy( p, psk, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002289 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002290
2291 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2292
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002293 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2294
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002295 return( 0 );
2296}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002300/*
2301 * SSLv3.0 MAC functions
2302 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002303#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002304static void ssl_mac( mbedtls_md_context_t *md_ctx,
2305 const unsigned char *secret,
2306 const unsigned char *buf, size_t len,
2307 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002308 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002309{
2310 unsigned char header[11];
2311 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002312 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2314 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002315
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002316 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002318 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002319 else
Paul Bakker68884e32013-01-07 18:20:04 +01002320 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002321
Teppo Järvelin91d79382019-10-02 09:09:31 +03002322 mbedtls_platform_memcpy( header, ctr, 8 );
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03002323 header[8] = (unsigned char) type;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002324 (void)mbedtls_platform_put_uint16_be( &header[9], len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002325
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002326 mbedtls_platform_memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 mbedtls_md_starts( md_ctx );
2328 mbedtls_md_update( md_ctx, secret, md_size );
2329 mbedtls_md_update( md_ctx, padding, padlen );
2330 mbedtls_md_update( md_ctx, header, 11 );
2331 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002332 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002333
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002334 mbedtls_platform_memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 mbedtls_md_starts( md_ctx );
2336 mbedtls_md_update( md_ctx, secret, md_size );
2337 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002338 mbedtls_md_update( md_ctx, out, md_size );
2339 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002340}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002342
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002343/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002344 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002345#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002346 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2347 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2348 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2349/* This function makes sure every byte in the memory region is accessed
2350 * (in ascending addresses order) */
2351static void ssl_read_memory( unsigned char *p, size_t len )
2352{
2353 unsigned char acc = 0;
2354 volatile unsigned char force;
2355
2356 for( ; len != 0; p++, len-- )
2357 acc ^= *p;
2358
2359 force = acc;
2360 (void) force;
2361}
2362#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2363
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002364/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002365 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002366 */
Hanno Becker3307b532017-12-27 21:37:21 +00002367
Hanno Beckera5a2b082019-05-15 14:03:01 +01002368#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002369/* This functions transforms a DTLS plaintext fragment and a record content
2370 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002371 *
2372 * struct {
2373 * opaque content[DTLSPlaintext.length];
2374 * ContentType real_type;
2375 * uint8 zeros[length_of_padding];
2376 * } DTLSInnerPlaintext;
2377 *
2378 * Input:
2379 * - `content`: The beginning of the buffer holding the
2380 * plaintext to be wrapped.
2381 * - `*content_size`: The length of the plaintext in Bytes.
2382 * - `max_len`: The number of Bytes available starting from
2383 * `content`. This must be `>= *content_size`.
2384 * - `rec_type`: The desired record content type.
2385 *
2386 * Output:
2387 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2388 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2389 *
2390 * Returns:
2391 * - `0` on success.
2392 * - A negative error code if `max_len` didn't offer enough space
2393 * for the expansion.
2394 */
2395static int ssl_cid_build_inner_plaintext( unsigned char *content,
2396 size_t *content_size,
2397 size_t remaining,
2398 uint8_t rec_type )
2399{
2400 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002401 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2402 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2403 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002404
2405 /* Write real content type */
2406 if( remaining == 0 )
2407 return( -1 );
2408 content[ len ] = rec_type;
2409 len++;
2410 remaining--;
2411
2412 if( remaining < pad )
2413 return( -1 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002414 mbedtls_platform_memset( content + len, 0, pad );
Hanno Becker92c930f2019-04-29 17:31:37 +01002415 len += pad;
2416 remaining -= pad;
2417
2418 *content_size = len;
2419 return( 0 );
2420}
2421
Hanno Becker7dc25772019-05-20 15:08:01 +01002422/* This function parses a DTLSInnerPlaintext structure.
2423 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002424static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2425 size_t *content_size,
2426 uint8_t *rec_type )
2427{
2428 size_t remaining = *content_size;
2429
2430 /* Determine length of padding by skipping zeroes from the back. */
2431 do
2432 {
2433 if( remaining == 0 )
2434 return( -1 );
2435 remaining--;
2436 } while( content[ remaining ] == 0 );
2437
2438 *content_size = remaining;
2439 *rec_type = content[ remaining ];
2440
2441 return( 0 );
2442}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002443#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002444
Hanno Becker99abf512019-05-20 14:50:53 +01002445/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002446 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002447static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002448 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002449 mbedtls_record *rec )
2450{
Hanno Becker99abf512019-05-20 14:50:53 +01002451 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002452 *
2453 * additional_data = seq_num + TLSCompressed.type +
2454 * TLSCompressed.version + TLSCompressed.length;
2455 *
Hanno Becker99abf512019-05-20 14:50:53 +01002456 * For the CID extension, this is extended as follows
2457 * (quoting draft-ietf-tls-dtls-connection-id-05,
2458 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002459 *
2460 * additional_data = seq_num + DTLSPlaintext.type +
2461 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002462 * cid +
2463 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002464 * length_of_DTLSInnerPlaintext;
2465 */
2466
Teppo Järvelin91d79382019-10-02 09:09:31 +03002467 mbedtls_platform_memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002468 add_data[8] = rec->type;
Teppo Järvelin91d79382019-10-02 09:09:31 +03002469 mbedtls_platform_memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002470
Hanno Beckera5a2b082019-05-15 14:03:01 +01002471#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002472 if( rec->cid_len != 0 )
2473 {
Teppo Järvelin91d79382019-10-02 09:09:31 +03002474 mbedtls_platform_memcpy( add_data + 11, rec->cid, rec->cid_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002475 add_data[11 + rec->cid_len + 0] = rec->cid_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002476 (void)mbedtls_platform_put_uint16_be( &add_data[11 + rec->cid_len + 1],
2477 rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002478 *add_data_len = 13 + 1 + rec->cid_len;
2479 }
2480 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002481#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002482 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002483 (void)mbedtls_platform_put_uint16_be( &add_data[11], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002484 *add_data_len = 13;
2485 }
Hanno Becker3307b532017-12-27 21:37:21 +00002486}
2487
Hanno Becker611a83b2018-01-03 14:27:32 +00002488int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2489 mbedtls_ssl_transform *transform,
2490 mbedtls_record *rec,
2491 int (*f_rng)(void *, unsigned char *, size_t),
2492 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002493{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002495 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002496 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002497 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002498 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002499 size_t post_avail;
2500
2501 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002502#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002503 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002504 ((void) ssl);
2505#endif
2506
2507 /* The PRNG is used for dynamic IV generation that's used
2508 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2509#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2510 ( defined(MBEDTLS_AES_C) || \
2511 defined(MBEDTLS_ARIA_C) || \
2512 defined(MBEDTLS_CAMELLIA_C) ) && \
2513 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2514 ((void) f_rng);
2515 ((void) p_rng);
2516#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Hanno Becker3307b532017-12-27 21:37:21 +00002520 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002521 {
Hanno Becker3307b532017-12-27 21:37:21 +00002522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2523 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2524 }
Hanno Becker505089d2019-05-01 09:45:57 +01002525 if( rec == NULL
2526 || rec->buf == NULL
2527 || rec->buf_len < rec->data_offset
2528 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002529#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002530 || rec->cid_len != 0
2531#endif
2532 )
Hanno Becker3307b532017-12-27 21:37:21 +00002533 {
2534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002536 }
2537
Hanno Becker3307b532017-12-27 21:37:21 +00002538 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002539 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002541 data, rec->data_len );
2542
2543 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2544
2545 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2546 {
2547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2548 (unsigned) rec->data_len,
2549 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2551 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002552
Hanno Beckera5a2b082019-05-15 14:03:01 +01002553#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002554 /*
2555 * Add CID information
2556 */
2557 rec->cid_len = transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03002558 /* Not using more secure mbedtls_platform_memcpy as cid is public */
2559 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
Hanno Beckere83efe62019-04-29 13:52:53 +01002560 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002561
2562 if( rec->cid_len != 0 )
2563 {
2564 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002565 * Wrap plaintext into DTLSInnerPlaintext structure.
2566 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002567 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002568 * Note that this changes `rec->data_len`, and hence
2569 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002570 */
2571 if( ssl_cid_build_inner_plaintext( data,
2572 &rec->data_len,
2573 post_avail,
2574 rec->type ) != 0 )
2575 {
2576 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2577 }
2578
2579 rec->type = MBEDTLS_SSL_MSG_CID;
2580 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002581#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002582
Hanno Becker92c930f2019-04-29 17:31:37 +01002583 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2584
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002586 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002588#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589 if( mode == MBEDTLS_MODE_STREAM ||
2590 ( mode == MBEDTLS_MODE_CBC
2591#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002592 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002593#endif
2594 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 {
Hanno Becker3307b532017-12-27 21:37:21 +00002596 if( post_avail < transform->maclen )
2597 {
2598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2599 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2600 }
2601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002603 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2604 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002605 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002606 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002607 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2608 data, rec->data_len, rec->ctr, rec->type, mac );
Teppo Järvelin91d79382019-10-02 09:09:31 +03002609 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002610 }
2611 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002612#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2614 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002615 if( mbedtls_ssl_ver_geq(
2616 mbedtls_ssl_transform_get_minor_ver( transform ),
2617 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002618 {
Hanno Becker992b6872017-11-09 18:57:39 +00002619 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2620
Hanno Beckere83efe62019-04-29 13:52:53 +01002621 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002622
Hanno Becker3307b532017-12-27 21:37:21 +00002623 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002624 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002625 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2626 data, rec->data_len );
2627 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2628 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2629
Teppo Järvelin91d79382019-10-02 09:09:31 +03002630 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002631 }
2632 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002633#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2636 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002637 }
2638
Hanno Becker3307b532017-12-27 21:37:21 +00002639 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2640 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002641
Hanno Becker3307b532017-12-27 21:37:21 +00002642 rec->data_len += transform->maclen;
2643 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002644 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002645 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002646#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002647
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002648 /*
2649 * Encrypt
2650 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2652 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002654 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002655 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002657 "including %d bytes of padding",
2658 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
Hanno Becker3307b532017-12-27 21:37:21 +00002660 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2661 transform->iv_enc, transform->ivlen,
2662 data, rec->data_len,
2663 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002666 return( ret );
2667 }
2668
Hanno Becker3307b532017-12-27 21:37:21 +00002669 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2672 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002673 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 }
Paul Bakker68884e32013-01-07 18:20:04 +01002675 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002677
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002678#if defined(MBEDTLS_GCM_C) || \
2679 defined(MBEDTLS_CCM_C) || \
2680 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002682 mode == MBEDTLS_MODE_CCM ||
2683 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002684 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002685 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002686 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002687 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002688
Hanno Becker3307b532017-12-27 21:37:21 +00002689 /* Check that there's space for both the authentication tag
2690 * and the explicit IV before and after the record content. */
2691 if( post_avail < transform->taglen ||
2692 rec->data_offset < explicit_iv_len )
2693 {
2694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2695 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2696 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002697
Paul Bakker68884e32013-01-07 18:20:04 +01002698 /*
2699 * Generate IV
2700 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002701 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2702 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002703 /* GCM and CCM: fixed || explicit (=seqnum) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002704 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2705 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, rec->ctr,
Hanno Becker3307b532017-12-27 21:37:21 +00002706 explicit_iv_len );
2707 /* Prefix record content with explicit IV. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002708 mbedtls_platform_memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002709 }
2710 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2711 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002712 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002713 unsigned char i;
2714
Teppo Järvelin91d79382019-10-02 09:09:31 +03002715 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002716
2717 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002718 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002719 }
2720 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002721 {
2722 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2724 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002725 }
2726
Hanno Beckere83efe62019-04-29 13:52:53 +01002727 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002728
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002729 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2730 iv, transform->ivlen );
2731 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002732 data - explicit_iv_len, explicit_iv_len );
2733 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002734 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002736 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002737 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002738
Paul Bakker68884e32013-01-07 18:20:04 +01002739 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002740 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002741 */
Hanno Becker3307b532017-12-27 21:37:21 +00002742
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002743 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002744 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002745 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002746 data, rec->data_len, /* source */
2747 data, &rec->data_len, /* destination */
2748 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002751 return( ret );
2752 }
2753
Hanno Becker3307b532017-12-27 21:37:21 +00002754 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2755 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002756
Hanno Becker3307b532017-12-27 21:37:21 +00002757 rec->data_len += transform->taglen + explicit_iv_len;
2758 rec->data_offset -= explicit_iv_len;
2759 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002760 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002761 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002762 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2764#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002765 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002767 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002768 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002769 size_t padlen, i;
2770 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002771
Hanno Becker3307b532017-12-27 21:37:21 +00002772 /* Currently we're always using minimal padding
2773 * (up to 255 bytes would be allowed). */
2774 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2775 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002776 padlen = 0;
2777
Hanno Becker3307b532017-12-27 21:37:21 +00002778 /* Check there's enough space in the buffer for the padding. */
2779 if( post_avail < padlen + 1 )
2780 {
2781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2782 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2783 }
2784
Paul Bakker5121ce52009-01-03 21:22:43 +00002785 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002786 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002787
Hanno Becker3307b532017-12-27 21:37:21 +00002788 rec->data_len += padlen + 1;
2789 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002792 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002793 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2794 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002795 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002796 if( mbedtls_ssl_ver_geq(
2797 mbedtls_ssl_transform_get_minor_ver( transform ),
2798 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002799 {
Hanno Becker3307b532017-12-27 21:37:21 +00002800 if( f_rng == NULL )
2801 {
2802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2803 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2804 }
2805
2806 if( rec->data_offset < transform->ivlen )
2807 {
2808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2809 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2810 }
2811
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002812 /*
2813 * Generate IV
2814 */
Hanno Becker3307b532017-12-27 21:37:21 +00002815 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002816 if( ret != 0 )
2817 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002818
Teppo Järvelin91d79382019-10-02 09:09:31 +03002819 mbedtls_platform_memcpy( data - transform->ivlen, transform->iv_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002820 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002821
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002822 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002826 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002827 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002828 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002829
Hanno Becker3307b532017-12-27 21:37:21 +00002830 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2831 transform->iv_enc,
2832 transform->ivlen,
2833 data, rec->data_len,
2834 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002837 return( ret );
2838 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002839
Hanno Becker3307b532017-12-27 21:37:21 +00002840 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2843 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002844 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002847 if( mbedtls_ssl_ver_lt(
2848 mbedtls_ssl_transform_get_minor_ver( transform ),
2849 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02002850 {
2851 /*
2852 * Save IV in SSL3 and TLS1
2853 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002854 mbedtls_platform_memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
Hanno Becker3307b532017-12-27 21:37:21 +00002855 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002856 }
Hanno Becker3307b532017-12-27 21:37:21 +00002857 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002858#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002859 {
2860 data -= transform->ivlen;
2861 rec->data_offset -= transform->ivlen;
2862 rec->data_len += transform->ivlen;
2863 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002866 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002867 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002868 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2869
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002870 /*
2871 * MAC(MAC_write_key, seq_num +
2872 * TLSCipherText.type +
2873 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002874 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002875 * IV + // except for TLS 1.0
2876 * ENC(content + padding + padding_length));
2877 */
Hanno Becker3307b532017-12-27 21:37:21 +00002878
2879 if( post_avail < transform->maclen)
2880 {
2881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2882 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2883 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002884
Hanno Beckere83efe62019-04-29 13:52:53 +01002885 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002888 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002889 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002890
Hanno Becker3307b532017-12-27 21:37:21 +00002891 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002892 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002893 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2894 data, rec->data_len );
2895 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2896 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002897
Teppo Järvelin91d79382019-10-02 09:09:31 +03002898 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002899
Hanno Becker3307b532017-12-27 21:37:21 +00002900 rec->data_len += transform->maclen;
2901 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002902 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002903 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002905 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002906 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002908 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002909 {
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é-Gonnardf7dc3782013-09-13 14:10:44 +02002912 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002913
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002914 /* Make extra sure authentication was performed, exactly once */
2915 if( auth_done != 1 )
2916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2918 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002919 }
2920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002922
2923 return( 0 );
2924}
2925
Hanno Becker40478be2019-07-12 08:23:59 +01002926int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002927 mbedtls_ssl_transform *transform,
2928 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002929{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002930 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002932 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002933#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002934 size_t padlen = 0, correct = 1;
2935#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002936 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002937 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002938 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002939
Hanno Becker611a83b2018-01-03 14:27:32 +00002940#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002941 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002942 ((void) ssl);
2943#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002946 if( rec == NULL ||
2947 rec->buf == NULL ||
2948 rec->buf_len < rec->data_offset ||
2949 rec->buf_len - rec->data_offset < rec->data_len )
2950 {
2951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002953 }
2954
Hanno Becker4c6876b2017-12-27 21:28:58 +00002955 data = rec->buf + rec->data_offset;
2956 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002957
Hanno Beckera5a2b082019-05-15 14:03:01 +01002958#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002959 /*
2960 * Match record's CID with incoming CID.
2961 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002962 if( rec->cid_len != transform->in_cid_len ||
Teppo Järvelin0efac532019-10-04 13:21:08 +03002963 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) // use regular memcmp as CID is public
Hanno Beckerabd7c892019-05-08 13:02:22 +01002964 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002965 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002966 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002967#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2970 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002971 {
2972 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002973 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2974 transform->iv_dec,
2975 transform->ivlen,
2976 data, rec->data_len,
2977 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002980 return( ret );
2981 }
2982
Hanno Becker4c6876b2017-12-27 21:28:58 +00002983 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2986 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002987 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002988 }
Paul Bakker68884e32013-01-07 18:20:04 +01002989 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002991#if defined(MBEDTLS_GCM_C) || \
2992 defined(MBEDTLS_CCM_C) || \
2993 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002995 mode == MBEDTLS_MODE_CCM ||
2996 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002997 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002998 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002999 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003000
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003001 /*
3002 * Prepare IV from explicit and implicit data.
3003 */
3004
3005 /* Check that there's enough space for the explicit IV
3006 * (at the beginning of the record) and the MAC (at the
3007 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003008 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003011 "+ taglen (%d)", rec->data_len,
3012 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003014 }
Paul Bakker68884e32013-01-07 18:20:04 +01003015
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003016#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003017 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
3018 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003019 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01003020
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003021 /* Fixed */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003022 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003023 /* Explicit */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003024 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003025 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003026 else
3027#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3028#if defined(MBEDTLS_CHACHAPOLY_C)
3029 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003030 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02003031 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003032 unsigned char i;
3033
Teppo Järvelin91d79382019-10-02 09:09:31 +03003034 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003035
3036 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003037 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003038 }
3039 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003040#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003041 {
3042 /* Reminder if we ever add an AEAD mode with a different size */
3043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3044 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3045 }
3046
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003047 /* Group changes to data, data_len, and add_data, because
3048 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003049 data += explicit_iv_len;
3050 rec->data_offset += explicit_iv_len;
3051 rec->data_len -= explicit_iv_len + transform->taglen;
3052
Hanno Beckere83efe62019-04-29 13:52:53 +01003053 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003054 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01003055 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003056
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003057 /* Because of the check above, we know that there are
3058 * explicit_iv_len Bytes preceeding data, and taglen
3059 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01003060 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003061 * mbedtls_cipher_auth_decrypt() below. */
3062
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003063 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003064 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00003065 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01003066
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003067 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003068 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003069 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003070 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
3071 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01003072 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003073 data, rec->data_len,
3074 data, &olen,
3075 data + rec->data_len,
3076 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
3081 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003082
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003083 return( ret );
3084 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003085 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003086
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003087 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003088 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3091 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003092 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003093 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003094 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3096#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003097 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003099 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003100 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003101
Paul Bakker5121ce52009-01-03 21:22:43 +00003102 /*
Paul Bakker45829992013-01-03 14:52:21 +01003103 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003106 if( mbedtls_ssl_ver_geq(
3107 mbedtls_ssl_transform_get_minor_ver( transform ),
3108 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003109 {
3110 /* The ciphertext is prefixed with the CBC IV. */
3111 minlen += transform->ivlen;
3112 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003113#endif
Paul Bakker45829992013-01-03 14:52:21 +01003114
Hanno Becker4c6876b2017-12-27 21:28:58 +00003115 /* Size considerations:
3116 *
3117 * - The CBC cipher text must not be empty and hence
3118 * at least of size transform->ivlen.
3119 *
3120 * Together with the potential IV-prefix, this explains
3121 * the first of the two checks below.
3122 *
3123 * - The record must contain a MAC, either in plain or
3124 * encrypted, depending on whether Encrypt-then-MAC
3125 * is used or not.
3126 * - If it is, the message contains the IV-prefix,
3127 * the CBC ciphertext, and the MAC.
3128 * - If it is not, the padded plaintext, and hence
3129 * the CBC ciphertext, has at least length maclen + 1
3130 * because there is at least the padding length byte.
3131 *
3132 * As the CBC ciphertext is not empty, both cases give the
3133 * lower bound minlen + maclen + 1 on the record size, which
3134 * we test for in the second check below.
3135 */
3136 if( rec->data_len < minlen + transform->ivlen ||
3137 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003140 "+ 1 ) ( + expl IV )", rec->data_len,
3141 transform->ivlen,
3142 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003144 }
3145
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003146 /*
3147 * Authenticate before decrypt if enabled
3148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003150 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003151 {
Hanno Becker992b6872017-11-09 18:57:39 +00003152 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003155
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003156 /* Update data_len in tandem with add_data.
3157 *
3158 * The subtraction is safe because of the previous check
3159 * data_len >= minlen + maclen + 1.
3160 *
3161 * Afterwards, we know that data + data_len is followed by at
3162 * least maclen Bytes, which justifies the call to
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003163 * mbedtls_platform_memcmp() below.
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003164 *
3165 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003166 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003167 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003168
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003169 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003170 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3171 add_data_len );
3172 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3173 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003174 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3175 data, rec->data_len );
3176 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3177 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003178
Hanno Becker4c6876b2017-12-27 21:28:58 +00003179 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3180 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003181 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003182 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003183
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003184 /* Compare expected MAC with MAC at the end of the record. */
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003185 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003186 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003190 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003191 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003192 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003194
3195 /*
3196 * Check length sanity
3197 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003198
3199 /* We know from above that data_len > minlen >= 0,
3200 * so the following check in particular implies that
3201 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003202 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003205 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003207 }
3208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003210 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003211 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003212 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003213 if( mbedtls_ssl_ver_geq(
3214 mbedtls_ssl_transform_get_minor_ver( transform ),
3215 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003216 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003217 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003218 mbedtls_platform_memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003219
Hanno Becker4c6876b2017-12-27 21:28:58 +00003220 data += transform->ivlen;
3221 rec->data_offset += transform->ivlen;
3222 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003223 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003225
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003226 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3227
Hanno Becker4c6876b2017-12-27 21:28:58 +00003228 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3229 transform->iv_dec, transform->ivlen,
3230 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003233 return( ret );
3234 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003235
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003236 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003237 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3240 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003241 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003244 if( mbedtls_ssl_ver_lt(
3245 mbedtls_ssl_transform_get_minor_ver( transform ),
3246 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02003247 {
3248 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003249 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3250 * records is equivalent to CBC decryption of the concatenation
3251 * of the records; in other words, IVs are maintained across
3252 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003253 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003254 mbedtls_platform_memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003255 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003256 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003257#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003258
Hanno Becker4c6876b2017-12-27 21:28:58 +00003259 /* Safe since data_len >= minlen + maclen + 1, so after having
3260 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003261 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3262 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003263 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003264
Hanno Becker4c6876b2017-12-27 21:28:58 +00003265 if( auth_done == 1 )
3266 {
3267 correct *= ( rec->data_len >= padlen + 1 );
3268 padlen *= ( rec->data_len >= padlen + 1 );
3269 }
3270 else
Paul Bakker45829992013-01-03 14:52:21 +01003271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003273 if( rec->data_len < transform->maclen + padlen + 1 )
3274 {
3275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3276 rec->data_len,
3277 transform->maclen,
3278 padlen + 1 ) );
3279 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003280#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003281
3282 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3283 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003284 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003285
Hanno Becker4c6876b2017-12-27 21:28:58 +00003286 padlen++;
3287
3288 /* Regardless of the validity of the padding,
3289 * we have data_len >= padlen here. */
3290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003292 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3293 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003294 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003295 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297#if defined(MBEDTLS_SSL_DEBUG_ALL)
3298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003299 "should be no more than %d",
3300 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003301#endif
Paul Bakker45829992013-01-03 14:52:21 +01003302 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003303 }
3304 }
3305 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3307#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3308 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003309 if( mbedtls_ssl_ver_gt(
3310 mbedtls_ssl_transform_get_minor_ver( transform ),
3311 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003312 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003313 /* The padding check involves a series of up to 256
3314 * consecutive memory reads at the end of the record
3315 * plaintext buffer. In order to hide the length and
3316 * validity of the padding, always perform exactly
3317 * `min(256,plaintext_len)` reads (but take into account
3318 * only the last `padlen` bytes for the padding check). */
3319 size_t pad_count = 0;
3320 size_t real_count = 0;
3321 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003322
Hanno Becker4c6876b2017-12-27 21:28:58 +00003323 /* Index of first padding byte; it has been ensured above
3324 * that the subtraction is safe. */
3325 size_t const padding_idx = rec->data_len - padlen;
3326 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3327 size_t const start_idx = rec->data_len - num_checks;
3328 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003329
Hanno Becker4c6876b2017-12-27 21:28:58 +00003330 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003331 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003332 real_count |= ( idx >= padding_idx );
3333 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003334 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003335 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003338 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003340#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003341 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003342 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003343 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3345 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3348 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003349 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003350
Hanno Becker4c6876b2017-12-27 21:28:58 +00003351 /* If the padding was found to be invalid, padlen == 0
3352 * and the subtraction is safe. If the padding was found valid,
3353 * padlen hasn't been changed and the previous assertion
3354 * data_len >= padlen still holds. */
3355 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003356 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003357 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003359 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3362 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003363 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003364
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003365#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003366 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003367 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003368#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003369
3370 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003371 * Authenticate if not done yet.
3372 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003373 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003374#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003375 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003376 {
Hanno Becker992b6872017-11-09 18:57:39 +00003377 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003378
Hanno Becker4c6876b2017-12-27 21:28:58 +00003379 /* If the initial value of padlen was such that
3380 * data_len < maclen + padlen + 1, then padlen
3381 * got reset to 1, and the initial check
3382 * data_len >= minlen + maclen + 1
3383 * guarantees that at this point we still
3384 * have at least data_len >= maclen.
3385 *
3386 * If the initial value of padlen was such that
3387 * data_len >= maclen + padlen + 1, then we have
3388 * subtracted either padlen + 1 (if the padding was correct)
3389 * or 0 (if the padding was incorrect) since then,
3390 * hence data_len >= maclen in any case.
3391 */
3392 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003393 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003396 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3397 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003398 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003399 ssl_mac( &transform->md_ctx_dec,
3400 transform->mac_dec,
3401 data, rec->data_len,
3402 rec->ctr, rec->type,
3403 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003404 }
3405 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3407#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3408 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003409 if( mbedtls_ssl_ver_gt(
3410 mbedtls_ssl_transform_get_minor_ver( transform ),
3411 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003412 {
3413 /*
3414 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003415 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003416 *
3417 * Known timing attacks:
3418 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3419 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003420 * To compensate for different timings for the MAC calculation
3421 * depending on how much padding was removed (which is determined
3422 * by padlen), process extra_run more blocks through the hash
3423 * function.
3424 *
3425 * The formula in the paper is
3426 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3427 * where L1 is the size of the header plus the decrypted message
3428 * plus CBC padding and L2 is the size of the header plus the
3429 * decrypted message. This is for an underlying hash function
3430 * with 64-byte blocks.
3431 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3432 * correctly. We round down instead of up, so -56 is the correct
3433 * value for our calculations instead of -55.
3434 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003435 * Repeat the formula rather than defining a block_size variable.
3436 * This avoids requiring division by a variable at runtime
3437 * (which would be marginally less efficient and would require
3438 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003439 */
3440 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003441 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003442
3443 /*
3444 * The next two sizes are the minimum and maximum values of
3445 * in_msglen over all padlen values.
3446 *
3447 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003448 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003449 *
3450 * Note that max_len + maclen is never more than the buffer
3451 * length, as we previously did in_msglen -= maclen too.
3452 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003453 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003454 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3455
Hanno Becker4c6876b2017-12-27 21:28:58 +00003456 memset( tmp, 0, sizeof( tmp ) );
3457
Hanno Beckera5cedbc2019-07-17 11:21:02 +01003458 switch( mbedtls_md_get_type(
3459 mbedtls_md_get_handle( &transform->md_ctx_dec ) ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003460 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003461#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3462 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003463 case MBEDTLS_MD_MD5:
3464 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003465 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003466 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003467 extra_run =
3468 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3469 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003470 break;
3471#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003472#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003473 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003474 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003475 extra_run =
3476 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3477 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003478 break;
3479#endif
3480 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003482 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3483 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003484
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003485 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003486
Hanno Beckere83efe62019-04-29 13:52:53 +01003487 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3488 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003489 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3490 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003491 /* Make sure we access everything even when padlen > 0. This
3492 * makes the synchronisation requirements for just-in-time
3493 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003494 ssl_read_memory( data + rec->data_len, padlen );
3495 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003496
3497 /* Call mbedtls_md_process at least once due to cache attacks
3498 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003499 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003500 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003501
Hanno Becker4c6876b2017-12-27 21:28:58 +00003502 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003503
3504 /* Make sure we access all the memory that could contain the MAC,
3505 * before we check it in the next code block. This makes the
3506 * synchronisation requirements for just-in-time Prime+Probe
3507 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003508 ssl_read_memory( data + min_len,
3509 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003510 }
3511 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3513 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3516 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003517 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003518
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003519#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003520 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3521 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003522#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003523
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003524 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003525 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527#if defined(MBEDTLS_SSL_DEBUG_ALL)
3528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003529#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003530 correct = 0;
3531 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003532 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003533 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003534
3535 /*
3536 * Finally check the correct flag
3537 */
3538 if( correct == 0 )
3539 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003540#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003541
3542 /* Make extra sure authentication was performed, exactly once */
3543 if( auth_done != 1 )
3544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3546 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003547 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003548
Hanno Beckera5a2b082019-05-15 14:03:01 +01003549#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003550 if( rec->cid_len != 0 )
3551 {
3552 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3553 &rec->type );
3554 if( ret != 0 )
3555 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3556 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003557#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003560
3561 return( 0 );
3562}
3563
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003564#undef MAC_NONE
3565#undef MAC_PLAINTEXT
3566#undef MAC_CIPHERTEXT
3567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003569/*
3570 * Compression/decompression functions
3571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003573{
3574 int ret;
3575 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003576 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003577 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003578 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003581
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003582 if( len_pre == 0 )
3583 return( 0 );
3584
Teppo Järvelin91d79382019-10-02 09:09:31 +03003585 mbedtls_platform_memcpy( msg_pre, ssl->out_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003588 ssl->out_msglen ) );
3589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003591 ssl->out_msg, ssl->out_msglen );
3592
Paul Bakker48916f92012-09-16 19:57:18 +00003593 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3594 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3595 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003596 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003597
Paul Bakker48916f92012-09-16 19:57:18 +00003598 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003599 if( ret != Z_OK )
3600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3602 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003603 }
3604
Angus Grattond8213d02016-05-25 20:56:48 +10003605 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003606 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003609 ssl->out_msglen ) );
3610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003612 ssl->out_msg, ssl->out_msglen );
3613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003615
3616 return( 0 );
3617}
3618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003620{
3621 int ret;
3622 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003623 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003624 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003625 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003628
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003629 if( len_pre == 0 )
3630 return( 0 );
3631
Teppo Järvelin91d79382019-10-02 09:09:31 +03003632 mbedtls_platform_memcpy( msg_pre, ssl->in_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003634 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003635 ssl->in_msglen ) );
3636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003638 ssl->in_msg, ssl->in_msglen );
3639
Paul Bakker48916f92012-09-16 19:57:18 +00003640 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3641 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3642 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003643 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003644 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003645
Paul Bakker48916f92012-09-16 19:57:18 +00003646 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003647 if( ret != Z_OK )
3648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3650 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003651 }
3652
Angus Grattond8213d02016-05-25 20:56:48 +10003653 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003654 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003657 ssl->in_msglen ) );
3658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003660 ssl->in_msg, ssl->in_msglen );
3661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003663
3664 return( 0 );
3665}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3669static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671#if defined(MBEDTLS_SSL_PROTO_DTLS)
3672static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003673{
3674 /* If renegotiation is not enforced, retransmit until we would reach max
3675 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003676 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003677 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003678 uint32_t ratio =
3679 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3680 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003681 unsigned char doublings = 1;
3682
3683 while( ratio != 0 )
3684 {
3685 ++doublings;
3686 ratio >>= 1;
3687 }
3688
3689 if( ++ssl->renego_records_seen > doublings )
3690 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003692 return( 0 );
3693 }
3694 }
3695
3696 return( ssl_write_hello_request( ssl ) );
3697}
3698#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003699#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003700
Paul Bakker5121ce52009-01-03 21:22:43 +00003701/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003702 * Fill the input message buffer by appending data to it.
3703 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003704 *
3705 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3706 * available (from this read and/or a previous one). Otherwise, an error code
3707 * is returned (possibly EOF or WANT_READ).
3708 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003709 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3710 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3711 * since we always read a whole datagram at once.
3712 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003713 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003714 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003717{
Paul Bakker23986e52011-04-24 08:57:21 +00003718 int ret;
3719 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003722
Hanno Beckera58a8962019-06-13 16:11:15 +01003723 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3724 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003727 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003729 }
3730
Angus Grattond8213d02016-05-25 20:56:48 +10003731 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3734 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003735 }
3736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003738 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003739 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003740 uint32_t timeout;
3741
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003742 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003743 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3744 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003745 {
3746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3747 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3748 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3749 }
3750
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003751 /*
3752 * The point is, we need to always read a full datagram at once, so we
3753 * sometimes read more then requested, and handle the additional data.
3754 * It could be the rest of the current record (while fetching the
3755 * header) and/or some other records in the same datagram.
3756 */
3757
3758 /*
3759 * Move to the next record in the already read datagram if applicable
3760 */
3761 if( ssl->next_record_offset != 0 )
3762 {
3763 if( ssl->in_left < ssl->next_record_offset )
3764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3766 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003767 }
3768
3769 ssl->in_left -= ssl->next_record_offset;
3770
3771 if( ssl->in_left != 0 )
3772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003774 ssl->next_record_offset ) );
3775 memmove( ssl->in_hdr,
3776 ssl->in_hdr + ssl->next_record_offset,
3777 ssl->in_left );
3778 }
3779
3780 ssl->next_record_offset = 0;
3781 }
3782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003784 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003785
3786 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003787 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003788 */
3789 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003792 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003793 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003794
3795 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003796 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003797 * are not at the beginning of a new record, the caller did something
3798 * wrong.
3799 */
3800 if( ssl->in_left != 0 )
3801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3803 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003804 }
3805
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003806 /*
3807 * Don't even try to read if time's out already.
3808 * This avoids by-passing the timer when repeatedly receiving messages
3809 * that will end up being dropped.
3810 */
3811 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003812 {
3813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003814 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003815 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003816 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003817 {
Angus Grattond8213d02016-05-25 20:56:48 +10003818 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003820 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003821 timeout = ssl->handshake->retransmit_timeout;
3822 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003823 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003825 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003826
Hanno Beckera58a8962019-06-13 16:11:15 +01003827 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3828 {
3829 ret = mbedtls_ssl_get_recv_timeout( ssl )
3830 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3831 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003832 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003833 {
3834 ret = mbedtls_ssl_get_recv( ssl )
3835 ( ssl->p_bio, ssl->in_hdr, len );
3836 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003839
3840 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003842 }
3843
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003844 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003847 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003850 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003851 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003854 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003855 }
3856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003859 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003860 return( ret );
3861 }
3862
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003863 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003864 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003866 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3867 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003868 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003869 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003870 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003873 return( ret );
3874 }
3875
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003876 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003877 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003879 }
3880
Paul Bakker5121ce52009-01-03 21:22:43 +00003881 if( ret < 0 )
3882 return( ret );
3883
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003884 ssl->in_left = ret;
3885 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003886 MBEDTLS_SSL_TRANSPORT_ELSE
3887#endif /* MBEDTLS_SSL_PROTO_DTLS */
3888#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003891 ssl->in_left, nb_want ) );
3892
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003893 while( ssl->in_left < nb_want )
3894 {
3895 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003896
3897 if( ssl_check_timer( ssl ) != 0 )
3898 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3899 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003900 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003901 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003902 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003903 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3904 ssl->in_hdr + ssl->in_left, len,
3905 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003906 }
3907 else
3908 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003909 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003910 ssl->in_hdr + ssl->in_left, len );
3911 }
3912 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003915 ssl->in_left, nb_want ) );
3916 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003917
3918 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003920
3921 if( ret < 0 )
3922 return( ret );
3923
mohammad160352aecb92018-03-28 23:41:40 -07003924 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003925 {
Darryl Green11999bb2018-03-13 15:22:58 +00003926 MBEDTLS_SSL_DEBUG_MSG( 1,
3927 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003928 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003929 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3930 }
3931
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003932 ssl->in_left += ret;
3933 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003934 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003935#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003938
3939 return( 0 );
3940}
3941
3942/*
3943 * Flush any data not yet written
3944 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003945int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003946{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003947 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003948 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003951
Hanno Beckera58a8962019-06-13 16:11:15 +01003952 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003955 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003957 }
3958
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003959 /* Avoid incrementing counter if data is flushed */
3960 if( ssl->out_left == 0 )
3961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003963 return( 0 );
3964 }
3965
Paul Bakker5121ce52009-01-03 21:22:43 +00003966 while( ssl->out_left > 0 )
3967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003969 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003970
Hanno Becker2b1e3542018-08-06 11:19:13 +01003971 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003972 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003974 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003975
3976 if( ret <= 0 )
3977 return( ret );
3978
mohammad160352aecb92018-03-28 23:41:40 -07003979 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003980 {
Darryl Green11999bb2018-03-13 15:22:58 +00003981 MBEDTLS_SSL_DEBUG_MSG( 1,
3982 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003983 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003984 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3985 }
3986
Paul Bakker5121ce52009-01-03 21:22:43 +00003987 ssl->out_left -= ret;
3988 }
3989
Hanno Becker2b1e3542018-08-06 11:19:13 +01003990#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003991 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003992 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003993 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003994 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003995 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003996#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003997#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003998 {
3999 ssl->out_hdr = ssl->out_buf + 8;
4000 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004001#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01004002 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004005
4006 return( 0 );
4007}
4008
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004009/*
4010 * Functions to handle the DTLS retransmission state machine
4011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004012#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004013/*
4014 * Append current handshake message to current outgoing flight
4015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004017{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01004019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
4020 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
4021 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004022
4023 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004024 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == 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",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027 sizeof( mbedtls_ssl_flight_item ) ) );
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
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004031 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004032 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004034 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004035 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004036 }
4037
4038 /* Copy current handshake message with headers */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004039 mbedtls_platform_memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004040 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004041 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004042 msg->next = NULL;
4043
4044 /* Append to the current flight */
4045 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004046 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004047 else
4048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004049 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004050 while( cur->next != NULL )
4051 cur = cur->next;
4052 cur->next = msg;
4053 }
4054
Hanno Becker3b235902018-08-06 09:54:53 +01004055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004056 return( 0 );
4057}
4058
4059/*
4060 * Free the current flight of handshake messages
4061 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004062static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004063{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064 mbedtls_ssl_flight_item *cur = flight;
4065 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004066
4067 while( cur != NULL )
4068 {
4069 next = cur->next;
4070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071 mbedtls_free( cur->p );
4072 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004073
4074 cur = next;
4075 }
4076}
4077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4079static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004080#endif
4081
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004082/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004083 * Swap transform_out and out_ctr with the alternative ones
4084 */
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004085static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004086{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004088 unsigned char tmp_out_ctr[8];
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004089#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4090 int ret;
4091#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004092
4093 if( ssl->transform_out == ssl->handshake->alt_transform_out )
4094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004096 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004097 }
4098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004100
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004101 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004102 tmp_transform = ssl->transform_out;
4103 ssl->transform_out = ssl->handshake->alt_transform_out;
4104 ssl->handshake->alt_transform_out = tmp_transform;
4105
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004106 /* Swap epoch + sequence_number */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004107 mbedtls_platform_memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4108 mbedtls_platform_memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
4109 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004110
4111 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004112 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004114#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4115 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004117 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4120 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004121 }
4122 }
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004123#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
4124
4125 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004126}
4127
4128/*
4129 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004130 */
4131int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4132{
4133 int ret = 0;
4134
4135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4136
4137 ret = mbedtls_ssl_flight_transmit( ssl );
4138
4139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4140
4141 return( ret );
4142}
4143
4144/*
4145 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004146 *
4147 * Need to remember the current message in case flush_output returns
4148 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004149 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004150 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004151int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004152{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004153 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004157 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004159
4160 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004161 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004162 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4163 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004165 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004166 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004167
4168 while( ssl->handshake->cur_msg != NULL )
4169 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004170 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004171 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004172
Hanno Beckere1dcb032018-08-17 16:47:58 +01004173 int const is_finished =
4174 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4175 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4176
Hanno Becker04da1892018-08-14 13:22:10 +01004177 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4178 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4179
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004180 /* Swap epochs before sending Finished: we can't do it after
4181 * sending ChangeCipherSpec, in case write returns WANT_READ.
4182 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004183 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004184 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004186 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4187 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004188 }
4189
Hanno Becker67bc7c32018-08-06 11:33:50 +01004190 ret = ssl_get_remaining_payload_in_datagram( ssl );
4191 if( ret < 0 )
4192 return( ret );
4193 max_frag_len = (size_t) ret;
4194
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004195 /* CCS is copied as is, while HS messages may need fragmentation */
4196 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4197 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004198 if( max_frag_len == 0 )
4199 {
4200 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4201 return( ret );
4202
4203 continue;
4204 }
4205
Teppo Järvelin91d79382019-10-02 09:09:31 +03004206 mbedtls_platform_memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004207 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004208 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004209
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004210 /* Update position inside current message */
4211 ssl->handshake->cur_msg_p += cur->len;
4212 }
4213 else
4214 {
4215 const unsigned char * const p = ssl->handshake->cur_msg_p;
4216 const size_t hs_len = cur->len - 12;
4217 const size_t frag_off = p - ( cur->p + 12 );
4218 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004219 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004220
Hanno Beckere1dcb032018-08-17 16:47:58 +01004221 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004222 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004223 if( is_finished )
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004224 {
4225 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4226 return( ret );
4227 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004228
Hanno Becker67bc7c32018-08-06 11:33:50 +01004229 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4230 return( ret );
4231
4232 continue;
4233 }
4234 max_hs_frag_len = max_frag_len - 12;
4235
4236 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4237 max_hs_frag_len : rem_len;
4238
4239 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004240 {
4241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004242 (unsigned) cur_hs_frag_len,
4243 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004244 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004245
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004246 /* Messages are stored with handshake headers as if not fragmented,
4247 * copy beginning of headers then fill fragmentation fields.
4248 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004249 mbedtls_platform_memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004250
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004251 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[6], frag_off );
4252 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[9],
4253 cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004254
4255 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4256
Hanno Becker3f7b9732018-08-28 09:53:25 +01004257 /* Copy the handshake message content and set records fields */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004258 mbedtls_platform_memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004259 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004260 ssl->out_msgtype = cur->type;
4261
4262 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004263 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004264 }
4265
4266 /* If done with the current message move to the next one if any */
4267 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4268 {
4269 if( cur->next != NULL )
4270 {
4271 ssl->handshake->cur_msg = cur->next;
4272 ssl->handshake->cur_msg_p = cur->next->p + 12;
4273 }
4274 else
4275 {
4276 ssl->handshake->cur_msg = NULL;
4277 ssl->handshake->cur_msg_p = NULL;
4278 }
4279 }
4280
4281 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004282 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004285 return( ret );
4286 }
4287 }
4288
Hanno Becker67bc7c32018-08-06 11:33:50 +01004289 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4290 return( ret );
4291
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004292 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004293 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4294 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004295 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004298 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4299 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004300
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004302
4303 return( 0 );
4304}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004305
4306/*
4307 * To be called when the last message of an incoming flight is received.
4308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004309void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004310{
4311 /* We won't need to resend that one any more */
4312 ssl_flight_free( ssl->handshake->flight );
4313 ssl->handshake->flight = NULL;
4314 ssl->handshake->cur_msg = NULL;
4315
4316 /* The next incoming flight will start with this msg_seq */
4317 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4318
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004319 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004320 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004321
Hanno Becker0271f962018-08-16 13:23:47 +01004322 /* Clear future message buffering structure. */
4323 ssl_buffering_free( ssl );
4324
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004325 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004326 ssl_set_timer( ssl, 0 );
4327
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é-Gonnard5d8ba532014-09-19 15:09:21 +02004330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004332 }
4333 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004335}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004336
4337/*
4338 * To be called when the last message of an outgoing flight is send.
4339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004341{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004342 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004343 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004345 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4346 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004348 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004349 }
4350 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004352}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004353#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004354
Paul Bakker5121ce52009-01-03 21:22:43 +00004355/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004356 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004357 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004358
4359/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004360 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004361 *
4362 * - fill in handshake headers
4363 * - update handshake checksum
4364 * - DTLS: save message for resending
4365 * - then pass to the record layer
4366 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004367 * DTLS: except for HelloRequest, messages are only queued, and will only be
4368 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004369 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004370 * Inputs:
4371 * - ssl->out_msglen: 4 + actual handshake message len
4372 * (4 is the size of handshake headers for TLS)
4373 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4374 * - ssl->out_msg + 4: the handshake message body
4375 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004376 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004377 * - ssl->out_msglen: the length of the record contents
4378 * (including handshake headers but excluding record headers)
4379 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004380 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004381int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004382{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004383 int ret;
4384 const size_t hs_len = ssl->out_msglen - 4;
4385 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004386
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4388
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004389 /*
4390 * Sanity checks
4391 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004392 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004393 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4394 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004395 /* In SSLv3, the client might send a NoCertificate alert. */
4396#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004397 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004398 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004399 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4400 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004401#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4402 {
4403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4404 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4405 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004406 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004407
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004408 /* Whenever we send anything different from a
4409 * HelloRequest we should be in a handshake - double check. */
4410 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4411 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004412 ssl->handshake == NULL )
4413 {
4414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4415 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4416 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004418#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004419 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004420 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004421 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004422 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4424 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004425 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004426#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004427
Hanno Beckerb50a2532018-08-06 11:52:54 +01004428 /* Double-check that we did not exceed the bounds
4429 * of the outgoing record buffer.
4430 * This should never fail as the various message
4431 * writing functions must obey the bounds of the
4432 * outgoing record buffer, but better be safe.
4433 *
4434 * Note: We deliberately do not check for the MTU or MFL here.
4435 */
4436 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4437 {
4438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4439 "size %u, maximum %u",
4440 (unsigned) ssl->out_msglen,
4441 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4442 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4443 }
4444
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004445 /*
4446 * Fill handshake headers
4447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004448 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004449 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004450 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[1], hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004451
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004452 /*
4453 * DTLS has additional fields in the Handshake layer,
4454 * between the length field and the actual payload:
4455 * uint16 message_seq;
4456 * uint24 fragment_offset;
4457 * uint24 fragment_length;
4458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004460 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004461 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004462 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004463 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004464 {
4465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4466 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004467 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004468 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004469 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4470 }
4471
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004472 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004473 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004474
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004475 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004476 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004477 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004478 (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[4],
4479 ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004480 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004481 }
4482 else
4483 {
4484 ssl->out_msg[4] = 0;
4485 ssl->out_msg[5] = 0;
4486 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004487
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004488 /* Handshake hashes are computed without fragmentation,
4489 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004490 mbedtls_platform_memset( ssl->out_msg + 6, 0x00, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03004491 mbedtls_platform_memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004492 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004493#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004494
Hanno Becker0207e532018-08-28 10:28:28 +01004495 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004496 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004497 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004498 }
4499
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004500 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004501#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004502 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004503 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4504 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004505 {
4506 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004508 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004509 return( ret );
4510 }
4511 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004512 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004513#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004514 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004515 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004516 {
4517 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4518 return( ret );
4519 }
4520 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004521
4522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4523
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004524 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004525}
4526
4527/*
4528 * Record layer functions
4529 */
4530
4531/*
4532 * Write current record.
4533 *
4534 * Uses:
4535 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4536 * - ssl->out_msglen: length of the record content (excl headers)
4537 * - ssl->out_msg: record content
4538 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004539int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004540{
4541 int ret, done = 0;
4542 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004543 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004544
4545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004548 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004549 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004550 {
4551 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004553 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004554 return( ret );
4555 }
4556
4557 len = ssl->out_msglen;
4558 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004559#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004561#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4562 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004566 ret = mbedtls_ssl_hw_record_write( ssl );
4567 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004569 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4570 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004571 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004572
4573 if( ret == 0 )
4574 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004575 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004576#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004577 if( !done )
4578 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004579 unsigned i;
4580 size_t protected_record_size;
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004581 volatile int encrypted_fi = 0;
Hanno Becker2b1e3542018-08-06 11:19:13 +01004582
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004583 /* Skip writing the record content type to after the encryption,
4584 * as it may change when using the CID extension. */
4585
Hanno Becker2881d802019-05-22 14:44:53 +01004586 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4587 mbedtls_ssl_get_minor_ver( ssl ),
4588 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004589
Teppo Järvelin91d79382019-10-02 09:09:31 +03004590 mbedtls_platform_memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004591 (void)mbedtls_platform_put_uint16_be( ssl->out_len, len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004592
Paul Bakker48916f92012-09-16 19:57:18 +00004593 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004594 {
Hanno Becker3307b532017-12-27 21:37:21 +00004595 mbedtls_record rec;
4596
4597 rec.buf = ssl->out_iv;
4598 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4599 ( ssl->out_iv - ssl->out_buf );
4600 rec.data_len = ssl->out_msglen;
4601 rec.data_offset = ssl->out_msg - rec.buf;
4602
Teppo Järvelin91d79382019-10-02 09:09:31 +03004603 mbedtls_platform_memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004604 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4605 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004606 ssl->conf->transport, rec.ver );
4607 rec.type = ssl->out_msgtype;
4608
Hanno Beckera5a2b082019-05-15 14:03:01 +01004609#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004610 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004611 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004612#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004613
Hanno Becker611a83b2018-01-03 14:27:32 +00004614 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004615 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004616 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004618 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004619 return( ret );
4620 }
4621
Hanno Becker3307b532017-12-27 21:37:21 +00004622 if( rec.data_offset != 0 )
4623 {
4624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4625 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4626 }
4627
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004628 /* Update the record content type and CID. */
4629 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004630#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03004631 /* Not using more secure mbedtls_platform_memcpy as cid is public */
4632 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004633#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004634 ssl->out_msglen = len = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004635 (void)mbedtls_platform_put_uint16_be( ssl->out_len, rec.data_len );
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004636 encrypted_fi = 1;
4637 }
4638
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004639 /* Double check to ensure the encryption has been done */
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004640 if( ssl->transform_out != NULL && encrypted_fi == 0 )
4641 {
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004642 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004643 }
4644
Hanno Becker43395762019-05-03 14:46:38 +01004645 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004646
4647#if defined(MBEDTLS_SSL_PROTO_DTLS)
4648 /* In case of DTLS, double-check that we don't exceed
4649 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004650 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004651 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004652 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004653 if( ret < 0 )
4654 return( ret );
4655
4656 if( protected_record_size > (size_t) ret )
4657 {
4658 /* Should never happen */
4659 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4660 }
4661 }
4662#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004663
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004664 /* Now write the potentially updated record content type. */
4665 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004667 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004668 "version = [%d:%d], msglen = %d",
4669 ssl->out_hdr[0], ssl->out_hdr[1],
4670 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004672 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004673 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004674
4675 ssl->out_left += protected_record_size;
4676 ssl->out_hdr += protected_record_size;
4677 ssl_update_out_pointers( ssl, ssl->transform_out );
4678
Hanno Becker04484622018-08-06 09:49:38 +01004679 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4680 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4681 break;
4682
4683 /* The loop goes to its end iff the counter is wrapping */
4684 if( i == ssl_ep_len( ssl ) )
4685 {
4686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4687 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4688 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004689 }
4690
Hanno Becker67bc7c32018-08-06 11:33:50 +01004691#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004692 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004693 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004694 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004695 size_t remaining;
4696 ret = ssl_get_remaining_payload_in_datagram( ssl );
4697 if( ret < 0 )
4698 {
4699 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4700 ret );
4701 return( ret );
4702 }
4703
4704 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004705 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004706 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004707 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004708 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004709 else
4710 {
Hanno Becker513815a2018-08-20 11:56:09 +01004711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004712 }
4713 }
4714#endif /* MBEDTLS_SSL_PROTO_DTLS */
4715
4716 if( ( flush == SSL_FORCE_FLUSH ) &&
4717 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004719 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004720 return( ret );
4721 }
4722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004724
4725 return( 0 );
4726}
4727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004728#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004729
4730static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4731{
4732 if( ssl->in_msglen < ssl->in_hslen ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +03004733 mbedtls_platform_memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4734 mbedtls_platform_memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
Hanno Beckere25e3b72018-08-16 09:30:53 +01004735 {
4736 return( 1 );
4737 }
4738 return( 0 );
4739}
Hanno Becker44650b72018-08-16 12:51:11 +01004740
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004741static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004742{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004743 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[9] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004744}
4745
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004746static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004747{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004748 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[6] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004749}
4750
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004751static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004752{
4753 uint32_t msg_len, frag_off, frag_len;
4754
4755 msg_len = ssl_get_hs_total_len( ssl );
4756 frag_off = ssl_get_hs_frag_off( ssl );
4757 frag_len = ssl_get_hs_frag_len( ssl );
4758
4759 if( frag_off > msg_len )
4760 return( -1 );
4761
4762 if( frag_len > msg_len - frag_off )
4763 return( -1 );
4764
4765 if( frag_len + 12 > ssl->in_msglen )
4766 return( -1 );
4767
4768 return( 0 );
4769}
4770
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004771/*
4772 * Mark bits in bitmask (used for DTLS HS reassembly)
4773 */
4774static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4775{
4776 unsigned int start_bits, end_bits;
4777
4778 start_bits = 8 - ( offset % 8 );
4779 if( start_bits != 8 )
4780 {
4781 size_t first_byte_idx = offset / 8;
4782
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004783 /* Special case */
4784 if( len <= start_bits )
4785 {
4786 for( ; len != 0; len-- )
4787 mask[first_byte_idx] |= 1 << ( start_bits - len );
4788
4789 /* Avoid potential issues with offset or len becoming invalid */
4790 return;
4791 }
4792
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004793 offset += start_bits; /* Now offset % 8 == 0 */
4794 len -= start_bits;
4795
4796 for( ; start_bits != 0; start_bits-- )
4797 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4798 }
4799
4800 end_bits = len % 8;
4801 if( end_bits != 0 )
4802 {
4803 size_t last_byte_idx = ( offset + len ) / 8;
4804
4805 len -= end_bits; /* Now len % 8 == 0 */
4806
4807 for( ; end_bits != 0; end_bits-- )
4808 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4809 }
4810
4811 memset( mask + offset / 8, 0xFF, len / 8 );
4812}
4813
4814/*
4815 * Check that bitmask is full
4816 */
4817static int ssl_bitmask_check( unsigned char *mask, size_t len )
4818{
4819 size_t i;
4820
4821 for( i = 0; i < len / 8; i++ )
4822 if( mask[i] != 0xFF )
4823 return( -1 );
4824
4825 for( i = 0; i < len % 8; i++ )
4826 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4827 return( -1 );
4828
4829 return( 0 );
4830}
4831
Hanno Becker56e205e2018-08-16 09:06:12 +01004832/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004833static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004834 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004835{
Hanno Becker56e205e2018-08-16 09:06:12 +01004836 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004837
Hanno Becker56e205e2018-08-16 09:06:12 +01004838 alloc_len = 12; /* Handshake header */
4839 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004840
Hanno Beckerd07df862018-08-16 09:14:58 +01004841 if( add_bitmap )
4842 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004843
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004844 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004845}
Hanno Becker56e205e2018-08-16 09:06:12 +01004846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004847#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004848
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004849static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004850{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004851 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[1] ) );
Hanno Becker12555c62018-08-16 12:47:53 +01004852}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004853
Simon Butcher99000142016-10-13 17:21:01 +01004854int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004855{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004856 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004859 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004860 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004861 }
4862
Hanno Becker12555c62018-08-16 12:47:53 +01004863 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004865 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004866 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004867 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004869#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004870 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004871 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004872 int ret;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004873 unsigned int recv_msg_seq = (unsigned int)
4874 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004875
Hanno Becker44650b72018-08-16 12:51:11 +01004876 if( ssl_check_hs_header( ssl ) != 0 )
4877 {
4878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4879 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4880 }
4881
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004882 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004883 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4884 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4885 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4886 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004887 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004888 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4889 {
4890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4891 recv_msg_seq,
4892 ssl->handshake->in_msg_seq ) );
4893 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4894 }
4895
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004896 /* Retransmit only on last message from previous flight, to avoid
4897 * too many retransmissions.
4898 * Besides, No sane server ever retransmits HelloVerifyRequest */
4899 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004900 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004903 "message_seq = %d, start_of_flight = %d",
4904 recv_msg_seq,
4905 ssl->handshake->in_flight_start_seq ) );
4906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004907 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004909 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004910 return( ret );
4911 }
4912 }
4913 else
4914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004916 "message_seq = %d, expected = %d",
4917 recv_msg_seq,
4918 ssl->handshake->in_msg_seq ) );
4919 }
4920
Hanno Becker90333da2017-10-10 11:27:13 +01004921 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004922 }
4923 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004924
Hanno Becker6d97ef52018-08-16 13:09:04 +01004925 /* Message reassembly is handled alongside buffering of future
4926 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004927 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004928 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004929 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004932 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004933 }
4934 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004935 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004936#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004937#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004938 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004939 /* With TLS we don't handle fragmentation (for now) */
4940 if( ssl->in_msglen < ssl->in_hslen )
4941 {
4942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4943 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4944 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004945 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004946#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004947
Simon Butcher99000142016-10-13 17:21:01 +01004948 return( 0 );
4949}
4950
4951void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4952{
Hanno Becker0271f962018-08-16 13:23:47 +01004953 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004954
Hanno Becker0271f962018-08-16 13:23:47 +01004955 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004956 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004957
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004958 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004959#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004960 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004961 ssl->handshake != NULL )
4962 {
Hanno Becker0271f962018-08-16 13:23:47 +01004963 unsigned offset;
4964 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004965
Hanno Becker0271f962018-08-16 13:23:47 +01004966 /* Increment handshake sequence number */
4967 hs->in_msg_seq++;
4968
4969 /*
4970 * Clear up handshake buffering and reassembly structure.
4971 */
4972
4973 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004974 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004975
4976 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004977 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4978 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004979 offset++, hs_buf++ )
4980 {
4981 *hs_buf = *(hs_buf + 1);
4982 }
4983
4984 /* Create a fresh last entry */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004985 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004986 }
4987#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004988}
4989
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004990/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004991 * DTLS anti-replay: RFC 6347 4.1.2.6
4992 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004993 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4994 * Bit n is set iff record number in_window_top - n has been seen.
4995 *
4996 * Usually, in_window_top is the last record number seen and the lsb of
4997 * in_window is set. The only exception is the initial state (record number 0
4998 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004999 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005000#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5001static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005002{
5003 ssl->in_window_top = 0;
5004 ssl->in_window = 0;
5005}
5006
5007static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
5008{
5009 return( ( (uint64_t) buf[0] << 40 ) |
5010 ( (uint64_t) buf[1] << 32 ) |
5011 ( (uint64_t) buf[2] << 24 ) |
5012 ( (uint64_t) buf[3] << 16 ) |
5013 ( (uint64_t) buf[4] << 8 ) |
5014 ( (uint64_t) buf[5] ) );
5015}
5016
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005017static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
5018{
5019 int ret;
5020 unsigned char *original_in_ctr;
5021
5022 // save original in_ctr
5023 original_in_ctr = ssl->in_ctr;
5024
5025 // use counter from record
5026 ssl->in_ctr = record_in_ctr;
5027
5028 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
5029
5030 // restore the counter
5031 ssl->in_ctr = original_in_ctr;
5032
5033 return ret;
5034}
5035
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005036/*
5037 * Return 0 if sequence number is acceptable, -1 otherwise
5038 */
Hanno Beckerfc551722019-07-12 08:50:37 +01005039int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005040{
5041 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5042 uint64_t bit;
5043
Hanno Becker7f376f42019-06-12 16:20:48 +01005044 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5045 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5046 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005047 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01005048 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005049
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005050 if( rec_seqnum > ssl->in_window_top )
5051 return( 0 );
5052
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005053 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005054
5055 if( bit >= 64 )
5056 return( -1 );
5057
5058 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
5059 return( -1 );
5060
5061 return( 0 );
5062}
5063
5064/*
5065 * Update replay window on new validated record
5066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005068{
5069 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5070
Hanno Becker7f376f42019-06-12 16:20:48 +01005071 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5072 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5073 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005074 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01005075 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005076
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005077 if( rec_seqnum > ssl->in_window_top )
5078 {
5079 /* Update window_top and the contents of the window */
5080 uint64_t shift = rec_seqnum - ssl->in_window_top;
5081
5082 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005083 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005084 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005085 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005086 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005087 ssl->in_window |= 1;
5088 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005089
5090 ssl->in_window_top = rec_seqnum;
5091 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005092 else
5093 {
5094 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005095 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005096
5097 if( bit < 64 ) /* Always true, but be extra sure */
5098 ssl->in_window |= (uint64_t) 1 << bit;
5099 }
5100}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005101#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005102
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005103#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005104/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005105static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
5106
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005107/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005108 * Without any SSL context, check if a datagram looks like a ClientHello with
5109 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01005110 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005111 *
5112 * - if cookie is valid, return 0
5113 * - if ClientHello looks superficially valid but cookie is not,
5114 * fill obuf and set olen, then
5115 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
5116 * - otherwise return a specific error code
5117 */
5118static int ssl_check_dtls_clihlo_cookie(
5119 mbedtls_ssl_cookie_write_t *f_cookie_write,
5120 mbedtls_ssl_cookie_check_t *f_cookie_check,
5121 void *p_cookie,
5122 const unsigned char *cli_id, size_t cli_id_len,
5123 const unsigned char *in, size_t in_len,
5124 unsigned char *obuf, size_t buf_len, size_t *olen )
5125{
5126 size_t sid_len, cookie_len;
5127 unsigned char *p;
5128
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005129 /*
5130 * Structure of ClientHello with record and handshake headers,
5131 * and expected values. We don't need to check a lot, more checks will be
5132 * done when actually parsing the ClientHello - skipping those checks
5133 * avoids code duplication and does not make cookie forging any easier.
5134 *
5135 * 0-0 ContentType type; copied, must be handshake
5136 * 1-2 ProtocolVersion version; copied
5137 * 3-4 uint16 epoch; copied, must be 0
5138 * 5-10 uint48 sequence_number; copied
5139 * 11-12 uint16 length; (ignored)
5140 *
5141 * 13-13 HandshakeType msg_type; (ignored)
5142 * 14-16 uint24 length; (ignored)
5143 * 17-18 uint16 message_seq; copied
5144 * 19-21 uint24 fragment_offset; copied, must be 0
5145 * 22-24 uint24 fragment_length; (ignored)
5146 *
5147 * 25-26 ProtocolVersion client_version; (ignored)
5148 * 27-58 Random random; (ignored)
5149 * 59-xx SessionID session_id; 1 byte len + sid_len content
5150 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5151 * ...
5152 *
5153 * Minimum length is 61 bytes.
5154 */
5155 if( in_len < 61 ||
5156 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5157 in[3] != 0 || in[4] != 0 ||
5158 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5159 {
5160 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5161 }
5162
5163 sid_len = in[59];
5164 if( sid_len > in_len - 61 )
5165 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5166
5167 cookie_len = in[60 + sid_len];
5168 if( cookie_len > in_len - 60 )
5169 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5170
5171 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5172 cli_id, cli_id_len ) == 0 )
5173 {
5174 /* Valid cookie */
5175 return( 0 );
5176 }
5177
5178 /*
5179 * If we get here, we've got an invalid cookie, let's prepare HVR.
5180 *
5181 * 0-0 ContentType type; copied
5182 * 1-2 ProtocolVersion version; copied
5183 * 3-4 uint16 epoch; copied
5184 * 5-10 uint48 sequence_number; copied
5185 * 11-12 uint16 length; olen - 13
5186 *
5187 * 13-13 HandshakeType msg_type; hello_verify_request
5188 * 14-16 uint24 length; olen - 25
5189 * 17-18 uint16 message_seq; copied
5190 * 19-21 uint24 fragment_offset; copied
5191 * 22-24 uint24 fragment_length; olen - 25
5192 *
5193 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5194 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5195 *
5196 * Minimum length is 28.
5197 */
5198 if( buf_len < 28 )
5199 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5200
5201 /* Copy most fields and adapt others */
Teppo Järvelin91d79382019-10-02 09:09:31 +03005202 mbedtls_platform_memcpy( obuf, in, 25 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005203 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5204 obuf[25] = 0xfe;
5205 obuf[26] = 0xff;
5206
5207 /* Generate and write actual cookie */
5208 p = obuf + 28;
5209 if( f_cookie_write( p_cookie,
5210 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5211 {
5212 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5213 }
5214
5215 *olen = p - obuf;
5216
5217 /* Go back and fill length fields */
5218 obuf[27] = (unsigned char)( *olen - 28 );
5219
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005220 (void)mbedtls_platform_put_uint24_be( &obuf[14], ( *olen - 25 ) );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005221 obuf[22] = obuf[14];
5222 obuf[23] = obuf[15];
5223 obuf[24] = obuf[16];
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005224
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005225 (void)mbedtls_platform_put_uint16_be( &obuf[11], ( *olen - 13 ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005226
5227 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5228}
5229
5230/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005231 * Handle possible client reconnect with the same UDP quadruplet
5232 * (RFC 6347 Section 4.2.8).
5233 *
5234 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5235 * that looks like a ClientHello.
5236 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005237 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005238 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005239 * - if the input looks like a ClientHello with a valid cookie,
5240 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005241 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005242 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005243 *
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005244 * This function is called (through ssl_check_client_reconnect()) when an
5245 * unexpected record is found in ssl_get_next_record(), which will discard the
5246 * record if we return 0, and bubble up the return value otherwise (this
5247 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
5248 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005249 */
5250static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5251{
5252 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005253 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005254
Hanno Becker87b56262019-07-10 14:37:41 +01005255 if( ssl->conf->f_cookie_write == NULL ||
5256 ssl->conf->f_cookie_check == NULL )
5257 {
5258 /* If we can't use cookies to verify reachability of the peer,
5259 * drop the record. */
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
5261 "can't check reconnect validity" ) );
Hanno Becker87b56262019-07-10 14:37:41 +01005262 return( 0 );
5263 }
5264
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005265 ret = ssl_check_dtls_clihlo_cookie(
5266 ssl->conf->f_cookie_write,
5267 ssl->conf->f_cookie_check,
5268 ssl->conf->p_cookie,
5269 ssl->cli_id, ssl->cli_id_len,
5270 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005271 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005272
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005273 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5274
5275 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005276 {
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005277 int send_ret;
5278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
5279 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
5280 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08005281 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005282 * If the error is permanent we'll catch it later,
5283 * if it's not, then hopefully it'll work next time. */
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005284 send_ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
5285 MBEDTLS_SSL_DEBUG_RET( 2, "mbedtls_ssl_get_send", send_ret );
5286 (void) send_ret;
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005287 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005288 }
5289
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005290 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005291 {
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005293 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5294 {
5295 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5296 return( ret );
5297 }
5298
5299 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005300 }
5301
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005302 return( ret );
5303}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005304#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005305
Hanno Becker46483f12019-05-03 13:25:54 +01005306static int ssl_check_record_type( uint8_t record_type )
5307{
5308 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5309 record_type != MBEDTLS_SSL_MSG_ALERT &&
5310 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5311 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5312 {
5313 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5314 }
5315
5316 return( 0 );
5317}
5318
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005319/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005320 * ContentType type;
5321 * ProtocolVersion version;
5322 * uint16 epoch; // DTLS only
5323 * uint48 sequence_number; // DTLS only
5324 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005325 *
5326 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005327 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005328 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5329 *
5330 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005331 * 1. proceed with the record if this function returns 0
5332 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5333 * 3. return CLIENT_RECONNECT if this function return that value
5334 * 4. drop the whole datagram if this function returns anything else.
5335 * Point 2 is needed when the peer is resending, and we have already received
5336 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005337 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005338static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005339 unsigned char *buf,
5340 size_t len,
5341 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005342{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005343 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005344
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005345 size_t const rec_hdr_type_offset = 0;
5346 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005347
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005348 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5349 rec_hdr_type_len;
5350 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005351
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005352 size_t const rec_hdr_ctr_len = 8;
5353#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005354 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005355 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5356 rec_hdr_version_len;
5357
Hanno Beckera5a2b082019-05-15 14:03:01 +01005358#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005359 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5360 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005361 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005362#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5363#endif /* MBEDTLS_SSL_PROTO_DTLS */
5364
5365 size_t rec_hdr_len_offset; /* To be determined */
5366 size_t const rec_hdr_len_len = 2;
5367
5368 /*
5369 * Check minimum lengths for record header.
5370 */
5371
5372#if defined(MBEDTLS_SSL_PROTO_DTLS)
5373 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5374 {
5375 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5376 }
5377 MBEDTLS_SSL_TRANSPORT_ELSE
5378#endif /* MBEDTLS_SSL_PROTO_DTLS */
5379#if defined(MBEDTLS_SSL_PROTO_TLS)
5380 {
5381 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5382 }
5383#endif /* MBEDTLS_SSL_PROTO_DTLS */
5384
5385 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5386 {
5387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5388 (unsigned) len,
5389 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5390 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5391 }
5392
5393 /*
5394 * Parse and validate record content type
5395 */
5396
5397 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005398
5399 /* Check record content type */
5400#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5401 rec->cid_len = 0;
5402
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005403 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005404 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5405 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005406 {
5407 /* Shift pointers to account for record header including CID
5408 * struct {
5409 * ContentType special_type = tls12_cid;
5410 * ProtocolVersion version;
5411 * uint16 epoch;
5412 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005413 * opaque cid[cid_length]; // Additional field compared to
5414 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005415 * uint16 length;
5416 * opaque enc_content[DTLSCiphertext.length];
5417 * } DTLSCiphertext;
5418 */
5419
5420 /* So far, we only support static CID lengths
5421 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005422 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5423 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005424
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005425 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005426 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5428 (unsigned) len,
5429 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005430 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005431 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005432
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005433 /* configured CID len is guaranteed at most 255, see
5434 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5435 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005436 /* Not using more secure mbedtls_platform_memcpy as cid is public */
5437 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005438 }
5439 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005440#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005441 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005442 if( ssl_check_record_type( rec->type ) )
5443 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5445 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005446 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5447 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005448 }
5449
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005450 /*
5451 * Parse and validate record version
5452 */
5453
Hanno Becker8061c6e2019-07-26 08:07:03 +01005454 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5455 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005456 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5457 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005458 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005459
Hanno Becker2881d802019-05-22 14:44:53 +01005460 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5463 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005464 }
5465
Hanno Becker7bcf2b52019-07-26 09:02:40 +01005466 if( mbedtls_ssl_ver_gt( minor_ver,
5467 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5470 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005471 }
5472
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005473 /*
5474 * Parse/Copy record sequence number.
5475 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005476
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005477#if defined(MBEDTLS_SSL_PROTO_DTLS)
5478 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5479 {
5480 /* Copy explicit record sequence number from input buffer. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005481 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5482 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005483 rec_hdr_ctr_len );
5484 }
5485 MBEDTLS_SSL_TRANSPORT_ELSE
5486#endif /* MBEDTLS_SSL_PROTO_DTLS */
5487#if defined(MBEDTLS_SSL_PROTO_TLS)
5488 {
5489 /* Copy implicit record sequence number from SSL context structure. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005490 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5491 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005492 }
5493#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005494
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005495 /*
5496 * Parse record length.
5497 */
5498
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005499 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005500 rec->data_len = mbedtls_platform_get_uint16_be( &buf[rec_hdr_len_offset] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005501 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5502
Hanno Becker8b09b732019-05-08 12:03:28 +01005503 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005504 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005505 rec->type,
5506 major_ver, minor_ver, rec->data_len ) );
5507
5508 rec->buf = buf;
5509 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005510
Hanno Beckerec014082019-07-26 08:20:27 +01005511 if( rec->data_len == 0 )
5512 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5513
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005514 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005515 * DTLS-related tests.
5516 * Check epoch before checking length constraint because
5517 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5518 * message gets duplicated before the corresponding Finished message,
5519 * the second ChangeCipherSpec should be discarded because it belongs
5520 * to an old epoch, but not because its length is shorter than
5521 * the minimum record length for packets using the new record transform.
5522 * Note that these two kinds of failures are handled differently,
5523 * as an unexpected record is silently skipped but an invalid
5524 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005525 */
5526#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005527 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005528 {
Arto Kinnunena3fa06e2019-09-09 12:22:51 +03005529 rec_epoch = (uint32_t)mbedtls_platform_get_uint16_be( rec->ctr );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005530
Hanno Beckere0452772019-07-10 17:12:07 +01005531 /* Check that the datagram is large enough to contain a record
5532 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005533 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005534 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5536 (unsigned) len,
5537 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005538 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5539 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005540
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005541 /* Records from other, non-matching epochs are silently discarded.
5542 * (The case of same-port Client reconnects must be considered in
5543 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005544 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005545 {
5546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5547 "expected %d, received %d",
5548 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005549
5550 /* Records from the next epoch are considered for buffering
5551 * (concretely: early Finished messages). */
5552 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5553 {
5554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5555 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5556 }
5557
Hanno Becker87b56262019-07-10 14:37:41 +01005558 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005559 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005560#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005561 /* For records from the correct epoch, check whether their
5562 * sequence number has been seen before. */
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005563 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
5564 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005565 {
5566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5567 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5568 }
5569#endif
5570 }
5571#endif /* MBEDTLS_SSL_PROTO_DTLS */
5572
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005573 return( 0 );
5574}
Paul Bakker5121ce52009-01-03 21:22:43 +00005575
Hanno Becker87b56262019-07-10 14:37:41 +01005576
5577#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5578static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5579{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005580 unsigned int rec_epoch = (unsigned int)
5581 mbedtls_platform_get_uint16_be( &ssl->in_ctr[0] );
Hanno Becker87b56262019-07-10 14:37:41 +01005582
5583 /*
5584 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5585 * access the first byte of record content (handshake type), as we
5586 * have an active transform (possibly iv_len != 0), so use the
5587 * fact that the record header len is 13 instead.
5588 */
5589 if( rec_epoch == 0 &&
5590 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5591 MBEDTLS_SSL_IS_SERVER &&
5592 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5593 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5594 ssl->in_left > 13 &&
5595 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5596 {
5597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5598 "from the same port" ) );
5599 return( ssl_handle_possible_reconnect( ssl ) );
5600 }
5601
5602 return( 0 );
5603}
5604#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5605
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005606/*
Manuel Pégourié-Gonnardae48d862020-01-03 12:18:49 +01005607 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005608 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005609static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5610 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005611{
5612 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005615 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5618 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622 ret = mbedtls_ssl_hw_record_read( ssl );
5623 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5626 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005627 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005628
5629 if( ret == 0 )
5630 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005631 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005633 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005634 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005635 unsigned char const old_msg_type = rec->type;
5636
Hanno Becker611a83b2018-01-03 14:27:32 +00005637 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005638 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005641
Hanno Beckera5a2b082019-05-15 14:03:01 +01005642#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005643 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005644 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5645 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005646 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005647 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005648 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005649 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005650#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005651
Paul Bakker5121ce52009-01-03 21:22:43 +00005652 return( ret );
5653 }
5654
Hanno Becker106f3da2019-07-12 09:35:58 +01005655 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005656 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005657 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005658 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005659 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005660
Paul Bakker5121ce52009-01-03 21:22:43 +00005661 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005662 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005663
Hanno Beckera5a2b082019-05-15 14:03:01 +01005664#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005665 /* We have already checked the record content type
5666 * in ssl_parse_record_header(), failing or silently
5667 * dropping the record in the case of an unknown type.
5668 *
5669 * Since with the use of CIDs, the record content type
5670 * might change during decryption, re-check the record
5671 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005672 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005673 {
5674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5675 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5676 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005677#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005678
Hanno Becker106f3da2019-07-12 09:35:58 +01005679 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005680 {
5681#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005682 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005683 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005684 {
5685 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5687 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5688 }
5689#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5690
5691 ssl->nb_zero++;
5692
5693 /*
5694 * Three or more empty messages may be a DoS attack
5695 * (excessive CPU consumption).
5696 */
5697 if( ssl->nb_zero > 3 )
5698 {
5699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005700 "messages, possible DoS attack" ) );
5701 /* Treat the records as if they were not properly authenticated,
5702 * thereby failing the connection if we see more than allowed
5703 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005704 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5705 }
5706 }
5707 else
5708 ssl->nb_zero = 0;
5709
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005710 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5711#if defined(MBEDTLS_SSL_PROTO_TLS)
5712 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005713 {
5714 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005715 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005716 if( ++ssl->in_ctr[i - 1] != 0 )
5717 break;
5718
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005719 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005720 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005721 {
5722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5723 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5724 }
5725 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005726#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005727
Paul Bakker5121ce52009-01-03 21:22:43 +00005728 }
5729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005730#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005731 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005733 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005734 }
5735#endif
5736
Hanno Beckerf0242852019-07-09 17:30:02 +01005737 /* Check actual (decrypted) record content length against
5738 * configured maximum. */
5739 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5740 {
5741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5742 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5743 }
5744
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005745 return( 0 );
5746}
5747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005748static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005749
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005750/*
5751 * Read a record.
5752 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005753 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5754 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5755 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005756 */
Hanno Becker1097b342018-08-15 14:09:41 +01005757
5758/* Helper functions for mbedtls_ssl_read_record(). */
5759static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005760static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5761static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005762
Hanno Becker327c93b2018-08-15 13:56:18 +01005763int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005764 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005765{
5766 int ret;
5767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005769
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005770 if( ssl->keep_current_message == 0 )
5771 {
5772 do {
Simon Butcher99000142016-10-13 17:21:01 +01005773
Hanno Becker26994592018-08-15 14:14:59 +01005774 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005775 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005776 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005777
Hanno Beckere74d5562018-08-15 14:26:08 +01005778 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005779 {
Hanno Becker40f50842018-08-15 14:48:01 +01005780#if defined(MBEDTLS_SSL_PROTO_DTLS)
5781 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005782
Hanno Becker40f50842018-08-15 14:48:01 +01005783 /* We only check for buffered messages if the
5784 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005785 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005786 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005787 {
Hanno Becker40f50842018-08-15 14:48:01 +01005788 if( ssl_load_buffered_message( ssl ) == 0 )
5789 have_buffered = 1;
5790 }
5791
5792 if( have_buffered == 0 )
5793#endif /* MBEDTLS_SSL_PROTO_DTLS */
5794 {
5795 ret = ssl_get_next_record( ssl );
5796 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5797 continue;
5798
5799 if( ret != 0 )
5800 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005801 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005802 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005803 return( ret );
5804 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005805 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005806 }
5807
5808 ret = mbedtls_ssl_handle_message_type( ssl );
5809
Hanno Becker40f50842018-08-15 14:48:01 +01005810#if defined(MBEDTLS_SSL_PROTO_DTLS)
5811 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5812 {
5813 /* Buffer future message */
5814 ret = ssl_buffer_message( ssl );
5815 if( ret != 0 )
5816 return( ret );
5817
5818 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5819 }
5820#endif /* MBEDTLS_SSL_PROTO_DTLS */
5821
Hanno Becker90333da2017-10-10 11:27:13 +01005822 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5823 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005824
5825 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005826 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005827 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005828 return( ret );
5829 }
5830
Hanno Becker327c93b2018-08-15 13:56:18 +01005831 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005832 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005833 {
5834 mbedtls_ssl_update_handshake_status( ssl );
5835 }
Simon Butcher99000142016-10-13 17:21:01 +01005836 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005837 else
Simon Butcher99000142016-10-13 17:21:01 +01005838 {
Hanno Becker02f59072018-08-15 14:00:24 +01005839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005840 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005841 }
5842
5843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5844
5845 return( 0 );
5846}
5847
Hanno Becker40f50842018-08-15 14:48:01 +01005848#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005849static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005850{
Hanno Becker40f50842018-08-15 14:48:01 +01005851 if( ssl->in_left > ssl->next_record_offset )
5852 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005853
Hanno Becker40f50842018-08-15 14:48:01 +01005854 return( 0 );
5855}
5856
5857static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5858{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005859 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005860 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005861 int ret = 0;
5862
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005863 if( hs == NULL )
5864 return( -1 );
5865
Hanno Beckere00ae372018-08-20 09:39:42 +01005866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5867
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005868 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5869 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5870 {
5871 /* Check if we have seen a ChangeCipherSpec before.
5872 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005873 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005874 {
5875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5876 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005877 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005878 }
5879
Hanno Becker39b8bc92018-08-28 17:17:13 +01005880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005881 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5882 ssl->in_msglen = 1;
5883 ssl->in_msg[0] = 1;
5884
5885 /* As long as they are equal, the exact value doesn't matter. */
5886 ssl->in_left = 0;
5887 ssl->next_record_offset = 0;
5888
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005889 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005890 goto exit;
5891 }
Hanno Becker37f95322018-08-16 13:55:32 +01005892
Hanno Beckerb8f50142018-08-28 10:01:34 +01005893#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005894 /* Debug only */
5895 {
5896 unsigned offset;
5897 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5898 {
5899 hs_buf = &hs->buffering.hs[offset];
5900 if( hs_buf->is_valid == 1 )
5901 {
5902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5903 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005904 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005905 }
5906 }
5907 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005908#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005909
5910 /* Check if we have buffered and/or fully reassembled the
5911 * next handshake message. */
5912 hs_buf = &hs->buffering.hs[0];
5913 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5914 {
5915 /* Synthesize a record containing the buffered HS message. */
Arto Kinnunen84eeb4f2019-09-10 10:32:30 +03005916 size_t msg_len = mbedtls_platform_get_uint24_be( &hs_buf->data[1] );
Hanno Becker37f95322018-08-16 13:55:32 +01005917
5918 /* Double-check that we haven't accidentally buffered
5919 * a message that doesn't fit into the input buffer. */
5920 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5921 {
5922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5923 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5924 }
5925
5926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5927 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5928 hs_buf->data, msg_len + 12 );
5929
5930 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5931 ssl->in_hslen = msg_len + 12;
5932 ssl->in_msglen = msg_len + 12;
Teppo Järvelin91d79382019-10-02 09:09:31 +03005933 mbedtls_platform_memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
Hanno Becker37f95322018-08-16 13:55:32 +01005934
5935 ret = 0;
5936 goto exit;
5937 }
5938 else
5939 {
5940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5941 hs->in_msg_seq ) );
5942 }
5943
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005944 ret = -1;
5945
5946exit:
5947
5948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5949 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005950}
5951
Hanno Beckera02b0b42018-08-21 17:20:27 +01005952static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5953 size_t desired )
5954{
5955 int offset;
5956 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5958 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005959
Hanno Becker01315ea2018-08-21 17:22:17 +01005960 /* Get rid of future records epoch first, if such exist. */
5961 ssl_free_buffered_record( ssl );
5962
5963 /* Check if we have enough space available now. */
5964 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5965 hs->buffering.total_bytes_buffered ) )
5966 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005968 return( 0 );
5969 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005970
Hanno Becker4f432ad2018-08-28 10:02:32 +01005971 /* We don't have enough space to buffer the next expected handshake
5972 * message. Remove buffers used for future messages to gain space,
5973 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005974 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5975 offset >= 0; offset-- )
5976 {
5977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5978 offset ) );
5979
Hanno Beckerb309b922018-08-23 13:18:05 +01005980 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005981
5982 /* Check if we have enough space available now. */
5983 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5984 hs->buffering.total_bytes_buffered ) )
5985 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005987 return( 0 );
5988 }
5989 }
5990
5991 return( -1 );
5992}
5993
Hanno Becker40f50842018-08-15 14:48:01 +01005994static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5995{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005996 int ret = 0;
5997 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5998
5999 if( hs == NULL )
6000 return( 0 );
6001
6002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
6003
6004 switch( ssl->in_msgtype )
6005 {
6006 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
6007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01006008
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01006009 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006010 break;
6011
6012 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01006013 {
6014 unsigned recv_msg_seq_offset;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03006015 unsigned recv_msg_seq = (unsigned)
6016 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006017
Hanno Becker37f95322018-08-16 13:55:32 +01006018 mbedtls_ssl_hs_buffer *hs_buf;
6019 size_t msg_len = ssl->in_hslen - 12;
6020
6021 /* We should never receive an old handshake
6022 * message - double-check nonetheless. */
6023 if( recv_msg_seq < ssl->handshake->in_msg_seq )
6024 {
6025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6026 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6027 }
6028
6029 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
6030 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
6031 {
6032 /* Silently ignore -- message too far in the future */
6033 MBEDTLS_SSL_DEBUG_MSG( 2,
6034 ( "Ignore future HS message with sequence number %u, "
6035 "buffering window %u - %u",
6036 recv_msg_seq, ssl->handshake->in_msg_seq,
6037 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
6038
6039 goto exit;
6040 }
6041
6042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
6043 recv_msg_seq, recv_msg_seq_offset ) );
6044
6045 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
6046
6047 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01006048 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01006049 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006050 size_t reassembly_buf_sz;
6051
Hanno Becker37f95322018-08-16 13:55:32 +01006052 hs_buf->is_fragmented =
6053 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
6054
6055 /* We copy the message back into the input buffer
6056 * after reassembly, so check that it's not too large.
6057 * This is an implementation-specific limitation
6058 * and not one from the standard, hence it is not
6059 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01006060 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01006061 {
6062 /* Ignore message */
6063 goto exit;
6064 }
6065
Hanno Beckere0b150f2018-08-21 15:51:03 +01006066 /* Check if we have enough space to buffer the message. */
6067 if( hs->buffering.total_bytes_buffered >
6068 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
6069 {
6070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6071 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6072 }
6073
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006074 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
6075 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01006076
6077 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
6078 hs->buffering.total_bytes_buffered ) )
6079 {
6080 if( recv_msg_seq_offset > 0 )
6081 {
6082 /* If we can't buffer a future message because
6083 * of space limitations -- ignore. */
6084 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",
6085 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6086 (unsigned) hs->buffering.total_bytes_buffered ) );
6087 goto exit;
6088 }
Hanno Beckere1801392018-08-21 16:51:05 +01006089 else
6090 {
6091 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",
6092 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6093 (unsigned) hs->buffering.total_bytes_buffered ) );
6094 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006095
Hanno Beckera02b0b42018-08-21 17:20:27 +01006096 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006097 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01006098 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",
6099 (unsigned) msg_len,
6100 (unsigned) reassembly_buf_sz,
6101 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01006102 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006103 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6104 goto exit;
6105 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006106 }
6107
6108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
6109 msg_len ) );
6110
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006111 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
6112 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01006113 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01006114 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01006115 goto exit;
6116 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006117 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006118
6119 /* Prepare final header: copy msg_type, length and message_seq,
6120 * then add standardised fragment_offset and fragment_length */
Teppo Järvelin91d79382019-10-02 09:09:31 +03006121 mbedtls_platform_memcpy( hs_buf->data, ssl->in_msg, 6 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02006122 mbedtls_platform_memset( hs_buf->data + 6, 0, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006123 mbedtls_platform_memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
Hanno Becker37f95322018-08-16 13:55:32 +01006124
6125 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01006126
6127 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006128 }
6129 else
6130 {
6131 /* Make sure msg_type and length are consistent */
Teppo Järvelin0efac532019-10-04 13:21:08 +03006132 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 +01006133 {
6134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6135 /* Ignore */
6136 goto exit;
6137 }
6138 }
6139
Hanno Becker4422bbb2018-08-20 09:40:19 +01006140 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006141 {
6142 size_t frag_len, frag_off;
6143 unsigned char * const msg = hs_buf->data + 12;
6144
6145 /*
6146 * Check and copy current fragment
6147 */
6148
6149 /* Validation of header fields already done in
6150 * mbedtls_ssl_prepare_handshake_record(). */
6151 frag_off = ssl_get_hs_frag_off( ssl );
6152 frag_len = ssl_get_hs_frag_len( ssl );
6153
6154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6155 frag_off, frag_len ) );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006156 mbedtls_platform_memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
Hanno Becker37f95322018-08-16 13:55:32 +01006157
6158 if( hs_buf->is_fragmented )
6159 {
6160 unsigned char * const bitmask = msg + msg_len;
6161 ssl_bitmask_set( bitmask, frag_off, frag_len );
6162 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6163 msg_len ) == 0 );
6164 }
6165 else
6166 {
6167 hs_buf->is_complete = 1;
6168 }
6169
6170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6171 hs_buf->is_complete ? "" : "not yet " ) );
6172 }
6173
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006174 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006175 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006176
6177 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006178 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006179 break;
6180 }
6181
6182exit:
6183
6184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6185 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006186}
6187#endif /* MBEDTLS_SSL_PROTO_DTLS */
6188
Hanno Becker1097b342018-08-15 14:09:41 +01006189static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006190{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006191 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006192 * Consume last content-layer message and potentially
6193 * update in_msglen which keeps track of the contents'
6194 * consumption state.
6195 *
6196 * (1) Handshake messages:
6197 * Remove last handshake message, move content
6198 * and adapt in_msglen.
6199 *
6200 * (2) Alert messages:
6201 * Consume whole record content, in_msglen = 0.
6202 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006203 * (3) Change cipher spec:
6204 * Consume whole record content, in_msglen = 0.
6205 *
6206 * (4) Application data:
6207 * Don't do anything - the record layer provides
6208 * the application data as a stream transport
6209 * and consumes through mbedtls_ssl_read only.
6210 *
6211 */
6212
6213 /* Case (1): Handshake messages */
6214 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006215 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006216 /* Hard assertion to be sure that no application data
6217 * is in flight, as corrupting ssl->in_msglen during
6218 * ssl->in_offt != NULL is fatal. */
6219 if( ssl->in_offt != NULL )
6220 {
6221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6222 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6223 }
6224
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006225 /*
6226 * Get next Handshake message in the current record
6227 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006228
Hanno Becker4a810fb2017-05-24 16:27:30 +01006229 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006230 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006231 * current handshake content: If DTLS handshake
6232 * fragmentation is used, that's the fragment
6233 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006234 * size here is faulty and should be changed at
6235 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006236 * (2) While it doesn't seem to cause problems, one
6237 * has to be very careful not to assume that in_hslen
6238 * is always <= in_msglen in a sensible communication.
6239 * Again, it's wrong for DTLS handshake fragmentation.
6240 * The following check is therefore mandatory, and
6241 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006242 * Additionally, ssl->in_hslen might be arbitrarily out of
6243 * bounds after handling a DTLS message with an unexpected
6244 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006245 */
6246 if( ssl->in_hslen < ssl->in_msglen )
6247 {
6248 ssl->in_msglen -= ssl->in_hslen;
6249 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6250 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006251
Hanno Becker4a810fb2017-05-24 16:27:30 +01006252 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6253 ssl->in_msg, ssl->in_msglen );
6254 }
6255 else
6256 {
6257 ssl->in_msglen = 0;
6258 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006259
Hanno Becker4a810fb2017-05-24 16:27:30 +01006260 ssl->in_hslen = 0;
6261 }
6262 /* Case (4): Application data */
6263 else if( ssl->in_offt != NULL )
6264 {
6265 return( 0 );
6266 }
6267 /* Everything else (CCS & Alerts) */
6268 else
6269 {
6270 ssl->in_msglen = 0;
6271 }
6272
Hanno Becker1097b342018-08-15 14:09:41 +01006273 return( 0 );
6274}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006275
Hanno Beckere74d5562018-08-15 14:26:08 +01006276static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6277{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006278 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006279 return( 1 );
6280
6281 return( 0 );
6282}
6283
Hanno Becker5f066e72018-08-16 14:56:31 +01006284#if defined(MBEDTLS_SSL_PROTO_DTLS)
6285
6286static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6287{
6288 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6289 if( hs == NULL )
6290 return;
6291
Hanno Becker01315ea2018-08-21 17:22:17 +01006292 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006293 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006294 hs->buffering.total_bytes_buffered -=
6295 hs->buffering.future_record.len;
6296
6297 mbedtls_free( hs->buffering.future_record.data );
6298 hs->buffering.future_record.data = NULL;
6299 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006300}
6301
6302static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6303{
6304 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6305 unsigned char * rec;
6306 size_t rec_len;
6307 unsigned rec_epoch;
6308
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006309 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006310 return( 0 );
6311
6312 if( hs == NULL )
6313 return( 0 );
6314
Hanno Becker5f066e72018-08-16 14:56:31 +01006315 rec = hs->buffering.future_record.data;
6316 rec_len = hs->buffering.future_record.len;
6317 rec_epoch = hs->buffering.future_record.epoch;
6318
6319 if( rec == NULL )
6320 return( 0 );
6321
Hanno Becker4cb782d2018-08-20 11:19:05 +01006322 /* Only consider loading future records if the
6323 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006324 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006325 return( 0 );
6326
Hanno Becker5f066e72018-08-16 14:56:31 +01006327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6328
6329 if( rec_epoch != ssl->in_epoch )
6330 {
6331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6332 goto exit;
6333 }
6334
6335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6336
6337 /* Double-check that the record is not too large */
6338 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6339 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6340 {
6341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6342 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6343 }
6344
Teppo Järvelin91d79382019-10-02 09:09:31 +03006345 mbedtls_platform_memcpy( ssl->in_hdr, rec, rec_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006346 ssl->in_left = rec_len;
6347 ssl->next_record_offset = 0;
6348
6349 ssl_free_buffered_record( ssl );
6350
6351exit:
6352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6353 return( 0 );
6354}
6355
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006356static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6357 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006358{
6359 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006360
6361 /* Don't buffer future records outside handshakes. */
6362 if( hs == NULL )
6363 return( 0 );
6364
6365 /* Only buffer handshake records (we are only interested
6366 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006367 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006368 return( 0 );
6369
6370 /* Don't buffer more than one future epoch record. */
6371 if( hs->buffering.future_record.data != NULL )
6372 return( 0 );
6373
Hanno Becker01315ea2018-08-21 17:22:17 +01006374 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006375 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006376 hs->buffering.total_bytes_buffered ) )
6377 {
6378 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 +01006379 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006380 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006381 return( 0 );
6382 }
6383
Hanno Becker5f066e72018-08-16 14:56:31 +01006384 /* Buffer record */
6385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6386 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006387 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006388
6389 /* ssl_parse_record_header() only considers records
6390 * of the next epoch as candidates for buffering. */
6391 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006392 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006393
6394 hs->buffering.future_record.data =
6395 mbedtls_calloc( 1, hs->buffering.future_record.len );
6396 if( hs->buffering.future_record.data == NULL )
6397 {
6398 /* If we run out of RAM trying to buffer a
6399 * record from the next epoch, just ignore. */
6400 return( 0 );
6401 }
6402
Teppo Järvelin91d79382019-10-02 09:09:31 +03006403 mbedtls_platform_memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006404
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006405 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006406 return( 0 );
6407}
6408
6409#endif /* MBEDTLS_SSL_PROTO_DTLS */
6410
Hanno Beckere74d5562018-08-15 14:26:08 +01006411static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006412{
6413 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006414 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006415
Hanno Becker5f066e72018-08-16 14:56:31 +01006416#if defined(MBEDTLS_SSL_PROTO_DTLS)
6417 /* We might have buffered a future record; if so,
6418 * and if the epoch matches now, load it.
6419 * On success, this call will set ssl->in_left to
6420 * the length of the buffered record, so that
6421 * the calls to ssl_fetch_input() below will
6422 * essentially be no-ops. */
6423 ret = ssl_load_buffered_record( ssl );
6424 if( ret != 0 )
6425 return( ret );
6426#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006427
Hanno Becker8b09b732019-05-08 12:03:28 +01006428 /* Ensure that we have enough space available for the default form
6429 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6430 * with no space for CIDs counted in). */
6431 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6432 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006435 return( ret );
6436 }
6437
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006438 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6439 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006441#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006442 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006443 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006444 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6445 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006446 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006447 if( ret != 0 )
6448 return( ret );
6449
6450 /* Fall through to handling of unexpected records */
6451 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6452 }
6453
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006454 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6455 {
Hanno Becker87b56262019-07-10 14:37:41 +01006456#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006457 /* Reset in pointers to default state for TLS/DTLS records,
6458 * assuming no CID and no offset between record content and
6459 * record plaintext. */
6460 ssl_update_in_pointers( ssl );
6461
Hanno Becker69412452019-07-12 08:33:49 +01006462 /* Setup internal message pointers from record structure. */
6463 ssl->in_msgtype = rec.type;
6464#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6465 ssl->in_len = ssl->in_cid + rec.cid_len;
6466#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006467 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006468 ssl->in_msglen = rec.data_len;
6469
Hanno Becker87b56262019-07-10 14:37:41 +01006470 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02006471 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker87b56262019-07-10 14:37:41 +01006472 if( ret != 0 )
6473 return( ret );
6474#endif
6475
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006476 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006477 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006478
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6480 "(header)" ) );
6481 }
6482 else
6483 {
6484 /* Skip invalid record and the rest of the datagram */
6485 ssl->next_record_offset = 0;
6486 ssl->in_left = 0;
6487
6488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6489 "(header)" ) );
6490 }
6491
6492 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006493 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006494 }
Hanno Becker87b56262019-07-10 14:37:41 +01006495 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006496#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006497 {
6498 return( ret );
6499 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006500 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006502#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006503 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006504 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006505 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006506 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006507 if( ssl->next_record_offset < ssl->in_left )
6508 {
6509 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6510 }
6511 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006512 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006513#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006514#if defined(MBEDTLS_SSL_PROTO_TLS)
6515 {
Hanno Beckere0452772019-07-10 17:12:07 +01006516 /*
6517 * Fetch record contents from underlying transport.
6518 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006519 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006520 if( ret != 0 )
6521 {
6522 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6523 return( ret );
6524 }
6525
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006526 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006527 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006528#endif /* MBEDTLS_SSL_PROTO_TLS */
6529
6530 /*
6531 * Decrypt record contents.
6532 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006533
Hanno Beckera89610a2019-07-11 13:07:45 +01006534 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006537 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006538 {
6539 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006540 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006541 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006542 /* Except when waiting for Finished as a bad mac here
6543 * probably means something went wrong in the handshake
6544 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6545 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6546 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6547 {
6548#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6549 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6550 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006551 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006552 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6553 }
6554#endif
6555 return( ret );
6556 }
6557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006559 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6560 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6563 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006564 }
6565#endif
6566
Hanno Becker4a810fb2017-05-24 16:27:30 +01006567 /* As above, invalid records cause
6568 * dismissal of the whole datagram. */
6569
6570 ssl->next_record_offset = 0;
6571 ssl->in_left = 0;
6572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006574 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006575 }
6576
6577 return( ret );
6578 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006579 MBEDTLS_SSL_TRANSPORT_ELSE
6580#endif /* MBEDTLS_SSL_PROTO_DTLS */
6581#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006582 {
6583 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006584#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6585 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006586 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006587 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006588 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006589 }
6590#endif
6591 return( ret );
6592 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006593#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006594 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006595
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006596
6597 /* Reset in pointers to default state for TLS/DTLS records,
6598 * assuming no CID and no offset between record content and
6599 * record plaintext. */
6600 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006601#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6602 ssl->in_len = ssl->in_cid + rec.cid_len;
6603#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006604 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006605
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006606 /* The record content type may change during decryption,
6607 * so re-read it. */
6608 ssl->in_msgtype = rec.type;
6609 /* Also update the input buffer, because unfortunately
6610 * the server-side ssl_parse_client_hello() reparses the
6611 * record header when receiving a ClientHello initiating
6612 * a renegotiation. */
6613 ssl->in_hdr[0] = rec.type;
6614 ssl->in_msg = rec.buf + rec.data_offset;
6615 ssl->in_msglen = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006616 (void)mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len );
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006617
Manuel Pégourié-Gonnardae48d862020-01-03 12:18:49 +01006618#if defined(MBEDTLS_ZLIB_SUPPORT)
6619 if( ssl->transform_in != NULL &&
6620 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
6621 {
6622 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
6623 {
6624 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
6625 return( ret );
6626 }
6627
6628 /* Check actual (decompress) record content length against
6629 * configured maximum. */
6630 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
6631 {
6632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
6633 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6634 }
6635 }
6636#endif /* MBEDTLS_ZLIB_SUPPORT */
6637
Simon Butcher99000142016-10-13 17:21:01 +01006638 return( 0 );
6639}
6640
6641int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6642{
6643 int ret;
6644
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006645 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006646 * Handle particular types of records
6647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006649 {
Simon Butcher99000142016-10-13 17:21:01 +01006650 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6651 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006652 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006653 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006654 }
6655
Hanno Beckere678eaa2018-08-21 14:57:46 +01006656 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006657 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006658 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006659 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6661 ssl->in_msglen ) );
6662 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006663 }
6664
Hanno Beckere678eaa2018-08-21 14:57:46 +01006665 if( ssl->in_msg[0] != 1 )
6666 {
6667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6668 ssl->in_msg[0] ) );
6669 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6670 }
6671
6672#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006673 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006674 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6675 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6676 {
6677 if( ssl->handshake == NULL )
6678 {
6679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6680 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6681 }
6682
6683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6684 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6685 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006686#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006687 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006689 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006690 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006691 if( ssl->in_msglen != 2 )
6692 {
6693 /* Note: Standard allows for more than one 2 byte alert
6694 to be packed in a single message, but Mbed TLS doesn't
6695 currently support this. */
6696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6697 ssl->in_msglen ) );
6698 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6699 }
6700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006702 ssl->in_msg[0], ssl->in_msg[1] ) );
6703
6704 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006705 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006710 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006712 }
6713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6715 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6718 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006719 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006720
6721#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6722 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6723 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6724 {
Hanno Becker90333da2017-10-10 11:27:13 +01006725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006726 /* Will be handled when trying to parse ServerHello */
6727 return( 0 );
6728 }
6729#endif
6730
6731#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006732 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006733 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6734 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006735 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6736 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6737 {
6738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6739 /* Will be handled in mbedtls_ssl_parse_certificate() */
6740 return( 0 );
6741 }
6742#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6743
6744 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006745 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006746 }
6747
Hanno Beckerc76c6192017-06-06 10:03:17 +01006748#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006749 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006750 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006751 /* Drop unexpected ApplicationData records,
6752 * except at the beginning of renegotiations */
6753 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6754 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6755#if defined(MBEDTLS_SSL_RENEGOTIATION)
6756 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6757 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006758#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006759 )
6760 {
6761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6762 return( MBEDTLS_ERR_SSL_NON_FATAL );
6763 }
6764
6765 if( ssl->handshake != NULL &&
6766 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6767 {
6768 ssl_handshake_wrapup_free_hs_transform( ssl );
6769 }
6770 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006771#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006772
Paul Bakker5121ce52009-01-03 21:22:43 +00006773 return( 0 );
6774}
6775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006777{
6778 int ret;
6779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006780 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6781 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6782 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006783 {
6784 return( ret );
6785 }
6786
6787 return( 0 );
6788}
6789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006791 unsigned char level,
6792 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006793{
6794 int ret;
6795
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006796 if( ssl == NULL || ssl->conf == NULL )
6797 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006800 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006802 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006803 ssl->out_msglen = 2;
6804 ssl->out_msg[0] = level;
6805 ssl->out_msg[1] = message;
6806
Hanno Becker67bc7c32018-08-06 11:33:50 +01006807 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006810 return( ret );
6811 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006813
6814 return( 0 );
6815}
6816
Hanno Becker17572472019-02-08 07:19:04 +00006817#if defined(MBEDTLS_X509_CRT_PARSE_C)
6818static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6819{
6820#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6821 if( session->peer_cert != NULL )
6822 {
6823 mbedtls_x509_crt_free( session->peer_cert );
6824 mbedtls_free( session->peer_cert );
6825 session->peer_cert = NULL;
6826 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006827#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006828 if( session->peer_cert_digest != NULL )
6829 {
6830 /* Zeroization is not necessary. */
6831 mbedtls_free( session->peer_cert_digest );
6832 session->peer_cert_digest = NULL;
6833 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6834 session->peer_cert_digest_len = 0;
6835 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006836#else
6837 ((void) session);
6838#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006839}
6840#endif /* MBEDTLS_X509_CRT_PARSE_C */
6841
Paul Bakker5121ce52009-01-03 21:22:43 +00006842/*
6843 * Handshake functions
6844 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006845#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006846/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006847int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006848{
Hanno Beckerdf645962019-06-26 13:02:22 +01006849 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6850 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006853
Hanno Becker5097cba2019-02-05 13:36:46 +00006854 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006857 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6858 {
6859 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6860 }
6861 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6862 {
6863 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6864 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006865 else
6866 {
6867 ssl->state = MBEDTLS_SSL_INVALID;
6868 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006869 return( 0 );
6870 }
6871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6873 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006874}
6875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006876int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006877{
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, ( "=> parse 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 parse 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 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6902 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006903}
Gilles Peskinef9828522017-05-03 12:28:43 +02006904
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006905#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006906/* Some certificate support -> implement write and parse */
6907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006909{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006911 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006913 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6914 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006917
Hanno Becker5097cba2019-02-05 13:36:46 +00006918 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006921 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6922 {
6923 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6924 }
6925 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6926 {
6927 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6928 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006929 else
6930 {
6931 ssl->state = MBEDTLS_SSL_INVALID;
6932 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006933 return( 0 );
6934 }
6935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006936#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006937 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6938 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006939 {
6940 if( ssl->client_auth == 0 )
6941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006943 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6944 {
6945 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6946 }
6947 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6948 {
6949 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6950 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006951 else
6952 {
6953 ssl->state = MBEDTLS_SSL_INVALID;
6954 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006955 return( 0 );
6956 }
6957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006958#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006959 /*
6960 * If using SSLv3 and got no cert, send an Alert message
6961 * (otherwise an empty Certificate message will be sent).
6962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006963 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006964 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006965 {
6966 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006967 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6968 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6969 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006972 goto write_msg;
6973 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006975 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006976#endif /* MBEDTLS_SSL_CLI_C */
6977#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006978 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006980 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6983 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006984 }
6985 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006986#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006988 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006989
6990 /*
6991 * 0 . 0 handshake type
6992 * 1 . 3 handshake length
6993 * 4 . 6 length of all certs
6994 * 7 . 9 length of cert. 1
6995 * 10 . n-1 peer certificate
6996 * n . n+2 length of cert. 2
6997 * n+3 . ... upper level cert, etc.
6998 */
6999 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007000 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007001
Paul Bakker29087132010-03-21 21:03:34 +00007002 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007003 {
7004 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10007005 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00007006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10007008 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007009 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007010 }
7011
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03007012 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007013
Teppo Järvelin91d79382019-10-02 09:09:31 +03007014 i += 3; mbedtls_platform_memcpy( ssl->out_msg + i, crt->raw.p, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007015 i += n; crt = crt->next;
7016 }
7017
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03007018 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007019
7020 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007021 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7022 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007023
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007024#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007025write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007027
Jarno Lamsa2b205162019-11-12 15:36:21 +02007028 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7029 {
7030 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7031 }
7032 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7033 {
7034 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7035 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007036 else
7037 {
7038 ssl->state = MBEDTLS_SSL_INVALID;
7039 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007040
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007041 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007042 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007044 return( ret );
7045 }
7046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007048
Paul Bakkered27a042013-04-18 22:46:23 +02007049 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007050}
7051
Hanno Becker285ff0c2019-01-31 07:44:03 +00007052#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00007053
7054#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00007055static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7056 unsigned char *crt_buf,
7057 size_t crt_buf_len )
7058{
7059 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7060
7061 if( peer_crt == NULL )
7062 return( -1 );
7063
7064 if( peer_crt->raw.len != crt_buf_len )
7065 return( -1 );
7066
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007067 return( mbedtls_platform_memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007068}
Hanno Becker5882dd02019-06-06 16:25:57 +01007069#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00007070static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7071 unsigned char *crt_buf,
7072 size_t crt_buf_len )
7073{
7074 int ret;
7075 unsigned char const * const peer_cert_digest =
7076 ssl->session->peer_cert_digest;
7077 mbedtls_md_type_t const peer_cert_digest_type =
7078 ssl->session->peer_cert_digest_type;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007079 mbedtls_md_handle_t digest_info =
Hanno Beckerdf759382019-02-05 17:02:46 +00007080 mbedtls_md_info_from_type( peer_cert_digest_type );
7081 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7082 size_t digest_len;
7083
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007084 if( peer_cert_digest == NULL ||
7085 digest_info == MBEDTLS_MD_INVALID_HANDLE )
7086 {
Hanno Beckerdf759382019-02-05 17:02:46 +00007087 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007088 }
Hanno Beckerdf759382019-02-05 17:02:46 +00007089
7090 digest_len = mbedtls_md_get_size( digest_info );
7091 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
7092 return( -1 );
7093
7094 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
7095 if( ret != 0 )
7096 return( -1 );
7097
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007098 return( mbedtls_platform_memcmp( tmp_digest, peer_cert_digest, digest_len ) );
Hanno Beckerdf759382019-02-05 17:02:46 +00007099}
Hanno Becker5882dd02019-06-06 16:25:57 +01007100#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00007101#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00007102
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007103/*
7104 * Once the certificate message is read, parse it into a cert chain and
7105 * perform basic checks, but leave actual verification to the caller
7106 */
Hanno Becker35e41772019-02-05 15:37:23 +00007107static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
7108 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00007109{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007110 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00007111#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7112 int crt_cnt=0;
7113#endif
Paul Bakker23986e52011-04-24 08:57:21 +00007114 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02007115 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00007116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007120 mbedtls_ssl_pend_fatal_alert( ssl,
7121 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007122 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007123 }
7124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
7126 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007129 mbedtls_ssl_pend_fatal_alert( ssl,
7130 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007132 }
7133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007135
Paul Bakker5121ce52009-01-03 21:22:43 +00007136 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007137 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00007138 */
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
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007141 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007142 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007143 {
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 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7151 i += 3;
7152
Hanno Becker33c3dc82019-01-30 14:46:46 +00007153 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007154 while( i < ssl->in_hslen )
7155 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007156 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02007157 if ( i + 3 > ssl->in_hslen ) {
7158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007159 mbedtls_ssl_pend_fatal_alert( ssl,
7160 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02007161 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
7162 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007163 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7164 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007165 if( ssl->in_msg[i] != 0 )
7166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007168 mbedtls_ssl_pend_fatal_alert( ssl,
7169 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007171 }
7172
Hanno Becker33c3dc82019-01-30 14:46:46 +00007173 /* Read length of the next CRT in the chain. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007174 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007175 i += 3;
7176
7177 if( n < 128 || i + n > ssl->in_hslen )
7178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007180 mbedtls_ssl_pend_fatal_alert( ssl,
7181 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007183 }
7184
Hanno Becker33c3dc82019-01-30 14:46:46 +00007185 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00007186#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7187 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01007188 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7189 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00007190 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007191 {
Hanno Becker68b856d2019-02-08 14:00:04 +00007192 /* During client-side renegotiation, check that the server's
7193 * end-CRTs hasn't changed compared to the initial handshake,
7194 * mitigating the triple handshake attack. On success, reuse
7195 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00007196 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7197 if( ssl_check_peer_crt_unchanged( ssl,
7198 &ssl->in_msg[i],
7199 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007200 {
Hanno Becker35e41772019-02-05 15:37:23 +00007201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007202 mbedtls_ssl_pend_fatal_alert( ssl,
7203 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007204 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007205 }
Hanno Becker35e41772019-02-05 15:37:23 +00007206
7207 /* Now we can safely free the original chain. */
7208 ssl_clear_peer_cert( ssl->session );
7209 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007210#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7211
Hanno Becker33c3dc82019-01-30 14:46:46 +00007212 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007213#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007214 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007215#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007216 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007217 * it in-place from the input buffer instead of making a copy. */
7218 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7219#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007220 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007221 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007222 case 0: /*ok*/
7223 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7224 /* Ignore certificate with an unknown algorithm: maybe a
7225 prior certificate was already trusted. */
7226 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007227
Hanno Becker33c3dc82019-01-30 14:46:46 +00007228 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7229 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7230 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007231
Hanno Becker33c3dc82019-01-30 14:46:46 +00007232 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7233 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7234 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007235
Hanno Becker33c3dc82019-01-30 14:46:46 +00007236 default:
7237 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7238 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007239 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007240 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7241 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007242 }
7243
7244 i += n;
7245 }
7246
Hanno Becker35e41772019-02-05 15:37:23 +00007247 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007248 return( 0 );
7249}
7250
Hanno Beckerb8a08572019-02-05 12:49:06 +00007251#if defined(MBEDTLS_SSL_SRV_C)
7252static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7253{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007254 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007255 return( -1 );
7256
7257#if defined(MBEDTLS_SSL_PROTO_SSL3)
7258 /*
7259 * Check if the client sent an empty certificate
7260 */
Hanno Becker2881d802019-05-22 14:44:53 +01007261 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007262 {
7263 if( ssl->in_msglen == 2 &&
7264 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7265 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7266 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7267 {
7268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7269 return( 0 );
7270 }
7271
7272 return( -1 );
7273 }
7274#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7275
7276#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7277 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7278 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7279 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7280 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Teppo Järvelin0efac532019-10-04 13:21:08 +03007281 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 +00007282 {
7283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7284 return( 0 );
7285 }
7286
Hanno Beckerb8a08572019-02-05 12:49:06 +00007287#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7288 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007289
7290 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007291}
7292#endif /* MBEDTLS_SSL_SRV_C */
7293
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007294/* Check if a certificate message is expected.
7295 * Return either
7296 * - SSL_CERTIFICATE_EXPECTED, or
7297 * - SSL_CERTIFICATE_SKIP
7298 * indicating whether a Certificate message is expected or not.
7299 */
7300#define SSL_CERTIFICATE_EXPECTED 0
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007301#define SSL_CERTIFICATE_SKIP 0xff
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007302static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7303 int authmode )
7304{
Hanno Becker473f98f2019-06-26 10:27:32 +01007305 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007306 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007307
7308 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7309 return( SSL_CERTIFICATE_SKIP );
7310
7311#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007312 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007313 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007314 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7315 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7316 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007317 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007318 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007319
7320 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7321 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007322 ssl->session_negotiate->verify_result =
7323 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7324 return( SSL_CERTIFICATE_SKIP );
7325 }
7326 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007327#else
7328 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007329#endif /* MBEDTLS_SSL_SRV_C */
7330
7331 return( SSL_CERTIFICATE_EXPECTED );
7332}
7333
Hanno Becker3cf50612019-02-05 14:36:34 +00007334static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007335 volatile int authmode,
Hanno Becker3cf50612019-02-05 14:36:34 +00007336 mbedtls_x509_crt *chain,
7337 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007338{
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007339 volatile int verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7340 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7341 volatile int flow_counter = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01007342 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007343 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007344 mbedtls_x509_crt *ca_chain;
7345 mbedtls_x509_crl *ca_crl;
7346
7347 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
Jarno Lamsae1621d42019-12-19 08:58:56 +02007348 {
Hanno Becker3cf50612019-02-05 14:36:34 +00007349 return( 0 );
Jarno Lamsae1621d42019-12-19 08:58:56 +02007350 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007351
Jarno Lamsae1621d42019-12-19 08:58:56 +02007352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Hanno Becker3cf50612019-02-05 14:36:34 +00007353#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7354 if( ssl->handshake->sni_ca_chain != NULL )
7355 {
7356 ca_chain = ssl->handshake->sni_ca_chain;
7357 ca_crl = ssl->handshake->sni_ca_crl;
7358 }
7359 else
7360#endif
7361 {
7362 ca_chain = ssl->conf->ca_chain;
7363 ca_crl = ssl->conf->ca_crl;
7364 }
7365
7366 /*
7367 * Main check: verify certificate
7368 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007369 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007370 chain,
7371 ca_chain, ca_crl,
7372 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007373#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007374 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007375#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007376 &ssl->session_negotiate->verify_result,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01007377#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
7378 ssl->conf->f_vrfy, ssl->conf->p_vrfy,
7379#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
7380 rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007381
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007382 if( verify_ret == 0 )
7383 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007384 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007385 if( verify_ret == 0 )
7386 {
7387 flow_counter++;
7388 }
7389 else
7390 {
7391 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7392 }
7393 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007394 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007395 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007396 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007397 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007398 }
7399
7400#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007401 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007402 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7403#endif
7404
7405 /*
7406 * Secondary checks: always done, but change 'ret' only if it was 0
7407 */
7408
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007409#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007410 {
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007411#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007412 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007413#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007414 mbedtls_pk_context *pk;
7415 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7416 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007417 {
7418 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007419 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007420 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007421
7422 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007423 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007424 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007425 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007426 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007427
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007428 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007429#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007430
7431 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007432 {
7433 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7434
7435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007436 if( verify_ret == 0 )
7437 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007438 flow_counter++;
7439 }
7440 if( ret == 0 )
7441 {
7442 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007443 }
7444 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007445#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007446
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007447 ret = mbedtls_ssl_check_cert_usage( chain,
Hanno Becker3cf50612019-02-05 14:36:34 +00007448 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007449 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7450 MBEDTLS_SSL_IS_CLIENT ),
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007451 &ssl->session_negotiate->verify_result );
7452 if( ret == 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007453 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007454 flow_counter++;
7455 }
7456 else
7457 {
7458 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007460 if( verify_ret == 0 )
7461 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007462 }
7463
7464 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7465 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7466 * with details encoded in the verification flags. All other kinds
7467 * of error codes, including those from the user provided f_vrfy
7468 * functions, are treated as fatal and lead to a failure of
7469 * ssl_parse_certificate even if verification was optional. */
7470 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007471 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7472 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007473 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007474 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007475 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7476 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7477 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7478 {
7479 verify_ret = 0;
7480 flow_counter++;
7481 }
7482 else
7483 {
7484 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7485 }
7486 } else {
7487 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007488 }
7489
7490 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7491 {
7492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007493 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007494 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007495 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007496 else
7497 {
7498 flow_counter++;
7499 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007500
Hanno Becker8c13ee62019-02-26 16:48:17 +00007501 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007502 {
7503 uint8_t alert;
7504
7505 /* The certificate may have been rejected for several reasons.
7506 Pick one and send the corresponding alert. Which alert to send
7507 may be a subject of debate in some cases. */
7508 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7509 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7510 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7511 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7512 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7513 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7514 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7515 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7516 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7517 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7518 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7519 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7520 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7521 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7522 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7523 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7524 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7525 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7526 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7527 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7528 else
7529 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007530 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007531 }
7532
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007533 if( verify_ret == 0 &&
7534#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7535 flow_counter == 5 )
7536#else
7537 flow_counter == 4 )
7538#endif
7539 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007540 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007541 if( verify_ret == 0 &&
7542#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7543 flow_counter == 5 )
7544#else
7545 flow_counter == 4 )
7546#endif
7547 {
Jarno Lamsae1621d42019-12-19 08:58:56 +02007548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER AUTHENTICATED" ) );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007549 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
7550 }
7551 else
7552 {
7553 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7554 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007555 } else {
7556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER NOT AUTHENTICATED, %d", flow_counter));
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007557 }
7558
Hanno Becker3cf50612019-02-05 14:36:34 +00007559#if defined(MBEDTLS_DEBUG_C)
7560 if( ssl->session_negotiate->verify_result != 0 )
7561 {
7562 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7563 ssl->session_negotiate->verify_result ) );
7564 }
7565 else
7566 {
7567 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7568 }
7569#endif /* MBEDTLS_DEBUG_C */
7570
Hanno Becker8c13ee62019-02-26 16:48:17 +00007571 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007572}
7573
Hanno Becker34106f62019-02-08 14:59:05 +00007574#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007575
7576#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007577static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7578 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007579{
7580 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007581 /* Remember digest of the peer's end-CRT. */
7582 ssl->session_negotiate->peer_cert_digest =
7583 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7584 if( ssl->session_negotiate->peer_cert_digest == NULL )
7585 {
7586 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7587 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007588 mbedtls_ssl_pend_fatal_alert( ssl,
7589 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007590
7591 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7592 }
7593
7594 ret = mbedtls_md( mbedtls_md_info_from_type(
7595 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7596 start, len,
7597 ssl->session_negotiate->peer_cert_digest );
7598
7599 ssl->session_negotiate->peer_cert_digest_type =
7600 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7601 ssl->session_negotiate->peer_cert_digest_len =
7602 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7603
7604 return( ret );
7605}
Hanno Becker5882dd02019-06-06 16:25:57 +01007606#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007607
7608static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7609 unsigned char *start, size_t len )
7610{
7611 unsigned char *end = start + len;
7612 int ret;
7613
7614 /* Make a copy of the peer's raw public key. */
7615 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7616 ret = mbedtls_pk_parse_subpubkey( &start, end,
7617 &ssl->handshake->peer_pubkey );
7618 if( ret != 0 )
7619 {
7620 /* We should have parsed the public key before. */
7621 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7622 }
7623
Manuel Pégourié-Gonnard2829bbf2019-09-19 10:45:14 +02007624 ssl->handshake->got_peer_pubkey = 1;
Hanno Becker34106f62019-02-08 14:59:05 +00007625 return( 0 );
7626}
7627#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7628
Hanno Becker3cf50612019-02-05 14:36:34 +00007629int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7630{
7631 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007632 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007633#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7634 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7635 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007636 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007637#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007638 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007639#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007640 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007641 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007642
7643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7644
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007645 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7646 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007647 {
7648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007649 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007650 }
7651
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007652#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7653 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007654 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007655 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007656 chain = ssl->handshake->ecrs_peer_cert;
7657 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007658 goto crt_verify;
7659 }
7660#endif
7661
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007662 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007663 {
7664 /* mbedtls_ssl_read_record may have sent an alert already. We
7665 let it decide whether to alert. */
7666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007667 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007668 }
7669
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007670#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007671 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7672 {
7673 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007674
Hanno Beckerb8a08572019-02-05 12:49:06 +00007675 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007676 ret = 0;
7677 else
7678 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007679
Hanno Becker613d4902019-02-05 13:11:17 +00007680 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007681 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007682#endif /* MBEDTLS_SSL_SRV_C */
7683
Hanno Becker35e41772019-02-05 15:37:23 +00007684 /* Clear existing peer CRT structure in case we tried to
7685 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007686 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007687
7688 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7689 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007690 {
7691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7692 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007693 mbedtls_ssl_pend_fatal_alert( ssl,
7694 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007695
Hanno Beckere4aeb762019-02-05 17:19:52 +00007696 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7697 goto exit;
7698 }
7699 mbedtls_x509_crt_init( chain );
7700
7701 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007702 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007703 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007704
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007705#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7706 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007707 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007708
7709crt_verify:
7710 if( ssl->handshake->ecrs_enabled)
7711 rs_ctx = &ssl->handshake->ecrs_ctx;
7712#endif
7713
Hanno Becker3cf50612019-02-05 14:36:34 +00007714 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007715 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007716 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007717 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007718
Hanno Becker3008d282019-02-05 17:02:28 +00007719#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007720 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007721 size_t pk_len;
7722 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007723
Hanno Becker34106f62019-02-08 14:59:05 +00007724 /* We parse the CRT chain without copying, so
7725 * these pointers point into the input buffer,
7726 * and are hence still valid after freeing the
7727 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007728
Hanno Becker5882dd02019-06-06 16:25:57 +01007729#if defined(MBEDTLS_SSL_RENEGOTIATION)
7730 unsigned char *crt_start;
7731 size_t crt_len;
7732
Hanno Becker34106f62019-02-08 14:59:05 +00007733 crt_start = chain->raw.p;
7734 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007735#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007736
Hanno Becker8c13ee62019-02-26 16:48:17 +00007737 pk_start = chain->cache->pk_raw.p;
7738 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007739
7740 /* Free the CRT structures before computing
7741 * digest and copying the peer's public key. */
7742 mbedtls_x509_crt_free( chain );
7743 mbedtls_free( chain );
7744 chain = NULL;
7745
Hanno Becker5882dd02019-06-06 16:25:57 +01007746#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007747 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007748 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007749 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007750#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007751
Hanno Becker34106f62019-02-08 14:59:05 +00007752 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007753 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007754 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007755 }
Hanno Becker34106f62019-02-08 14:59:05 +00007756#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7757 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007758 ssl->session_negotiate->peer_cert = chain;
7759 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007760#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007763
Hanno Becker613d4902019-02-05 13:11:17 +00007764exit:
7765
Hanno Beckere4aeb762019-02-05 17:19:52 +00007766 if( ret == 0 )
Jarno Lamsa2b205162019-11-12 15:36:21 +02007767 {
7768 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7769 {
7770 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7771 }
7772 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7773 {
7774 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7775 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007776 else
7777 {
7778 ssl->state = MBEDTLS_SSL_INVALID;
7779 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02007780 }
Hanno Beckere4aeb762019-02-05 17:19:52 +00007781
7782#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7783 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7784 {
7785 ssl->handshake->ecrs_peer_cert = chain;
7786 chain = NULL;
7787 }
7788#endif
7789
7790 if( chain != NULL )
7791 {
7792 mbedtls_x509_crt_free( chain );
7793 mbedtls_free( chain );
7794 }
7795
Paul Bakker5121ce52009-01-03 21:22:43 +00007796 return( ret );
7797}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007798#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007800int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007801{
7802 int ret;
7803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007806 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007807 ssl->out_msglen = 1;
7808 ssl->out_msg[0] = 1;
7809
Jarno Lamsa2b205162019-11-12 15:36:21 +02007810 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7811 {
7812 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7813 }
7814 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7815 {
7816 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7817 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007818 else
7819 {
7820 ssl->state = MBEDTLS_SSL_INVALID;
7821 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007822
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007823 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007824 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007825 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007826 return( ret );
7827 }
7828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007830
7831 return( 0 );
7832}
7833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007835{
7836 int ret;
7837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007839
Hanno Becker327c93b2018-08-15 13:56:18 +01007840 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007843 return( ret );
7844 }
7845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007849 mbedtls_ssl_pend_fatal_alert( ssl,
7850 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007851 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007852 }
7853
Hanno Beckere678eaa2018-08-21 14:57:46 +01007854 /* CCS records are only accepted if they have length 1 and content '1',
7855 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007856
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007857 /*
7858 * Switch to our negotiated transform and session parameters for inbound
7859 * data.
7860 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007862 ssl->transform_in = ssl->transform_negotiate;
7863 ssl->session_in = ssl->session_negotiate;
7864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007866 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007869 ssl_dtls_replay_reset( ssl );
7870#endif
7871
7872 /* Increment epoch */
7873 if( ++ssl->in_epoch == 0 )
7874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007876 /* This is highly unlikely to happen for legitimate reasons, so
7877 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007879 }
7880 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007881 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007883#if defined(MBEDTLS_SSL_PROTO_TLS)
7884 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02007885 mbedtls_platform_memset( ssl->in_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007886 }
7887#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007888
Hanno Beckerf5970a02019-05-08 09:38:41 +01007889 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7892 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007897 mbedtls_ssl_pend_fatal_alert( ssl,
7898 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007900 }
7901 }
7902#endif
7903
Jarno Lamsa2b205162019-11-12 15:36:21 +02007904 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7905 {
7906 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7907 }
7908 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7909 {
7910 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7911 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007912 else
7913 {
7914 ssl->state = MBEDTLS_SSL_INVALID;
7915 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007918
7919 return( 0 );
7920}
7921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007923{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007924#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7925 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007926 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007927 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007928#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7930#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007931 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007932#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007933#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007934 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007935#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007937}
7938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007940{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007942
7943 /*
7944 * Free our handshake params
7945 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007946 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007948 ssl->handshake = NULL;
7949
7950 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007951 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007952 */
7953 if( ssl->transform )
7954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007955 mbedtls_ssl_transform_free( ssl->transform );
7956 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007957 }
7958 ssl->transform = ssl->transform_negotiate;
7959 ssl->transform_negotiate = NULL;
7960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007962}
7963
Jarno Lamsae1621d42019-12-19 08:58:56 +02007964int mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007965{
Jarno Lamsae1621d42019-12-19 08:58:56 +02007966 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007967
7968#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Jarno Lamsa015aa442019-12-20 12:09:37 +02007969 volatile const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007970 ? ssl->handshake->sni_authmode
7971 : mbedtls_ssl_conf_get_authmode( ssl->conf );
7972#else
Jarno Lamsa015aa442019-12-20 12:09:37 +02007973 volatile const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007974#endif
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007975#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7976 volatile int crt_expected = SSL_CERTIFICATE_EXPECTED;
7977 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7978#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981#if defined(MBEDTLS_SSL_RENEGOTIATION)
7982 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007984 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007985 ssl->renego_records_seen = 0;
7986 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007987#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007988
7989 /*
7990 * Free the previous session and switch in the current one
7991 */
Paul Bakker0a597072012-09-25 21:55:46 +00007992 if( ssl->session )
7993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007995 /* RFC 7366 3.1: keep the EtM state */
7996 ssl->session_negotiate->encrypt_then_mac =
7997 ssl->session->encrypt_then_mac;
7998#endif
7999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008000 mbedtls_ssl_session_free( ssl->session );
8001 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00008002 }
8003 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00008004 ssl->session_negotiate = NULL;
8005
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02008006#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00008007 /*
8008 * Add cache entry
8009 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008010 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02008011 ssl->session->id_len != 0 &&
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008012 ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_UNSET )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02008013 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008014 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02008016 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02008017#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00008018
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008019 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8020 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8021#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8022 crt_expected == SSL_CERTIFICATE_SKIP )
8023#else
8024 1 )
8025#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008026 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008027 mbedtls_platform_random_delay();
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008028 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8029 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8030#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8031 crt_expected == SSL_CERTIFICATE_SKIP )
8032#else
8033 1 )
8034#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008035 {
8036 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
8037 }
8038 else
8039 {
8040 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8041 goto cleanup;
8042 }
8043 }
8044
Jarno Lamsae1621d42019-12-19 08:58:56 +02008045#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008046 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008047 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008048 mbedtls_platform_random_delay();
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008049 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008050 {
Jarno Lamsa06164052019-12-19 14:40:36 +02008051 /* When doing session resume, no premaster or peer authentication */
Jarno Lamsae1621d42019-12-19 08:58:56 +02008052 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsa06164052019-12-19 14:40:36 +02008053 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008054 }
8055 else
8056 {
8057 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008058 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008059 }
8060 }
8061#endif
8062
8063 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8064 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008065 mbedtls_platform_random_delay();
Jarno Lamsae1621d42019-12-19 08:58:56 +02008066 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8067 {
8068 ret = 0;
8069 }
8070 else
8071 {
8072 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008073 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008074 }
8075 }
8076 else
8077 {
8078 ret = MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008079 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008080 }
8081
Jarno Lamsa06164052019-12-19 14:40:36 +02008082 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8083 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8084 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8085 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008086 mbedtls_platform_random_delay();
Jarno Lamsa06164052019-12-19 14:40:36 +02008087 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8088 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8089 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8090 {
8091 ret = 0;
8092 }
8093 else
8094 {
8095 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8096 goto cleanup;
8097 }
8098 }
8099 else
8100 {
8101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "hello random %d", ssl->handshake->hello_random_set ) );
8102 MBEDTLS_SSL_DEBUG_MSG( 3, ( "key_derivation_done %d", ssl->handshake->key_derivation_done ) );
8103 MBEDTLS_SSL_DEBUG_MSG( 3, ( "premaster_generated %d", ssl->handshake->premaster_generated ) );
8104 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8105 }
8106
8107cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008108#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008109 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008110 ssl->handshake->flight != NULL )
8111 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008112 /* Cancel handshake timer */
8113 ssl_set_timer( ssl, 0 );
8114
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008115 /* Keep last flight around in case we need to resend it:
8116 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008117 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008118 }
8119 else
8120#endif
8121 ssl_handshake_wrapup_free_hs_transform( ssl );
8122
Jarno Lamsa2b205162019-11-12 15:36:21 +02008123 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Paul Bakker48916f92012-09-16 19:57:18 +00008124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008125 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Jarno Lamsae1621d42019-12-19 08:58:56 +02008126 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00008127}
8128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00008130{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008131 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00008132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008134
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008135 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01008136
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008137 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8138 mbedtls_ssl_suite_get_mac(
8139 mbedtls_ssl_ciphersuite_from_id(
8140 mbedtls_ssl_session_get_ciphersuite(
8141 ssl->session_negotiate ) ) ),
8142 ssl, ssl->out_msg + 4,
8143 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008144
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01008145 /*
8146 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8147 * may define some other value. Currently (early 2016), no defined
8148 * ciphersuite does this (and this is unlikely to change as activity has
8149 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8150 */
Hanno Becker2881d802019-05-22 14:44:53 +01008151 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008153#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008154 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008155 mbedtls_platform_memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008156#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008157
Paul Bakker5121ce52009-01-03 21:22:43 +00008158 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008159 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8160 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008161
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008162#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00008163 /*
8164 * In case of session resuming, invert the client and server
8165 * ChangeCipherSpec messages order.
8166 */
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008167 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008169#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008170 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8171 MBEDTLS_SSL_IS_CLIENT )
8172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008174 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008175#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008176#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008177 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8178 MBEDTLS_SSL_IS_SERVER )
8179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008180 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008181 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008182#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008183 }
8184 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008185#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008186 {
8187 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8188 {
8189 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8190 }
8191 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8192 {
8193 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8194 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008195 else
8196 {
8197 ssl->state = MBEDTLS_SSL_INVALID;
8198 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008199 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008200
Paul Bakker48916f92012-09-16 19:57:18 +00008201 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008202 * Switch to our negotiated transform and session parameters for outbound
8203 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008205 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008207#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008208 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008209 {
8210 unsigned char i;
8211
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008212 /* Remember current epoch settings for resending */
8213 ssl->handshake->alt_transform_out = ssl->transform_out;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008214 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008215
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008216 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008217 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008218
8219 /* Increment epoch */
8220 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008221 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008222 break;
8223
8224 /* The loop goes to its end iff the counter is wrapping */
8225 if( i == 0 )
8226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8228 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008229 }
8230 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008231 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008232#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008233#if defined(MBEDTLS_SSL_PROTO_TLS)
8234 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008235 mbedtls_platform_memset( ssl->cur_out_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008236 }
8237#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008238
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008239 ssl->transform_out = ssl->transform_negotiate;
8240 ssl->session_out = ssl->session_negotiate;
8241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008242#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8243 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008245 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008247 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8248 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008249 }
8250 }
8251#endif
8252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008253#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008254 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008255 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008256#endif
8257
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008258 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008259 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008260 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008261 return( ret );
8262 }
8263
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008264#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008265 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008266 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8267 {
8268 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8269 return( ret );
8270 }
8271#endif
8272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008274
8275 return( 0 );
8276}
8277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008278#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008279#define SSL_MAX_HASH_LEN 36
8280#else
8281#define SSL_MAX_HASH_LEN 12
8282#endif
8283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008284int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008285{
Paul Bakker23986e52011-04-24 08:57:21 +00008286 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008287 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008288 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008291
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008292 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8293 mbedtls_ssl_suite_get_mac(
8294 mbedtls_ssl_ciphersuite_from_id(
8295 mbedtls_ssl_session_get_ciphersuite(
8296 ssl->session_negotiate ) ) ),
8297 ssl, buf,
8298 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008299
Hanno Becker327c93b2018-08-15 13:56:18 +01008300 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008302 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008303 return( ret );
8304 }
8305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008306 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008309 mbedtls_ssl_pend_fatal_alert( ssl,
8310 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008311 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008312 }
8313
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008314 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008315#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01008316 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008317 hash_len = 36;
8318 else
8319#endif
8320 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008322 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8323 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008326 mbedtls_ssl_pend_fatal_alert( ssl,
8327 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008328 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008329 }
8330
Teppo Järvelin707ceb82019-10-04 07:49:39 +03008331 if( mbedtls_platform_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008332 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008335 mbedtls_ssl_pend_fatal_alert( ssl,
8336 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008337 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008338 }
8339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008340#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008341 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008342 mbedtls_platform_memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008343#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008344
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008345#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008346 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008348#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008349 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008350 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008351#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008352#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008353 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008355#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008356 }
8357 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008358#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008359 {
8360 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8361 {
8362 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8363 }
8364 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8365 {
8366 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8367 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008368 else
8369 {
8370 ssl->state = MBEDTLS_SSL_INVALID;
8371 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008372 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008374#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008375 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008376 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008377#endif
8378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008380
8381 return( 0 );
8382}
8383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008384static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008385{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008386 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8389 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8390 mbedtls_md5_init( &handshake->fin_md5 );
8391 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008392 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8393 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008394#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008395#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8396#if defined(MBEDTLS_SHA256_C)
8397 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008398 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008399#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008400#if defined(MBEDTLS_SHA512_C)
8401 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008402 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008403#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008404#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008405
Hanno Becker7e5437a2017-04-28 17:15:26 +01008406#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8407 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8408 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8409#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008411#if defined(MBEDTLS_DHM_C)
8412 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008413#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008414#if defined(MBEDTLS_ECDH_C)
8415 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008416#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008417#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008418 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008419#if defined(MBEDTLS_SSL_CLI_C)
8420 handshake->ecjpake_cache = NULL;
8421 handshake->ecjpake_cache_len = 0;
8422#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008423#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008424
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008425#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008426 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008427#endif
8428
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008429#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8430 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8431#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008432
8433#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8434 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8435 mbedtls_pk_init( &handshake->peer_pubkey );
8436#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008437}
8438
Hanno Becker611a83b2018-01-03 14:27:32 +00008439void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008440{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008441 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8444 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008445
Hanno Becker92231322018-01-03 15:32:51 +00008446#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008447 mbedtls_md_init( &transform->md_ctx_enc );
8448 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008449#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008450}
8451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008452void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008453{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008454 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008455}
8456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008457static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008458{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008459 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008460 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008461 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008462 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008463 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008464 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008465 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008466
8467 /*
8468 * Either the pointers are now NULL or cleared properly and can be freed.
8469 * Now allocate missing structures.
8470 */
8471 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008472 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008473 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008474 }
Paul Bakker48916f92012-09-16 19:57:18 +00008475
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008476 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008477 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008478 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008479 }
Paul Bakker48916f92012-09-16 19:57:18 +00008480
Paul Bakker82788fb2014-10-20 13:59:19 +02008481 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008482 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008483 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008484 }
Paul Bakker48916f92012-09-16 19:57:18 +00008485
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008486 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008487 if( ssl->handshake == NULL ||
8488 ssl->transform_negotiate == NULL ||
8489 ssl->session_negotiate == NULL )
8490 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008493 mbedtls_free( ssl->handshake );
8494 mbedtls_free( ssl->transform_negotiate );
8495 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008496
8497 ssl->handshake = NULL;
8498 ssl->transform_negotiate = NULL;
8499 ssl->session_negotiate = NULL;
8500
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008501 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008502 }
8503
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008504 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008505 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008506 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008507 ssl_handshake_params_init( ssl->handshake );
8508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008509#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008510 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008511 {
8512 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008513
Hanno Becker2d9623f2019-06-13 12:07:05 +01008514 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008515 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8516 else
8517 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8518 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008519#endif
8520
Paul Bakker48916f92012-09-16 19:57:18 +00008521 return( 0 );
8522}
8523
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008524#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008525/* Dummy cookie callbacks for defaults */
8526static int ssl_cookie_write_dummy( void *ctx,
8527 unsigned char **p, unsigned char *end,
8528 const unsigned char *cli_id, size_t cli_id_len )
8529{
8530 ((void) ctx);
8531 ((void) p);
8532 ((void) end);
8533 ((void) cli_id);
8534 ((void) cli_id_len);
8535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008536 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008537}
8538
8539static int ssl_cookie_check_dummy( void *ctx,
8540 const unsigned char *cookie, size_t cookie_len,
8541 const unsigned char *cli_id, size_t cli_id_len )
8542{
8543 ((void) ctx);
8544 ((void) cookie);
8545 ((void) cookie_len);
8546 ((void) cli_id);
8547 ((void) cli_id_len);
8548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008549 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008550}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008551#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008552
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008553/* Once ssl->out_hdr as the address of the beginning of the
8554 * next outgoing record is set, deduce the other pointers.
8555 *
8556 * Note: For TLS, we save the implicit record sequence number
8557 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8558 * and the caller has to make sure there's space for this.
8559 */
8560
8561static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8562 mbedtls_ssl_transform *transform )
8563{
8564#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008565 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008566 {
8567 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008568#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008569 ssl->out_cid = ssl->out_ctr + 8;
8570 ssl->out_len = ssl->out_cid;
8571 if( transform != NULL )
8572 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008573#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008574 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008575#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008576 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008577 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008578 MBEDTLS_SSL_TRANSPORT_ELSE
8579#endif /* MBEDTLS_SSL_PROTO_DTLS */
8580#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008581 {
8582 ssl->out_ctr = ssl->out_hdr - 8;
8583 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008584#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008585 ssl->out_cid = ssl->out_len;
8586#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008587 ssl->out_iv = ssl->out_hdr + 5;
8588 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008589#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008590
8591 /* Adjust out_msg to make space for explicit IV, if used. */
8592 if( transform != NULL &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008593 mbedtls_ssl_ver_geq(
8594 mbedtls_ssl_get_minor_ver( ssl ),
8595 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008596 {
8597 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8598 }
8599 else
8600 ssl->out_msg = ssl->out_iv;
8601}
8602
8603/* Once ssl->in_hdr as the address of the beginning of the
8604 * next incoming record is set, deduce the other pointers.
8605 *
8606 * Note: For TLS, we save the implicit record sequence number
8607 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8608 * and the caller has to make sure there's space for this.
8609 */
8610
Hanno Beckerf5970a02019-05-08 09:38:41 +01008611static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008612{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008613 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008614 * of unprotected TLS/DTLS records, with ssl->in_msg
8615 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008616 *
8617 * When decrypting a protected record, ssl->in_msg
8618 * will be shifted to point to the beginning of the
8619 * record plaintext.
8620 */
8621
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008622#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008623 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008624 {
Hanno Becker70e79282019-05-03 14:34:53 +01008625 /* This sets the header pointers to match records
8626 * without CID. When we receive a record containing
8627 * a CID, the fields are shifted accordingly in
8628 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008629 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008630#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008631 ssl->in_cid = ssl->in_ctr + 8;
8632 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008633#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008634 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008635#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008636 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008637 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008638 MBEDTLS_SSL_TRANSPORT_ELSE
8639#endif /* MBEDTLS_SSL_PROTO_DTLS */
8640#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008641 {
8642 ssl->in_ctr = ssl->in_hdr - 8;
8643 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008644#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008645 ssl->in_cid = ssl->in_len;
8646#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008647 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008648 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008649#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008650}
8651
Paul Bakker5121ce52009-01-03 21:22:43 +00008652/*
8653 * Initialize an SSL context
8654 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008655void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8656{
8657 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8658}
8659
8660/*
8661 * Setup an SSL context
8662 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008663
8664static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8665{
8666 /* Set the incoming and outgoing record pointers. */
8667#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008668 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008669 {
8670 ssl->out_hdr = ssl->out_buf;
8671 ssl->in_hdr = ssl->in_buf;
8672 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008673 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008674#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008675#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008676 {
8677 ssl->out_hdr = ssl->out_buf + 8;
8678 ssl->in_hdr = ssl->in_buf + 8;
8679 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008680#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008681
8682 /* Derive other internal pointers. */
8683 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008684 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008685}
8686
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008687int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008688 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008689{
Paul Bakker48916f92012-09-16 19:57:18 +00008690 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008691
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008692 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008693
Hanno Beckeref982d52019-07-23 15:56:18 +01008694#if defined(MBEDTLS_USE_TINYCRYPT)
8695 uECC_set_rng( &uecc_rng_wrapper );
8696#endif
8697
Paul Bakker62f2dee2012-09-28 07:31:51 +00008698 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008699 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008700 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008701
8702 /* Set to NULL in case of an error condition */
8703 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008704
Angus Grattond8213d02016-05-25 20:56:48 +10008705 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8706 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008707 {
Angus Grattond8213d02016-05-25 20:56:48 +10008708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008709 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008710 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008711 }
8712
8713 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8714 if( ssl->out_buf == NULL )
8715 {
8716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008717 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008718 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008719 }
8720
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008721 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008722
Paul Bakker48916f92012-09-16 19:57:18 +00008723 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008724 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008725
Hanno Beckerc8f52992019-07-25 11:15:08 +01008726 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008727
Paul Bakker5121ce52009-01-03 21:22:43 +00008728 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008729
8730error:
8731 mbedtls_free( ssl->in_buf );
8732 mbedtls_free( ssl->out_buf );
8733
8734 ssl->conf = NULL;
8735
8736 ssl->in_buf = NULL;
8737 ssl->out_buf = NULL;
8738
8739 ssl->in_hdr = NULL;
8740 ssl->in_ctr = NULL;
8741 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008742 ssl->in_msg = NULL;
8743
8744 ssl->out_hdr = NULL;
8745 ssl->out_ctr = NULL;
8746 ssl->out_len = NULL;
8747 ssl->out_iv = NULL;
8748 ssl->out_msg = NULL;
8749
k-stachowiak9f7798e2018-07-31 16:52:32 +02008750 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008751}
8752
8753/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008754 * Reset an initialized and used SSL context for re-use while retaining
8755 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008756 *
8757 * If partial is non-zero, keep data in the input buffer and client ID.
8758 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008759 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008760static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008761{
Paul Bakker48916f92012-09-16 19:57:18 +00008762 int ret;
8763
Hanno Becker7e772132018-08-10 12:38:21 +01008764#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8765 !defined(MBEDTLS_SSL_SRV_C)
8766 ((void) partial);
8767#endif
8768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008769 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008770
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008771 /* Cancel any possibly running timer */
8772 ssl_set_timer( ssl, 0 );
8773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008774#if defined(MBEDTLS_SSL_RENEGOTIATION)
8775 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008776 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008777
8778 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008779 mbedtls_platform_memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8780 mbedtls_platform_memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008781#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008782 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008783
Paul Bakker7eb013f2011-10-06 12:37:39 +00008784 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008785 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008786
8787 ssl->in_msgtype = 0;
8788 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008789#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008790 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008791 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008792#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008793#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008794 ssl_dtls_replay_reset( ssl );
8795#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008796
8797 ssl->in_hslen = 0;
8798 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008799
8800 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008801
8802 ssl->out_msgtype = 0;
8803 ssl->out_msglen = 0;
8804 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008805#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8806 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008807 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008808#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008809
Hanno Becker19859472018-08-06 09:40:20 +01008810 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8811
Paul Bakker48916f92012-09-16 19:57:18 +00008812 ssl->transform_in = NULL;
8813 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008814
Hanno Becker78640902018-08-13 16:35:15 +01008815 ssl->session_in = NULL;
8816 ssl->session_out = NULL;
8817
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008818 mbedtls_platform_memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008819
8820#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008821 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008822#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8823 {
8824 ssl->in_left = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008825 mbedtls_platform_memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008826 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008828#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8829 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8832 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008834 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8835 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008836 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008837 }
8838#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008839
Paul Bakker48916f92012-09-16 19:57:18 +00008840 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008842 mbedtls_ssl_transform_free( ssl->transform );
8843 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008844 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008845 }
Paul Bakker48916f92012-09-16 19:57:18 +00008846
Paul Bakkerc0463502013-02-14 11:19:38 +01008847 if( ssl->session )
8848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008849 mbedtls_ssl_session_free( ssl->session );
8850 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008851 ssl->session = NULL;
8852 }
8853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008854#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008855 ssl->alpn_chosen = NULL;
8856#endif
8857
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008858#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008859#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008860 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008861#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008862 {
8863 mbedtls_free( ssl->cli_id );
8864 ssl->cli_id = NULL;
8865 ssl->cli_id_len = 0;
8866 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008867#endif
8868
Paul Bakker48916f92012-09-16 19:57:18 +00008869 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8870 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008871
8872 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008873}
8874
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008875/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008876 * Reset an initialized and used SSL context for re-use while retaining
8877 * all application-set variables, function pointers and data.
8878 */
8879int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8880{
8881 return( ssl_session_reset_int( ssl, 0 ) );
8882}
8883
8884/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008885 * SSL set accessors
8886 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008887#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008888void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008889{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008890 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008891}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008892#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008893
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008894void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008895{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008896 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008897}
8898
Hanno Becker7f376f42019-06-12 16:20:48 +01008899#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8900 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008901void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008902{
Hanno Becker7f376f42019-06-12 16:20:48 +01008903 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008904}
Hanno Becker7f376f42019-06-12 16:20:48 +01008905#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008906
Hanno Beckerde671542019-06-12 16:30:46 +01008907#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8908 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8909void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8910 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008911{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008912 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008913}
Hanno Beckerde671542019-06-12 16:30:46 +01008914#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008916#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008917
Hanno Becker1841b0a2018-08-24 11:13:57 +01008918void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8919 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008920{
8921 ssl->disable_datagram_packing = !allow_packing;
8922}
8923
Hanno Becker1f835fa2019-06-13 10:14:59 +01008924#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8925 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008926void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8927 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008928{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008929 conf->hs_timeout_min = min;
8930 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008931}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008932#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8933 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8934void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8935 uint32_t min, uint32_t max )
8936{
8937 ((void) conf);
8938 ((void) min);
8939 ((void) max);
8940}
8941#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8942 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8943
8944#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008945
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008946void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008947{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008948#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8949 conf->authmode = authmode;
8950#else
8951 ((void) conf);
8952 ((void) authmode);
8953#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008954}
8955
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008956#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8957 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008958void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008959 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008960 void *p_vrfy )
8961{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008962 conf->f_vrfy = f_vrfy;
8963 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008964}
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008965#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008966
Hanno Beckerece325c2019-06-13 15:39:27 +01008967#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008968void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008969 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008970 void *p_rng )
8971{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008972 conf->f_rng = f_rng;
8973 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008974}
Hanno Beckerece325c2019-06-13 15:39:27 +01008975#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008976
Hanno Becker14a4a442019-07-02 17:00:34 +01008977#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008978void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008979 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008980 void *p_dbg )
8981{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008982 conf->f_dbg = f_dbg;
8983 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008984}
Hanno Becker14a4a442019-07-02 17:00:34 +01008985#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008986
Hanno Beckera58a8962019-06-13 16:11:15 +01008987#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8988 !defined(MBEDTLS_SSL_CONF_SEND) && \
8989 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008990void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008991 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008992 mbedtls_ssl_send_t *f_send,
8993 mbedtls_ssl_recv_t *f_recv,
8994 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008995{
Hanno Beckera58a8962019-06-13 16:11:15 +01008996 ssl->p_bio = p_bio;
8997 ssl->f_send = f_send;
8998 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008999 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009000}
Hanno Beckera58a8962019-06-13 16:11:15 +01009001#else
9002void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
9003 void *p_bio )
9004{
9005 ssl->p_bio = p_bio;
9006}
9007#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009008
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02009009#if defined(MBEDTLS_SSL_PROTO_DTLS)
9010void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
9011{
9012 ssl->mtu = mtu;
9013}
9014#endif
9015
Hanno Becker1f835fa2019-06-13 10:14:59 +01009016#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009017void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009018{
9019 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009020}
Hanno Becker1f835fa2019-06-13 10:14:59 +01009021#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009022
Hanno Becker0ae6b242019-06-13 16:45:36 +01009023#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
9024 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009025void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
9026 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00009027 mbedtls_ssl_set_timer_t *f_set_timer,
9028 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009029{
9030 ssl->p_timer = p_timer;
9031 ssl->f_set_timer = f_set_timer;
9032 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009033 /* Make sure we start with no timer running */
9034 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009035}
Hanno Becker0ae6b242019-06-13 16:45:36 +01009036#else
9037void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
9038 void *p_timer )
9039{
9040 ssl->p_timer = p_timer;
9041 /* Make sure we start with no timer running */
9042 ssl_set_timer( ssl, 0 );
9043}
9044#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009045
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009046#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009047void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009048 void *p_cache,
9049 int (*f_get_cache)(void *, mbedtls_ssl_session *),
9050 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00009051{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009052 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009053 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009054 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00009055}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009056#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009057
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009058#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009059int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00009060{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009061 int ret;
9062
9063 if( ssl == NULL ||
9064 session == NULL ||
9065 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009066 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009068 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009069 }
9070
Hanno Becker58fccf22019-02-06 14:30:46 +00009071 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
9072 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009073 return( ret );
9074
Jarno Lamsa8d09e572019-12-19 15:20:19 +02009075 ssl->handshake->resume = MBEDTLS_SSL_FI_FLAG_SET;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009076
9077 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009078}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009079#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009080
Hanno Becker73f4cb12019-06-27 13:51:07 +01009081#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009082void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009083 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00009084{
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009085 conf->ciphersuite_list[0] = ciphersuites;
9086 conf->ciphersuite_list[1] = ciphersuites;
9087 conf->ciphersuite_list[2] = ciphersuites;
9088 conf->ciphersuite_list[3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009089}
9090
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009091void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02009092 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009093 int major, int minor )
9094{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009095 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009096 return;
9097
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009098 if( mbedtls_ssl_ver_lt( minor, MBEDTLS_SSL_MINOR_VERSION_0 ) ||
9099 mbedtls_ssl_ver_gt( minor, MBEDTLS_SSL_MINOR_VERSION_3 ) )
9100 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009101 return;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009102 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009103
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009104 conf->ciphersuite_list[mbedtls_ssl_minor_ver_index( minor )] =
9105 ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00009106}
Hanno Becker73f4cb12019-06-27 13:51:07 +01009107#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009109#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009110void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01009111 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009112{
9113 conf->cert_profile = profile;
9114}
9115
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009116/* Append a new keycert entry to a (possibly empty) list */
9117static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
9118 mbedtls_x509_crt *cert,
9119 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009120{
niisato8ee24222018-06-25 19:05:48 +09009121 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009122
niisato8ee24222018-06-25 19:05:48 +09009123 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
9124 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009125 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009126
niisato8ee24222018-06-25 19:05:48 +09009127 new_cert->cert = cert;
9128 new_cert->key = key;
9129 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009130
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009131 /* Update head is the list was null, else add to the end */
9132 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01009133 {
niisato8ee24222018-06-25 19:05:48 +09009134 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01009135 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009136 else
9137 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009138 mbedtls_ssl_key_cert *cur = *head;
9139 while( cur->next != NULL )
9140 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09009141 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009142 }
9143
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009144 return( 0 );
9145}
9146
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009147int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009148 mbedtls_x509_crt *own_cert,
9149 mbedtls_pk_context *pk_key )
9150{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02009151 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009152}
9153
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009154void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009155 mbedtls_x509_crt *ca_chain,
9156 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009157{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009158 conf->ca_chain = ca_chain;
9159 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00009160}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009161#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00009162
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009163#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9164int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
9165 mbedtls_x509_crt *own_cert,
9166 mbedtls_pk_context *pk_key )
9167{
9168 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
9169 own_cert, pk_key ) );
9170}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02009171
9172void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
9173 mbedtls_x509_crt *ca_chain,
9174 mbedtls_x509_crl *ca_crl )
9175{
9176 ssl->handshake->sni_ca_chain = ca_chain;
9177 ssl->handshake->sni_ca_crl = ca_crl;
9178}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02009179
9180void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
9181 int authmode )
9182{
9183 ssl->handshake->sni_authmode = authmode;
9184}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009185#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9186
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009187#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009188/*
9189 * Set EC J-PAKE password for current handshake
9190 */
9191int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
9192 const unsigned char *pw,
9193 size_t pw_len )
9194{
9195 mbedtls_ecjpake_role role;
9196
Janos Follath8eb64132016-06-03 15:40:57 +01009197 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9199
Hanno Becker2d9623f2019-06-13 12:07:05 +01009200 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009201 role = MBEDTLS_ECJPAKE_SERVER;
9202 else
9203 role = MBEDTLS_ECJPAKE_CLIENT;
9204
9205 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
9206 role,
9207 MBEDTLS_MD_SHA256,
9208 MBEDTLS_ECP_DP_SECP256R1,
9209 pw, pw_len ) );
9210}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009211#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009213#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009214int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009215 const unsigned char *psk, size_t psk_len,
9216 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009217{
Paul Bakker6db455e2013-09-18 17:29:31 +02009218 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009219 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02009220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009221 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9222 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01009223
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009224 /* Identity len will be encoded on two bytes */
9225 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009226 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009227 {
9228 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9229 }
9230
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009231 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02009232 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009233 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009234
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009235 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009236 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009237 conf->psk_len = 0;
9238 }
9239 if( conf->psk_identity != NULL )
9240 {
9241 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009242 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009243 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02009244 }
9245
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009246 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
9247 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05009248 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009249 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009250 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009251 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009252 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009253 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05009254 }
Paul Bakker6db455e2013-09-18 17:29:31 +02009255
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009256 conf->psk_len = psk_len;
9257 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02009258
Teppo Järvelin91d79382019-10-02 09:09:31 +03009259 mbedtls_platform_memcpy( conf->psk, psk, conf->psk_len );
9260 mbedtls_platform_memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02009261
9262 return( 0 );
9263}
9264
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009265int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9266 const unsigned char *psk, size_t psk_len )
9267{
9268 if( psk == NULL || ssl->handshake == NULL )
9269 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9270
9271 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9272 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9273
9274 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009275 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009276 mbedtls_platform_zeroize( ssl->handshake->psk,
9277 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01009278 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009279 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009280 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009281
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009282 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009283 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009284
9285 ssl->handshake->psk_len = psk_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03009286 mbedtls_platform_memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009287
9288 return( 0 );
9289}
9290
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009291void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009292 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009293 size_t),
9294 void *p_psk )
9295{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009296 conf->f_psk = f_psk;
9297 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009298}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009299#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009300
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009301#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009302
9303#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009304int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009305{
9306 int ret;
9307
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009308 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9309 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9310 {
9311 mbedtls_mpi_free( &conf->dhm_P );
9312 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009313 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009314 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009315
9316 return( 0 );
9317}
Hanno Becker470a8c42017-10-04 15:28:46 +01009318#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009319
Hanno Beckera90658f2017-10-04 15:29:08 +01009320int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9321 const unsigned char *dhm_P, size_t P_len,
9322 const unsigned char *dhm_G, size_t G_len )
9323{
9324 int ret;
9325
9326 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9327 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9328 {
9329 mbedtls_mpi_free( &conf->dhm_P );
9330 mbedtls_mpi_free( &conf->dhm_G );
9331 return( ret );
9332 }
9333
9334 return( 0 );
9335}
Paul Bakker5121ce52009-01-03 21:22:43 +00009336
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009337int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009338{
9339 int ret;
9340
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009341 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9342 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9343 {
9344 mbedtls_mpi_free( &conf->dhm_P );
9345 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009346 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009347 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009348
9349 return( 0 );
9350}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009351#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009352
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009353#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9354/*
9355 * Set the minimum length for Diffie-Hellman parameters
9356 */
9357void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9358 unsigned int bitlen )
9359{
9360 conf->dhm_min_bitlen = bitlen;
9361}
9362#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9363
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009364#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009365/*
9366 * Set allowed/preferred hashes for handshake signatures
9367 */
9368void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9369 const int *hashes )
9370{
Hanno Becker56595f42019-06-19 16:31:38 +01009371#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009372 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01009373#else
9374 ((void) conf);
9375 ((void) hashes);
9376#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009377}
Hanno Becker947194e2017-04-07 13:25:49 +01009378#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009379
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009380#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01009381#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009382/*
9383 * Set the allowed elliptic curves
9384 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009385void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009386 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009387{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009388 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009389}
Hanno Beckerc1096e72019-06-19 12:30:41 +01009390#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01009391#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009392
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009393#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009394int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009395{
Hanno Becker947194e2017-04-07 13:25:49 +01009396 /* Initialize to suppress unnecessary compiler warning */
9397 size_t hostname_len = 0;
9398
9399 /* Check if new hostname is valid before
9400 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009401 if( hostname != NULL )
9402 {
9403 hostname_len = strlen( hostname );
9404
9405 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9406 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9407 }
9408
9409 /* Now it's clear that we will overwrite the old hostname,
9410 * so we can free it safely */
9411
9412 if( ssl->hostname != NULL )
9413 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009414 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009415 mbedtls_free( ssl->hostname );
9416 }
9417
9418 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009419
Paul Bakker5121ce52009-01-03 21:22:43 +00009420 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009421 {
9422 ssl->hostname = NULL;
9423 }
9424 else
9425 {
9426 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009427 if( ssl->hostname == NULL )
9428 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009429
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03009430 /* Not using more secure mbedtls_platform_memcpy as hostname is public in initial handshake */
9431 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009432
Hanno Becker947194e2017-04-07 13:25:49 +01009433 ssl->hostname[hostname_len] = '\0';
9434 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009435
9436 return( 0 );
9437}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009438#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009439
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009440#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009441void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009442 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009443 const unsigned char *, size_t),
9444 void *p_sni )
9445{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009446 conf->f_sni = f_sni;
9447 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009448}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009449#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009451#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009452int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009453{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009454 size_t cur_len, tot_len;
9455 const char **p;
9456
9457 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009458 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9459 * MUST NOT be truncated."
9460 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009461 */
9462 tot_len = 0;
9463 for( p = protos; *p != NULL; p++ )
9464 {
9465 cur_len = strlen( *p );
9466 tot_len += cur_len;
9467
9468 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009469 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009470 }
9471
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009472 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009473
9474 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009475}
9476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009478{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009479 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009480}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009481#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009482
Hanno Becker33b9b252019-07-05 11:23:25 +01009483#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9484 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9485void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9486 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009487{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009488 conf->max_major_ver = major;
9489 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009490}
Hanno Becker33b9b252019-07-05 11:23:25 +01009491#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9492 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009493
Hanno Becker33b9b252019-07-05 11:23:25 +01009494#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9495 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9496void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9497 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009498{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009499 conf->min_major_ver = major;
9500 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009501}
Hanno Becker33b9b252019-07-05 11:23:25 +01009502#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9503 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009505#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009506void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009507{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009508 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009509}
9510#endif
9511
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009512#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009513void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9514 char cert_req_ca_list )
9515{
9516 conf->cert_req_ca_list = cert_req_ca_list;
9517}
9518#endif
9519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009521void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009522{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009523 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009524}
9525#endif
9526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009527#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009528#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009529void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009530{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009531 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009532}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009533#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9534#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009535void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009536 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009537{
9538 conf->enforce_extended_master_secret = ems_enf;
9539}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009540#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009541#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009542
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009543#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009544void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009545{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009546 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009547}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009548#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009550#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009551int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009552{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009553 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009554 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009557 }
9558
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009559 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009560
9561 return( 0 );
9562}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009566void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009567{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009568 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009569}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009570#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009572#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009573void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009574{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009575 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009576}
9577#endif
9578
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009579#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009580void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009581{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009582 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009583}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009584#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009586#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009587void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009588{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009589 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009590}
9591
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009592void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009593{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009594 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009595}
9596
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009597void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009598 const unsigned char period[8] )
9599{
Teppo Järvelin91d79382019-10-02 09:09:31 +03009600 mbedtls_platform_memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009601}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009602#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009605#if defined(MBEDTLS_SSL_CLI_C)
9606void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009607{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009608 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009609}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009610#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009611
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009612#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009613void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9614 mbedtls_ssl_ticket_write_t *f_ticket_write,
9615 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9616 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009617{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009618 conf->f_ticket_write = f_ticket_write;
9619 conf->f_ticket_parse = f_ticket_parse;
9620 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009621}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009622#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009624
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009625#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9626void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9627 mbedtls_ssl_export_keys_t *f_export_keys,
9628 void *p_export_keys )
9629{
9630 conf->f_export_keys = f_export_keys;
9631 conf->p_export_keys = p_export_keys;
9632}
9633#endif
9634
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009635#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009636void mbedtls_ssl_conf_async_private_cb(
9637 mbedtls_ssl_config *conf,
9638 mbedtls_ssl_async_sign_t *f_async_sign,
9639 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9640 mbedtls_ssl_async_resume_t *f_async_resume,
9641 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009642 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009643{
9644 conf->f_async_sign_start = f_async_sign;
9645 conf->f_async_decrypt_start = f_async_decrypt;
9646 conf->f_async_resume = f_async_resume;
9647 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009648 conf->p_async_config_data = async_config_data;
9649}
9650
Gilles Peskine8f97af72018-04-26 11:46:10 +02009651void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9652{
9653 return( conf->p_async_config_data );
9654}
9655
Gilles Peskine1febfef2018-04-30 11:54:39 +02009656void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009657{
9658 if( ssl->handshake == NULL )
9659 return( NULL );
9660 else
9661 return( ssl->handshake->user_async_ctx );
9662}
9663
Gilles Peskine1febfef2018-04-30 11:54:39 +02009664void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009665 void *ctx )
9666{
9667 if( ssl->handshake != NULL )
9668 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009669}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009670#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009671
Paul Bakker5121ce52009-01-03 21:22:43 +00009672/*
9673 * SSL get accessors
9674 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009676{
9677 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9678}
9679
Hanno Becker8b170a02017-10-10 11:51:19 +01009680int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9681{
9682 /*
9683 * Case A: We're currently holding back
9684 * a message for further processing.
9685 */
9686
9687 if( ssl->keep_current_message == 1 )
9688 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009689 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009690 return( 1 );
9691 }
9692
9693 /*
9694 * Case B: Further records are pending in the current datagram.
9695 */
9696
9697#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009698 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009699 ssl->in_left > ssl->next_record_offset )
9700 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009701 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009702 return( 1 );
9703 }
9704#endif /* MBEDTLS_SSL_PROTO_DTLS */
9705
9706 /*
9707 * Case C: A handshake message is being processed.
9708 */
9709
Hanno Becker8b170a02017-10-10 11:51:19 +01009710 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9711 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009712 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009713 return( 1 );
9714 }
9715
9716 /*
9717 * Case D: An application data message is being processed
9718 */
9719 if( ssl->in_offt != NULL )
9720 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009721 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009722 return( 1 );
9723 }
9724
9725 /*
9726 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009727 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009728 * we implement support for multiple alerts in single records.
9729 */
9730
9731 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9732 return( 0 );
9733}
9734
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009735uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009736{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009737 if( ssl->session != NULL )
9738 return( ssl->session->verify_result );
9739
9740 if( ssl->session_negotiate != NULL )
9741 return( ssl->session_negotiate->verify_result );
9742
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009743 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009744}
9745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009746const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009747{
Hanno Beckere02758c2019-06-26 15:31:31 +01009748 int suite;
9749
Paul Bakker926c8e42013-03-06 10:23:34 +01009750 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009751 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009752
Hanno Beckere02758c2019-06-26 15:31:31 +01009753 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9754 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009755}
9756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009757const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009758{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009759#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009760 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009761 {
Hanno Becker2881d802019-05-22 14:44:53 +01009762 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009764 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009765 return( "DTLSv1.0" );
9766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009768 return( "DTLSv1.2" );
9769
9770 default:
9771 return( "unknown (DTLS)" );
9772 }
9773 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009774 MBEDTLS_SSL_TRANSPORT_ELSE
9775#endif /* MBEDTLS_SSL_PROTO_DTLS */
9776#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009777 {
Hanno Becker2881d802019-05-22 14:44:53 +01009778 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009779 {
9780 case MBEDTLS_SSL_MINOR_VERSION_0:
9781 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009782
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009783 case MBEDTLS_SSL_MINOR_VERSION_1:
9784 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009785
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009786 case MBEDTLS_SSL_MINOR_VERSION_2:
9787 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009788
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009789 case MBEDTLS_SSL_MINOR_VERSION_3:
9790 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009791
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009792 default:
9793 return( "unknown" );
9794 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009795 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009796#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009797}
9798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009799int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009800{
Hanno Becker3136ede2018-08-17 15:28:19 +01009801 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009803
Hanno Becker43395762019-05-03 14:46:38 +01009804 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9805
Hanno Becker78640902018-08-13 16:35:15 +01009806 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009807 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009809#if defined(MBEDTLS_ZLIB_SUPPORT)
9810 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9811 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009812#endif
9813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009814 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009815 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009816#if defined(MBEDTLS_GCM_C) || \
9817 defined(MBEDTLS_CCM_C) || \
9818 defined(MBEDTLS_CHACHAPOLY_C)
9819#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009821#endif
9822#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009823 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009824#endif
9825#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009826 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009827#endif
9828 transform_expansion =
9829 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009830 break;
9831
Hanno Beckera9d5c452019-07-25 16:47:12 +01009832#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9833 MBEDTLS_CHACHAPOLY_C */
9834
9835#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9836 case MBEDTLS_MODE_STREAM:
9837 transform_expansion = transform->maclen;
9838 break;
9839#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9840
9841#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009842 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009843 {
9844 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009845
9846 block_size = mbedtls_cipher_get_block_size(
9847 &transform->cipher_ctx_enc );
9848
Hanno Becker3136ede2018-08-17 15:28:19 +01009849 /* Expansion due to the addition of the MAC. */
9850 transform_expansion += transform->maclen;
9851
9852 /* Expansion due to the addition of CBC padding;
9853 * Theoretically up to 256 bytes, but we never use
9854 * more than the block size of the underlying cipher. */
9855 transform_expansion += block_size;
9856
9857 /* For TLS 1.1 or higher, an explicit IV is added
9858 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009859#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009860 if( mbedtls_ssl_ver_geq(
9861 mbedtls_ssl_get_minor_ver( ssl ),
9862 MBEDTLS_SSL_MINOR_VERSION_2 ) )
9863 {
Hanno Becker3136ede2018-08-17 15:28:19 +01009864 transform_expansion += block_size;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009865 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01009866#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009867
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009868 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009869 }
9870#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009871
9872 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009874 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009875 }
9876
Hanno Beckera5a2b082019-05-15 14:03:01 +01009877#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009878 if( transform->out_cid_len != 0 )
9879 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009880#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009881
Hanno Becker43395762019-05-03 14:46:38 +01009882 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009883}
9884
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009885#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9886size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9887{
9888 size_t max_len;
9889
9890 /*
9891 * Assume mfl_code is correct since it was checked when set
9892 */
Angus Grattond8213d02016-05-25 20:56:48 +10009893 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009894
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009895 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009896 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009897 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009898 {
Angus Grattond8213d02016-05-25 20:56:48 +10009899 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009900 }
9901
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009902 /* During a handshake, use the value being negotiated */
9903 if( ssl->session_negotiate != NULL &&
9904 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9905 {
9906 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9907 }
9908
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009909 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009910}
9911#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9912
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009913#if defined(MBEDTLS_SSL_PROTO_DTLS)
9914static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9915{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009916 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009917 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009918 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9919 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009920 return( 0 );
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009921
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009922 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9923 return( ssl->mtu );
9924
9925 if( ssl->mtu == 0 )
9926 return( ssl->handshake->mtu );
9927
9928 return( ssl->mtu < ssl->handshake->mtu ?
9929 ssl->mtu : ssl->handshake->mtu );
9930}
9931#endif /* MBEDTLS_SSL_PROTO_DTLS */
9932
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009933int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9934{
9935 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9936
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009937#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9938 !defined(MBEDTLS_SSL_PROTO_DTLS)
9939 (void) ssl;
9940#endif
9941
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009942#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9943 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9944
9945 if( max_len > mfl )
9946 max_len = mfl;
9947#endif
9948
9949#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009950 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009951 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009952 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009953 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9954 const size_t overhead = (size_t) ret;
9955
9956 if( ret < 0 )
9957 return( ret );
9958
9959 if( mtu <= overhead )
9960 {
9961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9962 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9963 }
9964
9965 if( max_len > mtu - overhead )
9966 max_len = mtu - overhead;
9967 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009968#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009969
Hanno Becker0defedb2018-08-10 12:35:02 +01009970#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9971 !defined(MBEDTLS_SSL_PROTO_DTLS)
9972 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009973#endif
9974
9975 return( (int) max_len );
9976}
9977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009978#if defined(MBEDTLS_X509_CRT_PARSE_C)
9979const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009980{
9981 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009982 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009983
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009984#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009985 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009986#else
9987 return( NULL );
9988#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009989}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009990#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009992#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009993int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9994 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009995{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009996 if( ssl == NULL ||
9997 dst == NULL ||
9998 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009999 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010001 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010002 }
10003
Hanno Becker58fccf22019-02-06 14:30:46 +000010004 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010005}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010006#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010007
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +020010008const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
10009{
10010 if( ssl == NULL )
10011 return( NULL );
10012
10013 return( ssl->session );
10014}
10015
Paul Bakker5121ce52009-01-03 21:22:43 +000010016/*
Hanno Beckerb5352f02019-05-16 12:39:07 +010010017 * Define ticket header determining Mbed TLS version
10018 * and structure of the ticket.
10019 */
10020
Hanno Becker41527622019-05-16 12:50:45 +010010021/*
Hanno Becker26829e92019-05-28 14:30:45 +010010022 * Define bitflag determining compile-time settings influencing
10023 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +010010024 */
10025
Hanno Becker26829e92019-05-28 14:30:45 +010010026#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010027#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +010010028#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010029#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +010010030#endif /* MBEDTLS_HAVE_TIME */
10031
10032#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010033#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +010010034#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010035#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +010010036#endif /* MBEDTLS_X509_CRT_PARSE_C */
10037
10038#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010039#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +010010040#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010041#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +010010042#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
10043
10044#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010045#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +010010046#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010047#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +010010048#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
10049
10050#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010051#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +010010052#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010053#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +010010054#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
10055
10056#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010057#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +010010058#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010059#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +010010060#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
10061
Hanno Becker41527622019-05-16 12:50:45 +010010062#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10063#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
10064#else
10065#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
10066#endif /* MBEDTLS_SSL_SESSION_TICKETS */
10067
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010068#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10069#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
10070#else
10071#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
10072#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10073
Hanno Becker88440552019-07-03 14:16:13 +010010074#if defined(MBEDTLS_ZLIB_SUPPORT)
10075#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
10076#else
10077#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
10078#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10079
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010080#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
10081#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
10082#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
10083#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
10084#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
10085#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
10086#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010087#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +010010088#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010089
Hanno Becker26829e92019-05-28 14:30:45 +010010090#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010091 ( (uint16_t) ( \
10092 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
10093 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
10094 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
10095 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
10096 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
10097 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010098 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +010010099 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010100 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +010010101
Hanno Becker557fe9f2019-05-16 12:41:07 +010010102static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +010010103 MBEDTLS_VERSION_MAJOR,
10104 MBEDTLS_VERSION_MINOR,
10105 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +010010106 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
10107 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +010010108};
Hanno Beckerb5352f02019-05-16 12:39:07 +010010109
10110/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010111 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010112 * (in the presentation language of TLS, RFC 8446 section 3)
10113 *
Hanno Becker26829e92019-05-28 14:30:45 +010010114 * opaque mbedtls_version[3]; // major, minor, patch
10115 * opaque session_format[2]; // version-specific 16-bit field determining
10116 * // the format of the remaining
10117 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +010010118 *
10119 * Note: When updating the format, remember to keep
10120 * these version+format bytes.
10121 *
Hanno Becker7bf77102019-06-04 09:43:16 +010010122 * // In this version, `session_format` determines
10123 * // the setting of those compile-time
10124 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +010010125 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010126 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +010010127 * uint8 ciphersuite[2]; // defined by the standard
10128 * uint8 compression; // 0 or 1
10129 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010130 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +010010131 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010132 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +010010133 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
10134 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
10135 * case disabled: uint8_t peer_cert_digest_type;
10136 * opaque peer_cert_digest<0..2^8-1>;
10137 * }
Hanno Becker26829e92019-05-28 14:30:45 +010010138 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010139 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +010010140 * uint8 mfl_code; // up to 255 according to standard
10141 * uint8 trunc_hmac; // 0 or 1
10142 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010143 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010144 * The order is the same as in the definition of the structure, except
10145 * verify_result is put before peer_cert so that all mandatory fields come
10146 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010147 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010148static int ssl_session_save( const mbedtls_ssl_session *session,
10149 unsigned char omit_header,
10150 unsigned char *buf,
10151 size_t buf_len,
10152 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010153{
10154 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010155 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010156#if defined(MBEDTLS_HAVE_TIME)
10157 uint64_t start;
10158#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010159#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010160#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010161 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010162#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +000010163#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010164
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010165 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010166 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010167 /*
10168 * Add version identifier
10169 */
10170
10171 used += sizeof( ssl_serialized_session_header );
10172
10173 if( used <= buf_len )
10174 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010175 mbedtls_platform_memcpy( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010176 sizeof( ssl_serialized_session_header ) );
10177 p += sizeof( ssl_serialized_session_header );
10178 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010179 }
10180
10181 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010182 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010183 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010184#if defined(MBEDTLS_HAVE_TIME)
10185 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010186
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010187 if( used <= buf_len )
10188 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010189 start = (uint64_t) session->start;
10190
10191 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
10192 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
10193 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
10194 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
10195 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
10196 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
10197 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
10198 *p++ = (unsigned char)( ( start ) & 0xFF );
10199 }
10200#endif /* MBEDTLS_HAVE_TIME */
10201
10202 /*
10203 * Basic mandatory fields
10204 */
Hanno Becker88440552019-07-03 14:16:13 +010010205 {
10206 size_t const ciphersuite_len = 2;
10207#if defined(MBEDTLS_ZLIB_SUPPORT)
10208 size_t const compression_len = 1;
10209#else
10210 size_t const compression_len = 0;
10211#endif
10212 size_t const id_len_len = 1;
10213 size_t const id_len = 32;
10214 size_t const master_len = 48;
10215 size_t const verif_result_len = 4;
10216
10217 size_t const basic_len =
10218 ciphersuite_len +
10219 compression_len +
10220 id_len_len +
10221 id_len +
10222 master_len +
10223 verif_result_len;
10224
10225 used += basic_len;
10226 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010227
10228 if( used <= buf_len )
10229 {
Hanno Beckere02758c2019-06-26 15:31:31 +010010230 const int ciphersuite =
10231 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010232 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010233
Hanno Becker88440552019-07-03 14:16:13 +010010234#if defined(MBEDTLS_ZLIB_SUPPORT)
10235 *p++ = (unsigned char)(
10236 mbedtls_ssl_session_get_compression( session ) );
10237#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010238
10239 *p++ = (unsigned char)( session->id_len & 0xFF );
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010240 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10241 memcpy( p, session->id, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010242 p += 32;
10243
Teppo Järvelin91d79382019-10-02 09:09:31 +030010244 mbedtls_platform_memcpy( p, session->master, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010245 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010246 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010247 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010248
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010249 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010250 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010251 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010252#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010253#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010254 if( session->peer_cert == NULL )
10255 cert_len = 0;
10256 else
10257 cert_len = session->peer_cert->raw.len;
10258
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010259 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010260
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010261 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010262 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010263 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010264
10265 if( session->peer_cert != NULL )
10266 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010267 mbedtls_platform_memcpy( p, session->peer_cert->raw.p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010268 p += cert_len;
10269 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010270 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010271
Hanno Becker5882dd02019-06-06 16:25:57 +010010272#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010273 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010274 if( session->peer_cert_digest != NULL )
10275 {
10276 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
10277 if( used <= buf_len )
10278 {
10279 *p++ = (unsigned char) session->peer_cert_digest_type;
10280 *p++ = (unsigned char) session->peer_cert_digest_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +030010281 mbedtls_platform_memcpy( p, session->peer_cert_digest,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010282 session->peer_cert_digest_len );
10283 p += session->peer_cert_digest_len;
10284 }
10285 }
10286 else
10287 {
10288 used += 2;
10289 if( used <= buf_len )
10290 {
10291 *p++ = (unsigned char) MBEDTLS_MD_NONE;
10292 *p++ = 0;
10293 }
10294 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010295#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010296#endif /* MBEDTLS_X509_CRT_PARSE_C */
10297
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010298 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010299 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010300 */
10301#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010302 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010303
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010304 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010305 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010306 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010307
10308 if( session->ticket != NULL )
10309 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010310 mbedtls_platform_memcpy( p, session->ticket, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010311 p += session->ticket_len;
10312 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010313
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010314 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010315 }
10316#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10317
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010318 /*
10319 * Misc extension-related info
10320 */
10321#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10322 used += 1;
10323
10324 if( used <= buf_len )
10325 *p++ = session->mfl_code;
10326#endif
10327
10328#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10329 used += 1;
10330
10331 if( used <= buf_len )
10332 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
10333#endif
10334
10335#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10336 used += 1;
10337
10338 if( used <= buf_len )
10339 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
10340#endif
10341
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010342 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010343 *olen = used;
10344
10345 if( used > buf_len )
10346 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010347
10348 return( 0 );
10349}
10350
10351/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010352 * Public wrapper for ssl_session_save()
10353 */
10354int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
10355 unsigned char *buf,
10356 size_t buf_len,
10357 size_t *olen )
10358{
10359 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
10360}
10361
10362/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010363 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010364 *
10365 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010366 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010367 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010368static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010369 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010370 const unsigned char *buf,
10371 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010372{
10373 const unsigned char *p = buf;
10374 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +010010375 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010376#if defined(MBEDTLS_HAVE_TIME)
10377 uint64_t start;
10378#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010379#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010380#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010381 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +000010382#endif
10383#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010384
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010385 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010386 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010387 /*
10388 * Check version identifier
10389 */
10390
10391 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
10392 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10393
Teppo Järvelin0efac532019-10-04 13:21:08 +030010394 // use regular memcmp as session header is public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010395 if( memcmp( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010396 sizeof( ssl_serialized_session_header ) ) != 0 )
10397 {
10398 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10399 }
10400 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +010010401 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010402
10403 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010404 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010405 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010406#if defined(MBEDTLS_HAVE_TIME)
10407 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010408 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10409
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010410 start = ( (uint64_t) p[0] << 56 ) |
10411 ( (uint64_t) p[1] << 48 ) |
10412 ( (uint64_t) p[2] << 40 ) |
10413 ( (uint64_t) p[3] << 32 ) |
10414 ( (uint64_t) p[4] << 24 ) |
10415 ( (uint64_t) p[5] << 16 ) |
10416 ( (uint64_t) p[6] << 8 ) |
10417 ( (uint64_t) p[7] );
10418 p += 8;
10419
10420 session->start = (time_t) start;
10421#endif /* MBEDTLS_HAVE_TIME */
10422
10423 /*
10424 * Basic mandatory fields
10425 */
Hanno Becker88440552019-07-03 14:16:13 +010010426 {
10427 size_t const ciphersuite_len = 2;
10428#if defined(MBEDTLS_ZLIB_SUPPORT)
10429 size_t const compression_len = 1;
10430#else
10431 size_t const compression_len = 0;
10432#endif
10433 size_t const id_len_len = 1;
10434 size_t const id_len = 32;
10435 size_t const master_len = 48;
10436 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010437
Hanno Becker88440552019-07-03 14:16:13 +010010438 size_t const basic_len =
10439 ciphersuite_len +
10440 compression_len +
10441 id_len_len +
10442 id_len +
10443 master_len +
10444 verif_result_len;
10445
10446 if( basic_len > (size_t)( end - p ) )
10447 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10448 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010449
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010450 ciphersuite = (int)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010451 p += 2;
10452
Hanno Becker73f4cb12019-06-27 13:51:07 +010010453#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010454 session->ciphersuite = ciphersuite;
10455#else
10456 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010457 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010458 {
10459 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10460 }
10461#endif
10462
Hanno Becker88440552019-07-03 14:16:13 +010010463#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010464 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010465#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010466
10467 session->id_len = *p++;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010468 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10469 memcpy( session->id, p, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010470 p += 32;
10471
Teppo Järvelin91d79382019-10-02 09:09:31 +030010472 mbedtls_platform_memcpy( session->master, p, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010473 p += 48;
10474
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010475 session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010476 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010477
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010478 /* Immediately clear invalid pointer values that have been read, in case
10479 * we exit early before we replaced them with valid ones. */
10480#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010481#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010482 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010483#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010484 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010485#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010486#endif
10487#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10488 session->ticket = NULL;
10489#endif
10490
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010491 /*
10492 * Peer certificate
10493 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010494#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010495#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010496 if( 3 > (size_t)( end - p ) )
10497 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10498
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010499 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10500
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010501 p += 3;
10502
10503 if( cert_len == 0 )
10504 {
10505 session->peer_cert = NULL;
10506 }
10507 else
10508 {
10509 int ret;
10510
10511 if( cert_len > (size_t)( end - p ) )
10512 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10513
10514 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10515
10516 if( session->peer_cert == NULL )
10517 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10518
10519 mbedtls_x509_crt_init( session->peer_cert );
10520
10521 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10522 p, cert_len ) ) != 0 )
10523 {
10524 mbedtls_x509_crt_free( session->peer_cert );
10525 mbedtls_free( session->peer_cert );
10526 session->peer_cert = NULL;
10527 return( ret );
10528 }
10529
10530 p += cert_len;
10531 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010532#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010533 /* Deserialize CRT digest from the end of the ticket. */
10534 if( 2 > (size_t)( end - p ) )
10535 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10536
10537 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10538 session->peer_cert_digest_len = (size_t) *p++;
10539
10540 if( session->peer_cert_digest_len != 0 )
10541 {
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010542 mbedtls_md_handle_t md_info =
Hanno Becker2326d202019-06-06 14:54:55 +010010543 mbedtls_md_info_from_type( session->peer_cert_digest_type );
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010544 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Hanno Becker2326d202019-06-06 14:54:55 +010010545 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10546 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10548
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010549 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10551
10552 session->peer_cert_digest =
10553 mbedtls_calloc( 1, session->peer_cert_digest_len );
10554 if( session->peer_cert_digest == NULL )
10555 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10556
Teppo Järvelin91d79382019-10-02 09:09:31 +030010557 mbedtls_platform_memcpy( session->peer_cert_digest, p,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010558 session->peer_cert_digest_len );
10559 p += session->peer_cert_digest_len;
10560 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010561#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010562#endif /* MBEDTLS_X509_CRT_PARSE_C */
10563
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010564 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010565 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010566 */
10567#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10568 if( 3 > (size_t)( end - p ) )
10569 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10570
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010571 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010572 p += 3;
10573
10574 if( session->ticket_len != 0 )
10575 {
10576 if( session->ticket_len > (size_t)( end - p ) )
10577 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10578
10579 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10580 if( session->ticket == NULL )
10581 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10582
Teppo Järvelin91d79382019-10-02 09:09:31 +030010583 mbedtls_platform_memcpy( session->ticket, p, session->ticket_len );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010584 p += session->ticket_len;
10585 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010586
10587 if( 4 > (size_t)( end - p ) )
10588 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10589
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010590 session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010591 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010592#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10593
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010594 /*
10595 * Misc extension-related info
10596 */
10597#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10598 if( 1 > (size_t)( end - p ) )
10599 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10600
10601 session->mfl_code = *p++;
10602#endif
10603
10604#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10605 if( 1 > (size_t)( end - p ) )
10606 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10607
10608 session->trunc_hmac = *p++;
10609#endif
10610
10611#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10612 if( 1 > (size_t)( end - p ) )
10613 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10614
10615 session->encrypt_then_mac = *p++;
10616#endif
10617
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010618 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010619 if( p != end )
10620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10621
10622 return( 0 );
10623}
10624
10625/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010626 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010627 */
10628int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10629 const unsigned char *buf,
10630 size_t len )
10631{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010632 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010633
10634 if( ret != 0 )
10635 mbedtls_ssl_session_free( session );
10636
10637 return( ret );
10638}
10639
10640/*
Paul Bakker1961b702013-01-25 14:49:24 +010010641 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010642 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010643int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010644{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010645 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010646
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010647 if( ssl == NULL || ssl->conf == NULL )
10648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010650#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010651 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010652 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010653#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010654#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010655 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010656 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010657#endif
10658
Hanno Beckerb82350b2019-07-26 07:24:05 +010010659 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010660 return( ret );
10661}
10662
10663/*
10664 * Perform the SSL handshake
10665 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010666int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010667{
10668 int ret = 0;
10669
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010670 if( ssl == NULL || ssl->conf == NULL )
10671 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010675 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010677 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010678
10679 if( ret != 0 )
10680 break;
10681 }
10682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010684
10685 return( ret );
10686}
10687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010688#if defined(MBEDTLS_SSL_RENEGOTIATION)
10689#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010690/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010691 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010692 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010693static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010694{
10695 int ret;
10696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010698
10699 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010700 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10701 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010702
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010703 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010704 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010705 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010706 return( ret );
10707 }
10708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010710
10711 return( 0 );
10712}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010713#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010714
10715/*
10716 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010717 * - any side: calling mbedtls_ssl_renegotiate(),
10718 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10719 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010720 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010721 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010722 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010724static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010725{
10726 int ret;
10727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010729
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010730 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10731 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010732
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010733 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10734 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010735#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010736 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010737 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010738 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010739 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10740 MBEDTLS_SSL_IS_SERVER )
10741 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010742 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010743 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010744 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010745 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010746 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010747 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010748 }
10749#endif
10750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010751 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10752 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010754 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010756 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010757 return( ret );
10758 }
10759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010761
10762 return( 0 );
10763}
10764
10765/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010766 * Renegotiate current connection on client,
10767 * or request renegotiation on server
10768 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010769int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010770{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010771 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010772
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010773 if( ssl == NULL || ssl->conf == NULL )
10774 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010776#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010777 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010778 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010780 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10781 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010783 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010784
10785 /* Did we already try/start sending HelloRequest? */
10786 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010787 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010788
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010789 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010790 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010791#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010793#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010794 /*
10795 * On client, either start the renegotiation process or,
10796 * if already in progress, continue the handshake
10797 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010798 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010800 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10801 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010802
10803 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010805 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010806 return( ret );
10807 }
10808 }
10809 else
10810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010811 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010813 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010814 return( ret );
10815 }
10816 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010817#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010818
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010819 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010820}
10821
10822/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010823 * Check record counters and renegotiate if they're above the limit.
10824 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010825static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010826{
Andres AG2196c7f2016-12-15 17:01:16 +000010827 size_t ep_len = ssl_ep_len( ssl );
10828 int in_ctr_cmp;
10829 int out_ctr_cmp;
10830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010831 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10832 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010833 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010834 {
10835 return( 0 );
10836 }
10837
Teppo Järvelin0efac532019-10-04 13:21:08 +030010838 // use regular memcmp as counters are public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010839 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010840 ssl->conf->renego_period + ep_len, 8 - ep_len );
Teppo Järvelin650343c2019-10-03 15:36:59 +030010841 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010842 ssl->conf->renego_period + ep_len, 8 - ep_len );
10843
10844 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010845 {
10846 return( 0 );
10847 }
10848
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010850 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010851}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010852#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010853
10854/*
10855 * Receive application data decrypted from the SSL layer
10856 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010857int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010858{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010859 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010860 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010861
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010862 if( ssl == NULL || ssl->conf == NULL )
10863 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010867#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010868 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010870 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010871 return( ret );
10872
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010873 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010874 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010875 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010876 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010877 return( ret );
10878 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010879 }
10880#endif
10881
Hanno Becker4a810fb2017-05-24 16:27:30 +010010882 /*
10883 * Check if renegotiation is necessary and/or handshake is
10884 * in process. If yes, perform/continue, and fall through
10885 * if an unexpected packet is received while the client
10886 * is waiting for the ServerHello.
10887 *
10888 * (There is no equivalent to the last condition on
10889 * the server-side as it is not treated as within
10890 * a handshake while waiting for the ClientHello
10891 * after a renegotiation request.)
10892 */
10893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010894#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010895 ret = ssl_check_ctr_renegotiate( ssl );
10896 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10897 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010899 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010900 return( ret );
10901 }
10902#endif
10903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010904 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010906 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010907 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10908 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010910 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010911 return( ret );
10912 }
10913 }
10914
Hanno Beckere41158b2017-10-23 13:30:32 +010010915 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010916 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010917 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010918 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010919 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10920 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010921 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010922 ssl_set_timer( ssl,
10923 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010924 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010925
Hanno Becker327c93b2018-08-15 13:56:18 +010010926 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010927 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010928 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10929 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010930
Hanno Becker4a810fb2017-05-24 16:27:30 +010010931 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10932 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010933 }
10934
10935 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010936 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010937 {
10938 /*
10939 * OpenSSL sends empty messages to randomize the IV
10940 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010941 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010943 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010944 return( 0 );
10945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010946 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010947 return( ret );
10948 }
10949 }
10950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010951 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010954
Hanno Becker4a810fb2017-05-24 16:27:30 +010010955 /*
10956 * - For client-side, expect SERVER_HELLO_REQUEST.
10957 * - For server-side, expect CLIENT_HELLO.
10958 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10959 */
10960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010961#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010962 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10963 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010964 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010965 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010968
10969 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010970#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010971 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010972 {
10973 continue;
10974 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010975 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010976#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010977#if defined(MBEDTLS_SSL_PROTO_TLS)
10978 {
10979 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10980 }
10981#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010982 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010983#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010984
Hanno Becker4a810fb2017-05-24 16:27:30 +010010985#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010986 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10987 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010988 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010991
10992 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010993#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010994 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010995 {
10996 continue;
10997 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010998 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010999#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020011000#if defined(MBEDTLS_SSL_PROTO_TLS)
11001 {
11002 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
11003 }
11004#endif
Paul Bakker48916f92012-09-16 19:57:18 +000011005 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010011006#endif /* MBEDTLS_SSL_SRV_C */
11007
Hanno Becker21df7f92017-10-17 11:03:26 +010011008#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010011009 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011010 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
11011 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010011012 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011013 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
11014 {
11015 /*
11016 * Accept renegotiation request
11017 */
Paul Bakker48916f92012-09-16 19:57:18 +000011018
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011019 /* DTLS clients need to know renego is server-initiated */
11020#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011021 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010011022 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11023 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011024 {
11025 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
11026 }
11027#endif
11028 ret = ssl_start_renegotiation( ssl );
11029 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
11030 ret != 0 )
11031 {
11032 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
11033 return( ret );
11034 }
11035 }
11036 else
Hanno Becker21df7f92017-10-17 11:03:26 +010011037#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000011038 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010011039 /*
11040 * Refuse renegotiation
11041 */
11042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011043 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011045#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010011046 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000011047 {
Gilles Peskine92e44262017-05-10 17:27:49 +020011048 /* SSLv3 does not have a "no_renegotiation" warning, so
11049 we send a fatal alert and abort the connection. */
11050 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11051 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
11052 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011053 }
11054 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011055#endif /* MBEDTLS_SSL_PROTO_SSL3 */
11056#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11057 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011058 if( mbedtls_ssl_ver_geq(
11059 mbedtls_ssl_get_minor_ver( ssl ),
11060 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011061 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010011062 ret = mbedtls_ssl_send_alert_message( ssl,
11063 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11064 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
11065 if( ret != 0 )
11066 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000011067 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020011068 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011069#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
11070 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020011071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
11073 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020011074 }
Paul Bakker48916f92012-09-16 19:57:18 +000011075 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011076
Hanno Becker90333da2017-10-10 11:27:13 +010011077 /* At this point, we don't know whether the renegotiation has been
11078 * completed or not. The cases to consider are the following:
11079 * 1) The renegotiation is complete. In this case, no new record
11080 * has been read yet.
11081 * 2) The renegotiation is incomplete because the client received
11082 * an application data record while awaiting the ServerHello.
11083 * 3) The renegotiation is incomplete because the client received
11084 * a non-handshake, non-application data message while awaiting
11085 * the ServerHello.
11086 * In each of these case, looping will be the proper action:
11087 * - For 1), the next iteration will read a new record and check
11088 * if it's application data.
11089 * - For 2), the loop condition isn't satisfied as application data
11090 * is present, hence continue is the same as break
11091 * - For 3), the loop condition is satisfied and read_record
11092 * will re-deliver the message that was held back by the client
11093 * when expecting the ServerHello.
11094 */
11095 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000011096 }
Hanno Becker21df7f92017-10-17 11:03:26 +010011097#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011098 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011099 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011100 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011101 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011102 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011105 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011106 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011107 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011108 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011109 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011110#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011112 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
11113 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010011116 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011117 }
11118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011119 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000011120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
11122 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000011123 }
11124
11125 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020011126
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011127 /* We're going to return something now, cancel timer,
11128 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011129 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011130 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011131
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020011132#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011133 /* If we requested renego but received AppData, resend HelloRequest.
11134 * Do it now, after setting in_offt, to avoid taking this branch
11135 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011136#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010011137 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11138 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011139 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011140 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011141 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011143 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011144 return( ret );
11145 }
11146 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011147#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010011148#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000011149 }
11150
11151 n = ( len < ssl->in_msglen )
11152 ? len : ssl->in_msglen;
11153
Teppo Järvelin91d79382019-10-02 09:09:31 +030011154 mbedtls_platform_memcpy( buf, ssl->in_offt, n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011155 ssl->in_msglen -= n;
11156
Teppo Järvelincafb6c92020-01-08 09:19:07 +020011157 // clear incoming data after it's copied to buffer
11158 mbedtls_platform_memset(ssl->in_offt, 0, n);
11159
Paul Bakker5121ce52009-01-03 21:22:43 +000011160 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010011161 {
11162 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000011163 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010011164 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011165 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011166 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010011167 {
Paul Bakker5121ce52009-01-03 21:22:43 +000011168 /* more data available */
11169 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011170 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011173
Paul Bakker23986e52011-04-24 08:57:21 +000011174 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011175}
11176
11177/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011178 * Send application data to be encrypted by the SSL layer, taking care of max
11179 * fragment length and buffer size.
11180 *
11181 * According to RFC 5246 Section 6.2.1:
11182 *
11183 * Zero-length fragments of Application data MAY be sent as they are
11184 * potentially useful as a traffic analysis countermeasure.
11185 *
11186 * Therefore, it is possible that the input message length is 0 and the
11187 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000011188 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011189static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011190 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000011191{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020011192 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
11193 const size_t max_len = (size_t) ret;
11194
11195 if( ret < 0 )
11196 {
11197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
11198 return( ret );
11199 }
11200
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011201 if( len > max_len )
11202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011203#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011204 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011207 "maximum fragment length: %d > %d",
11208 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011209 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011210 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011211 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011212#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011213#if defined(MBEDTLS_SSL_PROTO_TLS)
11214 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011215 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011216 }
11217#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011218 }
Paul Bakker887bd502011-06-08 13:10:54 +000011219
Paul Bakker5121ce52009-01-03 21:22:43 +000011220 if( ssl->out_left != 0 )
11221 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011222 /*
11223 * The user has previously tried to send the data and
11224 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
11225 * written. In this case, we expect the high-level write function
11226 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
11227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011228 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011230 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011231 return( ret );
11232 }
11233 }
Paul Bakker887bd502011-06-08 13:10:54 +000011234 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000011235 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011236 /*
11237 * The user is trying to send a message the first time, so we need to
11238 * copy the data into the internal buffers and setup the data structure
11239 * to keep track of partial writes
11240 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011241 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011242 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Teppo Järvelin91d79382019-10-02 09:09:31 +030011243 mbedtls_platform_memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000011244
Hanno Becker67bc7c32018-08-06 11:33:50 +010011245 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000011246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011247 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000011248 return( ret );
11249 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011250 }
11251
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011252 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000011253}
11254
11255/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011256 * Write application data, doing 1/n-1 splitting if necessary.
11257 *
11258 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011259 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010011260 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011262#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011263static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011264 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011265{
11266 int ret;
11267
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011268 if( ssl->conf->cbc_record_splitting ==
11269 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011270 len <= 1 ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011271 mbedtls_ssl_ver_gt(
11272 mbedtls_ssl_get_minor_ver( ssl ),
11273 MBEDTLS_SSL_MINOR_VERSION_1 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011274 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
11275 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011276 {
11277 return( ssl_write_real( ssl, buf, len ) );
11278 }
11279
11280 if( ssl->split_done == 0 )
11281 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011282 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011283 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011284 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011285 }
11286
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011287 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
11288 return( ret );
11289 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011290
11291 return( ret + 1 );
11292}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011293#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011294
11295/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011296 * Write application data (public-facing wrapper)
11297 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011298int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011299{
11300 int ret;
11301
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011303
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011304 if( ssl == NULL || ssl->conf == NULL )
11305 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11306
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011307#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011308 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
11309 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011310 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011311 return( ret );
11312 }
11313#endif
11314
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011315 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011316 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011317 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011318 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020011319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011320 return( ret );
11321 }
11322 }
11323
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011324#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011325 ret = ssl_write_split( ssl, buf, len );
11326#else
11327 ret = ssl_write_real( ssl, buf, len );
11328#endif
11329
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011331
11332 return( ret );
11333}
11334
11335/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011336 * Notify the peer that the connection is being closed
11337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011338int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011339{
11340 int ret;
11341
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011342 if( ssl == NULL || ssl->conf == NULL )
11343 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011346
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011347 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011348 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011350 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000011351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011352 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
11353 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11354 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011356 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011357 return( ret );
11358 }
11359 }
11360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011362
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011363 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000011364}
11365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011366void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000011367{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011368 if( transform == NULL )
11369 return;
11370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011371#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000011372 deflateEnd( &transform->ctx_deflate );
11373 inflateEnd( &transform->ctx_inflate );
11374#endif
11375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011376 mbedtls_cipher_free( &transform->cipher_ctx_enc );
11377 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020011378
Hanno Becker92231322018-01-03 15:32:51 +000011379#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011380 mbedtls_md_free( &transform->md_ctx_enc );
11381 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000011382#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011383
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011384 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011385}
11386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011387#if defined(MBEDTLS_X509_CRT_PARSE_C)
11388static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011389{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011390 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011391
11392 while( cur != NULL )
11393 {
11394 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011395 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011396 cur = next;
11397 }
11398}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011399#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011400
Hanno Becker0271f962018-08-16 13:23:47 +010011401#if defined(MBEDTLS_SSL_PROTO_DTLS)
11402
11403static void ssl_buffering_free( mbedtls_ssl_context *ssl )
11404{
11405 unsigned offset;
11406 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11407
11408 if( hs == NULL )
11409 return;
11410
Hanno Becker283f5ef2018-08-24 09:34:47 +010011411 ssl_free_buffered_record( ssl );
11412
Hanno Becker0271f962018-08-16 13:23:47 +010011413 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010011414 ssl_buffering_free_slot( ssl, offset );
11415}
11416
11417static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
11418 uint8_t slot )
11419{
11420 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11421 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010011422
11423 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
11424 return;
11425
Hanno Beckere605b192018-08-21 15:59:07 +010011426 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010011427 {
Hanno Beckere605b192018-08-21 15:59:07 +010011428 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010011429 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010011430 mbedtls_free( hs_buf->data );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020011431 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010011432 }
11433}
11434
11435#endif /* MBEDTLS_SSL_PROTO_DTLS */
11436
Gilles Peskine9b562d52018-04-25 20:32:43 +020011437void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011438{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011439 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11440
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011441 if( handshake == NULL )
11442 return;
11443
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011444#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11445 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11446 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011447 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011448 handshake->async_in_progress = 0;
11449 }
11450#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11451
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011452#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11453 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11454 mbedtls_md5_free( &handshake->fin_md5 );
11455 mbedtls_sha1_free( &handshake->fin_sha1 );
11456#endif
11457#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11458#if defined(MBEDTLS_SHA256_C)
11459 mbedtls_sha256_free( &handshake->fin_sha256 );
11460#endif
11461#if defined(MBEDTLS_SHA512_C)
11462 mbedtls_sha512_free( &handshake->fin_sha512 );
11463#endif
11464#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011466#if defined(MBEDTLS_DHM_C)
11467 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011468#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011469#if defined(MBEDTLS_ECDH_C)
11470 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011471#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011472#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011473 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011474#if defined(MBEDTLS_SSL_CLI_C)
11475 mbedtls_free( handshake->ecjpake_cache );
11476 handshake->ecjpake_cache = NULL;
11477 handshake->ecjpake_cache_len = 0;
11478#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011479#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011480
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011481#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11482 if( handshake->psk != NULL )
11483 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011484 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011485 mbedtls_free( handshake->psk );
11486 }
11487#endif
11488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011489#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11490 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011491 /*
11492 * Free only the linked list wrapper, not the keys themselves
11493 * since the belong to the SNI callback
11494 */
11495 if( handshake->sni_key_cert != NULL )
11496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011497 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011498
11499 while( cur != NULL )
11500 {
11501 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011502 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011503 cur = next;
11504 }
11505 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011506#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011507
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011508#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011509 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011510 if( handshake->ecrs_peer_cert != NULL )
11511 {
11512 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11513 mbedtls_free( handshake->ecrs_peer_cert );
11514 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011515#endif
11516
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011517#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11518 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11519 mbedtls_pk_free( &handshake->peer_pubkey );
11520#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011522#if defined(MBEDTLS_SSL_PROTO_DTLS)
11523 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011524 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011525 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011526#endif
11527
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011528 mbedtls_platform_zeroize( handshake,
11529 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011530}
11531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011532void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011533{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011534 if( session == NULL )
11535 return;
11536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011537#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011538 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011539#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011540
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011541#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011542 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011543#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011544
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011545 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011546}
11547
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011548#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011549
11550#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11551#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11552#else
11553#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11554#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11555
11556#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11557#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11558#else
11559#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11560#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11561
11562#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11563#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11564#else
11565#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11566#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11567
11568#if defined(MBEDTLS_SSL_ALPN)
11569#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11570#else
11571#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11572#endif /* MBEDTLS_SSL_ALPN */
11573
11574#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11575#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11576#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11577#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11578
11579#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11580 ( (uint32_t) ( \
11581 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11582 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11583 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11584 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11585 0u ) )
11586
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011587static unsigned char ssl_serialized_context_header[] = {
11588 MBEDTLS_VERSION_MAJOR,
11589 MBEDTLS_VERSION_MINOR,
11590 MBEDTLS_VERSION_PATCH,
11591 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11592 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011593 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11594 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11595 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011596};
11597
Paul Bakker5121ce52009-01-03 21:22:43 +000011598/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011599 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011600 *
11601 * The format of the serialized data is:
11602 * (in the presentation language of TLS, RFC 8446 section 3)
11603 *
11604 * // header
11605 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011606 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011607 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011608 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011609 * Note: When updating the format, remember to keep these
11610 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011611 *
11612 * // session sub-structure
11613 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11614 * // transform sub-structure
11615 * uint8 random[64]; // ServerHello.random+ClientHello.random
11616 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11617 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11618 * // fields from ssl_context
11619 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11620 * uint64 in_window_top; // DTLS: last validated record seq_num
11621 * uint64 in_window; // DTLS: bitmask for replay protection
11622 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11623 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11624 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11625 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11626 *
11627 * Note that many fields of the ssl_context or sub-structures are not
11628 * serialized, as they fall in one of the following categories:
11629 *
11630 * 1. forced value (eg in_left must be 0)
11631 * 2. pointer to dynamically-allocated memory (eg session, transform)
11632 * 3. value can be re-derived from other data (eg session keys from MS)
11633 * 4. value was temporary (eg content of input buffer)
11634 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011635 */
11636int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11637 unsigned char *buf,
11638 size_t buf_len,
11639 size_t *olen )
11640{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011641 unsigned char *p = buf;
11642 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011643 size_t session_len;
11644 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011645
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011646 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011647 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11648 * this function's documentation.
11649 *
11650 * These are due to assumptions/limitations in the implementation. Some of
11651 * them are likely to stay (no handshake in progress) some might go away
11652 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011653 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011654 /* The initial handshake must be over */
11655 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011656 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011657 if( ssl->handshake != NULL )
11658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11659 /* Double-check that sub-structures are indeed ready */
11660 if( ssl->transform == NULL || ssl->session == NULL )
11661 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11662 /* There must be no pending incoming or outgoing data */
11663 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11664 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11665 if( ssl->out_left != 0 )
11666 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11667 /* Protocol must be DLTS, not TLS */
11668 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11669 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11670 /* Version must be 1.2 */
11671 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11672 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11673 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11674 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11675 /* We must be using an AEAD ciphersuite */
11676 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11677 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11678 /* Renegotiation must not be enabled */
11679 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11680 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011681
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011682 /*
11683 * Version and format identifier
11684 */
11685 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011686
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011687 if( used <= buf_len )
11688 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011689 mbedtls_platform_memcpy( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011690 sizeof( ssl_serialized_context_header ) );
11691 p += sizeof( ssl_serialized_context_header );
11692 }
11693
11694 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011695 * Session (length + data)
11696 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011697 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011698 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11699 return( ret );
11700
11701 used += 4 + session_len;
11702 if( used <= buf_len )
11703 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011704 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011705
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011706 ret = ssl_session_save( ssl->session, 1,
11707 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011708 if( ret != 0 )
11709 return( ret );
11710
11711 p += session_len;
11712 }
11713
11714 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011715 * Transform
11716 */
11717 used += sizeof( ssl->transform->randbytes );
11718 if( used <= buf_len )
11719 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011720 mbedtls_platform_memcpy( p, ssl->transform->randbytes,
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011721 sizeof( ssl->transform->randbytes ) );
11722 p += sizeof( ssl->transform->randbytes );
11723 }
11724
11725#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11726 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11727 if( used <= buf_len )
11728 {
11729 *p++ = ssl->transform->in_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011730 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11731 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011732 p += ssl->transform->in_cid_len;
11733
11734 *p++ = ssl->transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011735 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11736 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011737 p += ssl->transform->out_cid_len;
11738 }
11739#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11740
11741 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011742 * Saved fields from top-level ssl_context structure
11743 */
11744#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11745 used += 4;
11746 if( used <= buf_len )
11747 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011748 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011749 }
11750#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11751
11752#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11753 used += 16;
11754 if( used <= buf_len )
11755 {
11756 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11757 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11758 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11759 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11760 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11761 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11762 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11763 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11764
11765 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11766 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11767 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11768 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11769 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11770 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11771 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11772 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11773 }
11774#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11775
11776#if defined(MBEDTLS_SSL_PROTO_DTLS)
11777 used += 1;
11778 if( used <= buf_len )
11779 {
11780 *p++ = ssl->disable_datagram_packing;
11781 }
11782#endif /* MBEDTLS_SSL_PROTO_DTLS */
11783
11784 used += 8;
11785 if( used <= buf_len )
11786 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011787 mbedtls_platform_memcpy( p, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011788 p += 8;
11789 }
11790
11791#if defined(MBEDTLS_SSL_PROTO_DTLS)
11792 used += 2;
11793 if( used <= buf_len )
11794 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011795 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011796 }
11797#endif /* MBEDTLS_SSL_PROTO_DTLS */
11798
11799#if defined(MBEDTLS_SSL_ALPN)
11800 {
11801 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011802 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011803 : 0;
11804
11805 used += 1 + alpn_len;
11806 if( used <= buf_len )
11807 {
11808 *p++ = alpn_len;
11809
11810 if( ssl->alpn_chosen != NULL )
11811 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011812 mbedtls_platform_memcpy( p, ssl->alpn_chosen, alpn_len );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011813 p += alpn_len;
11814 }
11815 }
11816 }
11817#endif /* MBEDTLS_SSL_ALPN */
11818
11819 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011820 * Done
11821 */
11822 *olen = used;
11823
11824 if( used > buf_len )
11825 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011826
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011827 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11828
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011829 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011830}
11831
11832/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011833 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011834 *
11835 * This internal version is wrapped by a public function that cleans up in
11836 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011837 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011838static int ssl_context_load( mbedtls_ssl_context *ssl,
11839 const unsigned char *buf,
11840 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011841{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011842 const unsigned char *p = buf;
11843 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011844 size_t session_len;
11845 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011846
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011847 /*
11848 * The context should have been freshly setup or reset.
11849 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011850 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011851 * renegotiating, or if the user mistakenly loaded a session first.)
11852 */
11853 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11854 ssl->session != NULL )
11855 {
11856 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11857 }
11858
11859 /*
11860 * We can't check that the config matches the initial one, but we can at
11861 * least check it matches the requirements for serializing.
11862 */
11863 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011864 mbedtls_ssl_ver_lt(
11865 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ),
11866 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11867 mbedtls_ssl_ver_gt(
11868 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ),
11869 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11870 mbedtls_ssl_ver_lt(
11871 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ),
11872 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
11873 mbedtls_ssl_ver_gt(
11874 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ),
11875 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011876 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011877 {
11878 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11879 }
11880
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011881 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11882
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011883 /*
11884 * Check version identifier
11885 */
11886 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11887 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11888
Teppo Järvelin650343c2019-10-03 15:36:59 +030011889 // use regular memcmp as header is not that critical
11890 if( memcmp( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011891 sizeof( ssl_serialized_context_header ) ) != 0 )
11892 {
11893 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11894 }
11895 p += sizeof( ssl_serialized_context_header );
11896
11897 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011898 * Session
11899 */
11900 if( (size_t)( end - p ) < 4 )
11901 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11902
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011903 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011904 p += 4;
11905
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011906 /* This has been allocated by ssl_handshake_init(), called by
11907 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11908 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011909 ssl->session_in = ssl->session;
11910 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011911 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011912
11913 if( (size_t)( end - p ) < session_len )
11914 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11915
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011916 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011917 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011918 {
11919 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011920 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011921 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011922
11923 p += session_len;
11924
11925 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011926 * Transform
11927 */
11928
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011929 /* This has been allocated by ssl_handshake_init(), called by
11930 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11931 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011932 ssl->transform_in = ssl->transform;
11933 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011934 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011935
11936 /* Read random bytes and populate structure */
11937 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11938 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11939
11940 ret = ssl_populate_transform( ssl->transform,
11941 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11942 ssl->session->master,
11943#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11944#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11945 ssl->session->encrypt_then_mac,
11946#endif
11947#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11948 ssl->session->trunc_hmac,
11949#endif
11950#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11951#if defined(MBEDTLS_ZLIB_SUPPORT)
11952 ssl->session->compression,
11953#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011954 p, /* currently pointing to randbytes */
11955 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11956 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11957 ssl );
11958 if( ret != 0 )
11959 return( ret );
11960
11961 p += sizeof( ssl->transform->randbytes );
11962
11963#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11964 /* Read connection IDs and store them */
11965 if( (size_t)( end - p ) < 1 )
11966 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11967
11968 ssl->transform->in_cid_len = *p++;
11969
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011970 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011971 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11972
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011973 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11974 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011975 p += ssl->transform->in_cid_len;
11976
11977 ssl->transform->out_cid_len = *p++;
11978
11979 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11980 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11981
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011982 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11983 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011984 p += ssl->transform->out_cid_len;
11985#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11986
11987 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011988 * Saved fields from top-level ssl_context structure
11989 */
11990#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11991 if( (size_t)( end - p ) < 4 )
11992 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11993
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011994 ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011995 p += 4;
11996#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11997
11998#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11999 if( (size_t)( end - p ) < 16 )
12000 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12001
12002 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
12003 ( (uint64_t) p[1] << 48 ) |
12004 ( (uint64_t) p[2] << 40 ) |
12005 ( (uint64_t) p[3] << 32 ) |
12006 ( (uint64_t) p[4] << 24 ) |
12007 ( (uint64_t) p[5] << 16 ) |
12008 ( (uint64_t) p[6] << 8 ) |
12009 ( (uint64_t) p[7] );
12010 p += 8;
12011
12012 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
12013 ( (uint64_t) p[1] << 48 ) |
12014 ( (uint64_t) p[2] << 40 ) |
12015 ( (uint64_t) p[3] << 32 ) |
12016 ( (uint64_t) p[4] << 24 ) |
12017 ( (uint64_t) p[5] << 16 ) |
12018 ( (uint64_t) p[6] << 8 ) |
12019 ( (uint64_t) p[7] );
12020 p += 8;
12021#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
12022
12023#if defined(MBEDTLS_SSL_PROTO_DTLS)
12024 if( (size_t)( end - p ) < 1 )
12025 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12026
12027 ssl->disable_datagram_packing = *p++;
12028#endif /* MBEDTLS_SSL_PROTO_DTLS */
12029
12030 if( (size_t)( end - p ) < 8 )
12031 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12032
Teppo Järvelin91d79382019-10-02 09:09:31 +030012033 mbedtls_platform_memcpy( ssl->cur_out_ctr, p, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012034 p += 8;
12035
12036#if defined(MBEDTLS_SSL_PROTO_DTLS)
12037 if( (size_t)( end - p ) < 2 )
12038 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030012039 ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012040 p += 2;
12041#endif /* MBEDTLS_SSL_PROTO_DTLS */
12042
12043#if defined(MBEDTLS_SSL_ALPN)
12044 {
12045 uint8_t alpn_len;
12046 const char **cur;
12047
12048 if( (size_t)( end - p ) < 1 )
12049 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12050
12051 alpn_len = *p++;
12052
12053 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
12054 {
12055 /* alpn_chosen should point to an item in the configured list */
12056 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
12057 {
12058 if( strlen( *cur ) == alpn_len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +030012059 mbedtls_platform_memcmp( p, cur, alpn_len ) == 0 )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012060 {
12061 ssl->alpn_chosen = *cur;
12062 break;
12063 }
12064 }
12065 }
12066
12067 /* can only happen on conf mismatch */
12068 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
12069 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12070
12071 p += alpn_len;
12072 }
12073#endif /* MBEDTLS_SSL_ALPN */
12074
12075 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012076 * Forced fields from top-level ssl_context structure
12077 *
12078 * Most of them already set to the correct value by mbedtls_ssl_init() and
12079 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
12080 */
12081 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
12082
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012083#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012084 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012085#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
12086#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012087 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012088#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012089
Hanno Becker83985822019-08-30 10:42:49 +010012090 /* Adjust pointers for header fields of outgoing records to
12091 * the given transform, accounting for explicit IV and CID. */
12092 ssl_update_out_pointers( ssl, ssl->transform );
12093
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012094#if defined(MBEDTLS_SSL_PROTO_DTLS)
12095 ssl->in_epoch = 1;
12096#endif
12097
12098 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
12099 * which we don't want - otherwise we'd end up freeing the wrong transform
12100 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
12101 if( ssl->handshake != NULL )
12102 {
12103 mbedtls_ssl_handshake_free( ssl );
12104 mbedtls_free( ssl->handshake );
12105 ssl->handshake = NULL;
12106 }
12107
12108 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020012109 * Done - should have consumed entire buffer
12110 */
12111 if( p != end )
12112 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012113
12114 return( 0 );
12115}
12116
12117/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020012118 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012119 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012120int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012121 const unsigned char *buf,
12122 size_t len )
12123{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012124 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012125
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012126 if( ret != 0 )
12127 mbedtls_ssl_free( context );
12128
12129 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012130}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020012131#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012132
12133/*
Paul Bakker5121ce52009-01-03 21:22:43 +000012134 * Free an SSL context
12135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012136void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000012137{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020012138 if( ssl == NULL )
12139 return;
12140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012142
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012143 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012144 {
Angus Grattond8213d02016-05-25 20:56:48 +100012145 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012146 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012147 }
12148
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012149 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012150 {
Angus Grattond8213d02016-05-25 20:56:48 +100012151 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012152 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012153 }
12154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012155#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020012156 if( ssl->compress_buf != NULL )
12157 {
Angus Grattond8213d02016-05-25 20:56:48 +100012158 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012159 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020012160 }
12161#endif
12162
Paul Bakker48916f92012-09-16 19:57:18 +000012163 if( ssl->transform )
12164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012165 mbedtls_ssl_transform_free( ssl->transform );
12166 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000012167 }
12168
12169 if( ssl->handshake )
12170 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020012171 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012172 mbedtls_ssl_transform_free( ssl->transform_negotiate );
12173 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012175 mbedtls_free( ssl->handshake );
12176 mbedtls_free( ssl->transform_negotiate );
12177 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012178 }
12179
Paul Bakkerc0463502013-02-14 11:19:38 +010012180 if( ssl->session )
12181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012182 mbedtls_ssl_session_free( ssl->session );
12183 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010012184 }
12185
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030012186#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020012187 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012188 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012189 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012190 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000012191 }
Paul Bakker0be444a2013-08-27 21:55:01 +020012192#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000012193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012194#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
12195 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000012196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
12198 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000012199 }
12200#endif
12201
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012202#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012203 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020012204#endif
12205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000012207
Paul Bakker86f04f42013-02-14 11:20:09 +010012208 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012209 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012210}
12211
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012212/*
12213 * Initialze mbedtls_ssl_config
12214 */
12215void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
12216{
12217 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010012218
12219#if !defined(MBEDTLS_SSL_PROTO_TLS)
12220 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
12221#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012222}
12223
Simon Butcherc97b6972015-12-27 23:48:17 +000012224#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012225#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012226static int ssl_preset_default_hashes[] = {
12227#if defined(MBEDTLS_SHA512_C)
12228 MBEDTLS_MD_SHA512,
12229 MBEDTLS_MD_SHA384,
12230#endif
12231#if defined(MBEDTLS_SHA256_C)
12232 MBEDTLS_MD_SHA256,
12233 MBEDTLS_MD_SHA224,
12234#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020012235#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012236 MBEDTLS_MD_SHA1,
12237#endif
12238 MBEDTLS_MD_NONE
12239};
Simon Butcherc97b6972015-12-27 23:48:17 +000012240#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012241#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012242
Hanno Becker73f4cb12019-06-27 13:51:07 +010012243#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012244static int ssl_preset_suiteb_ciphersuites[] = {
12245 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
12246 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
12247 0
12248};
Hanno Becker73f4cb12019-06-27 13:51:07 +010012249#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012250
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012251#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012252#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012253static int ssl_preset_suiteb_hashes[] = {
12254 MBEDTLS_MD_SHA256,
12255 MBEDTLS_MD_SHA384,
12256 MBEDTLS_MD_NONE
12257};
12258#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012259#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012260
Hanno Beckerc1096e72019-06-19 12:30:41 +010012261#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012262static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010012263#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012264 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012265#endif
12266#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012267 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012268#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012269 MBEDTLS_ECP_DP_NONE
12270};
12271#endif
12272
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012273/*
Tillmann Karras588ad502015-09-25 04:27:22 +020012274 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012275 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012276int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012277 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012278{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012279#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012280 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012281#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012282
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020012283 /* Use the functions here so that they are covered in tests,
12284 * but otherwise access member directly for efficiency */
12285 mbedtls_ssl_conf_endpoint( conf, endpoint );
12286 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012287
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012288 /*
12289 * Things that are common to all presets
12290 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012291#if defined(MBEDTLS_SSL_CLI_C)
12292 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
12293 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012294#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012295 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012296#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012297#if defined(MBEDTLS_SSL_SESSION_TICKETS)
12298 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
12299#endif
12300 }
12301#endif
12302
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012303#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012304 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012305#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012306
12307#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
12308 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
12309#endif
12310
12311#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010012312#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012313 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012314#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
12315#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030012316 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030012317 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012318#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012319#endif
12320
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010012321#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
12322 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
12323#endif
12324
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012325#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012326 conf->f_cookie_write = ssl_cookie_write_dummy;
12327 conf->f_cookie_check = ssl_cookie_check_dummy;
12328#endif
12329
Hanno Becker7f376f42019-06-12 16:20:48 +010012330#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
12331 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012332 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
12333#endif
12334
Janos Follath088ce432017-04-10 12:42:31 +010012335#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012336#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010012337 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012338#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
12339#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010012340
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012341#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010012342#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012343 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012344#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
12345#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012346 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012347#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
12348#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012349
12350#if defined(MBEDTLS_SSL_RENEGOTIATION)
12351 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020012352 mbedtls_platform_memset( conf->renego_period, 0x00, 2 );
12353 mbedtls_platform_memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012354#endif
12355
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012356#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
12357 if( endpoint == MBEDTLS_SSL_IS_SERVER )
12358 {
Hanno Becker00d0a682017-10-04 13:14:29 +010012359 const unsigned char dhm_p[] =
12360 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
12361 const unsigned char dhm_g[] =
12362 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
12363
Hanno Beckera90658f2017-10-04 15:29:08 +010012364 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
12365 dhm_p, sizeof( dhm_p ),
12366 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012367 {
12368 return( ret );
12369 }
12370 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020012371#endif
12372
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012373 /*
12374 * Preset-specific defaults
12375 */
12376 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012377 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012378 /*
12379 * NSA Suite B
12380 */
12381 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010012382#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012383 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010012384#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12385#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012386 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010012387#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12388#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012389 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012390#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12391#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012392 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012393#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012394
Hanno Becker73f4cb12019-06-27 13:51:07 +010012395#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012396 conf->ciphersuite_list[0] =
12397 conf->ciphersuite_list[1] =
12398 conf->ciphersuite_list[2] =
12399 conf->ciphersuite_list[3] =
12400 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010012401#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012402
12403#if defined(MBEDTLS_X509_CRT_PARSE_C)
12404 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012405#endif
12406
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012407#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012408#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012409 conf->sig_hashes = ssl_preset_suiteb_hashes;
12410#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012411#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012412
12413#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012414#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012415 conf->curve_list = ssl_preset_suiteb_curves;
12416#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012417#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020012418 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012419
12420 /*
12421 * Default
12422 */
12423 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010012424#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012425 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
12426 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
12427 MBEDTLS_SSL_MIN_MAJOR_VERSION :
12428 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012429#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12430#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012431 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
12432 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
12433 MBEDTLS_SSL_MIN_MINOR_VERSION :
12434 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012435#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020012436 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012437 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
12438#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010012439#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12440#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
12441 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12442#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12443#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12444 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12445#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012446
Hanno Becker73f4cb12019-06-27 13:51:07 +010012447#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012448 conf->ciphersuite_list[0] =
12449 conf->ciphersuite_list[1] =
12450 conf->ciphersuite_list[2] =
12451 conf->ciphersuite_list[3] =
12452 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012453#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012454
12455#if defined(MBEDTLS_X509_CRT_PARSE_C)
12456 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12457#endif
12458
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012459#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012460#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012461 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012462#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012463#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012464
12465#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012466#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012467 conf->curve_list = mbedtls_ecp_grp_id_list();
12468#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012469#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012470
12471#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12472 conf->dhm_min_bitlen = 1024;
12473#endif
12474 }
12475
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012476 return( 0 );
12477}
12478
12479/*
12480 * Free mbedtls_ssl_config
12481 */
12482void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12483{
12484#if defined(MBEDTLS_DHM_C)
12485 mbedtls_mpi_free( &conf->dhm_P );
12486 mbedtls_mpi_free( &conf->dhm_G );
12487#endif
12488
12489#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12490 if( conf->psk != NULL )
12491 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012492 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012493 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012494 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012495 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012496 }
12497
12498 if( conf->psk_identity != NULL )
12499 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012500 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012501 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012502 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012503 conf->psk_identity_len = 0;
12504 }
12505#endif
12506
12507#if defined(MBEDTLS_X509_CRT_PARSE_C)
12508 ssl_key_cert_free( conf->key_cert );
12509#endif
12510
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012511 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012512}
12513
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012514#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012515 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12516 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012517/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012518 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012520unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012521{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012522#if defined(MBEDTLS_RSA_C)
12523 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12524 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012525#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012526#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012527 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12528 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012529#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012530 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012531}
12532
Hanno Becker7e5437a2017-04-28 17:15:26 +010012533unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12534{
12535 switch( type ) {
12536 case MBEDTLS_PK_RSA:
12537 return( MBEDTLS_SSL_SIG_RSA );
12538 case MBEDTLS_PK_ECDSA:
12539 case MBEDTLS_PK_ECKEY:
12540 return( MBEDTLS_SSL_SIG_ECDSA );
12541 default:
12542 return( MBEDTLS_SSL_SIG_ANON );
12543 }
12544}
12545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012546mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012547{
12548 switch( sig )
12549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012550#if defined(MBEDTLS_RSA_C)
12551 case MBEDTLS_SSL_SIG_RSA:
12552 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012553#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012554#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012555 case MBEDTLS_SSL_SIG_ECDSA:
12556 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012557#endif
12558 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012559 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012560 }
12561}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012562#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012563
Hanno Becker7e5437a2017-04-28 17:15:26 +010012564#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12565 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12566
12567/* Find an entry in a signature-hash set matching a given hash algorithm. */
12568mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12569 mbedtls_pk_type_t sig_alg )
12570{
12571 switch( sig_alg )
12572 {
12573 case MBEDTLS_PK_RSA:
12574 return( set->rsa );
12575 case MBEDTLS_PK_ECDSA:
12576 return( set->ecdsa );
12577 default:
12578 return( MBEDTLS_MD_NONE );
12579 }
12580}
12581
12582/* Add a signature-hash-pair to a signature-hash set */
12583void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12584 mbedtls_pk_type_t sig_alg,
12585 mbedtls_md_type_t md_alg )
12586{
12587 switch( sig_alg )
12588 {
12589 case MBEDTLS_PK_RSA:
12590 if( set->rsa == MBEDTLS_MD_NONE )
12591 set->rsa = md_alg;
12592 break;
12593
12594 case MBEDTLS_PK_ECDSA:
12595 if( set->ecdsa == MBEDTLS_MD_NONE )
12596 set->ecdsa = md_alg;
12597 break;
12598
12599 default:
12600 break;
12601 }
12602}
12603
12604/* Allow exactly one hash algorithm for each signature. */
12605void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12606 mbedtls_md_type_t md_alg )
12607{
12608 set->rsa = md_alg;
12609 set->ecdsa = md_alg;
12610}
12611
12612#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12613 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12614
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012615/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012616 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012618mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012619{
12620 switch( hash )
12621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012622#if defined(MBEDTLS_MD5_C)
12623 case MBEDTLS_SSL_HASH_MD5:
12624 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012625#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012626#if defined(MBEDTLS_SHA1_C)
12627 case MBEDTLS_SSL_HASH_SHA1:
12628 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012629#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012630#if defined(MBEDTLS_SHA256_C)
12631 case MBEDTLS_SSL_HASH_SHA224:
12632 return( MBEDTLS_MD_SHA224 );
12633 case MBEDTLS_SSL_HASH_SHA256:
12634 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012635#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012636#if defined(MBEDTLS_SHA512_C)
12637 case MBEDTLS_SSL_HASH_SHA384:
12638 return( MBEDTLS_MD_SHA384 );
12639 case MBEDTLS_SSL_HASH_SHA512:
12640 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012641#endif
12642 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012643 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012644 }
12645}
12646
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012647/*
12648 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12649 */
12650unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12651{
12652 switch( md )
12653 {
12654#if defined(MBEDTLS_MD5_C)
12655 case MBEDTLS_MD_MD5:
12656 return( MBEDTLS_SSL_HASH_MD5 );
12657#endif
12658#if defined(MBEDTLS_SHA1_C)
12659 case MBEDTLS_MD_SHA1:
12660 return( MBEDTLS_SSL_HASH_SHA1 );
12661#endif
12662#if defined(MBEDTLS_SHA256_C)
12663 case MBEDTLS_MD_SHA224:
12664 return( MBEDTLS_SSL_HASH_SHA224 );
12665 case MBEDTLS_MD_SHA256:
12666 return( MBEDTLS_SSL_HASH_SHA256 );
12667#endif
12668#if defined(MBEDTLS_SHA512_C)
12669 case MBEDTLS_MD_SHA384:
12670 return( MBEDTLS_SSL_HASH_SHA384 );
12671 case MBEDTLS_MD_SHA512:
12672 return( MBEDTLS_SSL_HASH_SHA512 );
12673#endif
12674 default:
12675 return( MBEDTLS_SSL_HASH_NONE );
12676 }
12677}
12678
Hanno Beckeree902df2019-08-23 13:47:47 +010012679#if defined(MBEDTLS_USE_TINYCRYPT)
12680/*
12681 * Check if a curve proposed by the peer is in our list.
12682 * Return 0 if we're willing to use it, -1 otherwise.
12683 */
12684int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12685 mbedtls_uecc_group_id grp_id )
12686{
12687 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12688 if( own_ec_id == grp_id )
12689 return( 0 );
12690 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12691
12692 return( -1 );
12693}
12694#endif /* MBEDTLS_USE_TINYCRYPT */
12695
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012696#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012697/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012698 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012699 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012700 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012701int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12702 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012703{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012704 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12705 if( own_ec_id == grp_id )
12706 return( 0 );
12707 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012708
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012709 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012710}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012711#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012712
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012713#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012714/*
12715 * Check if a hash proposed by the peer is in our list.
12716 * Return 0 if we're willing to use it, -1 otherwise.
12717 */
12718int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12719 mbedtls_md_type_t md )
12720{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012721 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12722 if( md_alg == md )
12723 return( 0 );
12724 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012725
12726 return( -1 );
12727}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012728#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012730#if defined(MBEDTLS_X509_CRT_PARSE_C)
12731int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012732 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012733 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012734 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012735{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012736 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012737#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012738 int usage = 0;
12739#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012740#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012741 const char *ext_oid;
12742 size_t ext_len;
12743#endif
12744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012745#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12746 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012747 ((void) cert);
12748 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012749 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012750#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012752#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12753 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012754 {
12755 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012756 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012758 case MBEDTLS_KEY_EXCHANGE_RSA:
12759 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012760 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012761 break;
12762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012763 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12764 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12765 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12766 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012767 break;
12768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012769 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12770 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012771 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012772 break;
12773
12774 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012775 case MBEDTLS_KEY_EXCHANGE_NONE:
12776 case MBEDTLS_KEY_EXCHANGE_PSK:
12777 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12778 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012779 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012780 usage = 0;
12781 }
12782 }
12783 else
12784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012785 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12786 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012787 }
12788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012789 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012790 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012791 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012792 ret = -1;
12793 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012794#else
12795 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012796#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012798#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12799 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012801 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12802 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012803 }
12804 else
12805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012806 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12807 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012808 }
12809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012810 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012811 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012812 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012813 ret = -1;
12814 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012815#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012816
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012817 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012818}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012819#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012820
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012821#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12822 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12823int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12824 unsigned char *output,
12825 unsigned char *data, size_t data_len )
12826{
12827 int ret = 0;
12828 mbedtls_md5_context mbedtls_md5;
12829 mbedtls_sha1_context mbedtls_sha1;
12830
12831 mbedtls_md5_init( &mbedtls_md5 );
12832 mbedtls_sha1_init( &mbedtls_sha1 );
12833
12834 /*
12835 * digitally-signed struct {
12836 * opaque md5_hash[16];
12837 * opaque sha_hash[20];
12838 * };
12839 *
12840 * md5_hash
12841 * MD5(ClientHello.random + ServerHello.random
12842 * + ServerParams);
12843 * sha_hash
12844 * SHA(ClientHello.random + ServerHello.random
12845 * + ServerParams);
12846 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012847 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012848 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012849 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012850 goto exit;
12851 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012852 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012853 ssl->handshake->randbytes, 64 ) ) != 0 )
12854 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012855 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012856 goto exit;
12857 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012858 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012859 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012860 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012861 goto exit;
12862 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012863 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012864 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012865 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012866 goto exit;
12867 }
12868
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012869 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012870 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012871 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012872 goto exit;
12873 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012874 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012875 ssl->handshake->randbytes, 64 ) ) != 0 )
12876 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012877 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012878 goto exit;
12879 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012880 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012881 data_len ) ) != 0 )
12882 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012883 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012884 goto exit;
12885 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012886 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012887 output + 16 ) ) != 0 )
12888 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012889 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012890 goto exit;
12891 }
12892
12893exit:
12894 mbedtls_md5_free( &mbedtls_md5 );
12895 mbedtls_sha1_free( &mbedtls_sha1 );
12896
12897 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012898 mbedtls_ssl_pend_fatal_alert( ssl,
12899 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012900
12901 return( ret );
12902
12903}
12904#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12905 MBEDTLS_SSL_PROTO_TLS1_1 */
12906
12907#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12908 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12909int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012910 unsigned char *hash, size_t *hashlen,
12911 unsigned char *data, size_t data_len,
12912 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012913{
12914 int ret = 0;
12915 mbedtls_md_context_t ctx;
Hanno Beckera5cedbc2019-07-17 11:21:02 +010012916 mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012917 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012918
12919 mbedtls_md_init( &ctx );
12920
12921 /*
12922 * digitally-signed struct {
12923 * opaque client_random[32];
12924 * opaque server_random[32];
12925 * ServerDHParams params;
12926 * };
12927 */
12928 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12929 {
12930 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12931 goto exit;
12932 }
12933 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12934 {
12935 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12936 goto exit;
12937 }
12938 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12939 {
12940 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12941 goto exit;
12942 }
12943 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12944 {
12945 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12946 goto exit;
12947 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012948 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012949 {
12950 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12951 goto exit;
12952 }
12953
12954exit:
12955 mbedtls_md_free( &ctx );
12956
12957 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012958 mbedtls_ssl_pend_fatal_alert( ssl,
12959 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012960
12961 return( ret );
12962}
12963#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12964 MBEDTLS_SSL_PROTO_TLS1_2 */
12965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012966#endif /* MBEDTLS_SSL_TLS_C */