blob: c92ab7f392a9aae1eca42307761a1f5b780d9508 [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
Rich Evans00ab4702015-02-06 13:43:58 +000053#include <string.h>
54
Janos Follath23bdca02016-10-07 14:47:14 +010055#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020057#endif
58
Hanno Beckeref982d52019-07-23 15:56:18 +010059#if defined(MBEDTLS_USE_TINYCRYPT)
60static int uecc_rng_wrapper( uint8_t *dest, unsigned int size )
61{
Hanno Beckerd089fad2019-07-24 09:05:05 +010062 int ret;
63 ret = mbedtls_ssl_conf_rng_func( NULL, dest, size );
64 if( ret == 0 )
65 return( (int) size );
66
67 return( 0 );
Hanno Beckeref982d52019-07-23 15:56:18 +010068}
Hanno Becker75f12d12019-07-23 16:16:15 +010069
70int mbedtls_ssl_ecdh_read_peerkey( mbedtls_ssl_context *ssl,
71 unsigned char **p, unsigned char *end )
72{
73 size_t const secp256r1_uncompressed_point_length =
74 1 /* length */ + 1 /* length */ + 2 * NUM_ECC_BYTES /* data */;
75
76 if( (size_t)( end - *p ) < secp256r1_uncompressed_point_length )
77 {
78 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Bad ECDH peer pubkey (too short)" ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010079 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010080 }
81
82 if( (*p)[0] != 2 * NUM_ECC_BYTES + 1 ||
83 (*p)[1] != 0x04 )
84 {
85 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Unexpected ECDH peer pubkey header - expected { %#02x, %#02x }, got { %#02x, %#02x }",
86 2 * NUM_ECC_BYTES + 1,
87 0x04,
88 (unsigned) (*p)[0],
89 (unsigned) (*p)[1] ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010090 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010091 }
92
Teppo Järvelin91d79382019-10-02 09:09:31 +030093 mbedtls_platform_memcpy( ssl->handshake->ecdh_peerkey, *p + 2, 2 * NUM_ECC_BYTES );
Hanno Becker75f12d12019-07-23 16:16:15 +010094
95 *p += secp256r1_uncompressed_point_length;
96 return( 0 );
97}
Hanno Beckeref982d52019-07-23 15:56:18 +010098#endif /* MBEDTLS_USE_TINYCRYPT */
99
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100100static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +0100101static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100102
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100103/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100105{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200106#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +0100107 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100108#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200109
110#if defined(MBEDTLS_SSL_PROTO_DTLS)
111 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
112 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200113 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200114#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200115#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100116 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200117#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100118}
119
Hanno Beckerb82350b2019-07-26 07:24:05 +0100120static void ssl_send_pending_fatal_alert( mbedtls_ssl_context *ssl )
121{
122 if( ssl->pending_fatal_alert_msg == MBEDTLS_SSL_ALERT_MSG_NONE )
123 return;
124
125 mbedtls_ssl_send_alert_message( ssl,
126 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
127 ssl->pending_fatal_alert_msg );
128 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
129}
130
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200131/*
132 * Start a timer.
133 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200136{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100137 if( mbedtls_ssl_get_set_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200138 return;
139
140 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
Hanno Becker0ae6b242019-06-13 16:45:36 +0100141 mbedtls_ssl_get_set_timer( ssl )( ssl->p_timer,
142 millisecs / 4,
143 millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200144}
145
146/*
147 * Return -1 is timer is expired, 0 if it isn't.
148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200150{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100151 if( mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200152 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200153
Hanno Becker0ae6b242019-06-13 16:45:36 +0100154 if( mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200155 {
156 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200157 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200158 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200159
160 return( 0 );
161}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200162
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100163static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
164 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100165static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100166
Hanno Becker02f26092019-07-03 16:13:00 +0100167#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker03e2db62019-07-12 14:40:00 +0100168static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
169 unsigned char *buf,
170 size_t len,
171 mbedtls_record *rec );
172
Hanno Becker02f26092019-07-03 16:13:00 +0100173int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
174 unsigned char *buf,
175 size_t buflen )
176{
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400177 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Hanno Becker03e2db62019-07-12 14:40:00 +0100178 mbedtls_record rec;
179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
180 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
181
182 /* We don't support record checking in TLS because
183 * (a) there doesn't seem to be a usecase for it, and
184 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
185 * and we'd need to backup the transform here.
186 */
187#if defined(MBEDTLS_SSL_PROTO_TLS)
188 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
189 {
190 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
191 goto exit;
192 }
193 MBEDTLS_SSL_TRANSPORT_ELSE
194#endif /* MBEDTLS_SSL_PROTO_TLS */
195#if defined(MBEDTLS_SSL_PROTO_DTLS)
196 {
197 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
198 if( ret != 0 )
199 {
200 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
201 goto exit;
202 }
203
204 if( ssl->transform_in != NULL )
205 {
206 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
207 if( ret != 0 )
208 {
209 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
210 goto exit;
211 }
212 }
213 }
214#endif /* MBEDTLS_SSL_PROTO_DTLS */
215
216exit:
217 /* On success, we have decrypted the buffer in-place, so make
218 * sure we don't leak any plaintext data. */
219 mbedtls_platform_zeroize( buf, buflen );
220
221 /* For the purpose of this API, treat messages with unexpected CID
222 * as well as such from future epochs as unexpected. */
223 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
224 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
225 {
226 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
227 }
228
229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
230 return( ret );
Hanno Becker02f26092019-07-03 16:13:00 +0100231}
232#endif /* MBEDTLS_SSL_RECORD_CHECKING */
233
Hanno Becker67bc7c32018-08-06 11:33:50 +0100234#define SSL_DONT_FORCE_FLUSH 0
235#define SSL_FORCE_FLUSH 1
236
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200237#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100238
Hanno Beckera5a2b082019-05-15 14:03:01 +0100239#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100240/* Top-level Connection ID API */
241
Hanno Beckere0200da2019-06-13 09:23:43 +0100242#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
243 !defined(MBEDTLS_SSL_CONF_CID_LEN) && \
244 !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100245int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
246 size_t len,
247 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100248{
249 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
250 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
251
Hanno Becker791ec6b2019-05-14 11:45:26 +0100252 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
253 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
254 {
255 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
256 }
257
258 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100259 conf->cid_len = len;
260 return( 0 );
261}
Hanno Beckere0200da2019-06-13 09:23:43 +0100262#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
263 !MBEDTLS_SSL_CONF_CID_LEN &&
264 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
265
266#if MBEDTLS_SSL_CONF_CID_LEN > MBEDTLS_SSL_CID_IN_LEN_MAX
267#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_CID_LEN"
268#endif
269#if MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE && \
270 MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_FAIL
271#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID"
272#endif
273
274#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
275 !MBEDTLS_SSL_CONF_CID_LEN &&
276 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
Hanno Beckereec2be92019-05-03 13:06:44 +0100277
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100278int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
279 int enable,
280 unsigned char const *own_cid,
281 size_t own_cid_len )
282{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200283 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100284 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
285
Hanno Becker07489862019-04-25 16:01:49 +0100286 ssl->negotiate_cid = enable;
287 if( enable == MBEDTLS_SSL_CID_DISABLED )
288 {
289 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
290 return( 0 );
291 }
292 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100293 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100294
Hanno Beckere0200da2019-06-13 09:23:43 +0100295 if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) )
Hanno Becker07489862019-04-25 16:01:49 +0100296 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100297 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
298 (unsigned) own_cid_len,
Hanno Beckere0200da2019-06-13 09:23:43 +0100299 (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) );
Hanno Becker07489862019-04-25 16:01:49 +0100300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
301 }
302
Teppo Järvelin6f4e0302019-10-04 13:53:53 +0300303 /* Not using more secure mbedtls_platform_memcpy as cid is public */
304 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100305 /* Truncation is not an issue here because
306 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
307 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100308
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100309 return( 0 );
310}
311
312int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
313 int *enabled,
314 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
315 size_t *peer_cid_len )
316{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100317 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100318
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200319 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100320 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
321 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100323 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100324
Hanno Beckercb063f52019-05-03 12:54:52 +0100325 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
326 * were used, but client and server requested the empty CID.
327 * This is indistinguishable from not using the CID extension
328 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100329 if( ssl->transform_in->in_cid_len == 0 &&
330 ssl->transform_in->out_cid_len == 0 )
331 {
332 return( 0 );
333 }
334
Hanno Becker633d6042019-05-22 16:50:35 +0100335 if( peer_cid_len != NULL )
336 {
337 *peer_cid_len = ssl->transform_in->out_cid_len;
338 if( peer_cid != NULL )
339 {
Teppo Järvelin6f4e0302019-10-04 13:53:53 +0300340 /* Not using more secure mbedtls_platform_memcpy as cid is public */
341 memcpy( peer_cid, ssl->transform_in->out_cid,
Hanno Becker633d6042019-05-22 16:50:35 +0100342 ssl->transform_in->out_cid_len );
343 }
344 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100345
346 *enabled = MBEDTLS_SSL_CID_ENABLED;
347
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100348 return( 0 );
349}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100350#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100351
Hanno Beckerd5847772018-08-28 10:09:23 +0100352/* Forward declarations for functions related to message buffering. */
353static void ssl_buffering_free( mbedtls_ssl_context *ssl );
354static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
355 uint8_t slot );
356static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
357static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
358static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
359static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +0100360static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
361 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100362static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100363
Hanno Beckera67dee22018-08-22 10:05:20 +0100364static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100365static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100366{
Hanno Becker11682cc2018-08-22 14:41:02 +0100367 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100368
369 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100370 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100371
372 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
373}
374
Hanno Becker67bc7c32018-08-06 11:33:50 +0100375static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
376{
Hanno Becker11682cc2018-08-22 14:41:02 +0100377 size_t const bytes_written = ssl->out_left;
378 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100379
380 /* Double-check that the write-index hasn't gone
381 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100382 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100383 {
384 /* Should never happen... */
385 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
386 }
387
388 return( (int) ( mtu - bytes_written ) );
389}
390
391static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
392{
393 int ret;
394 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400395 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100396
397#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
398 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
399
400 if( max_len > mfl )
401 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100402
403 /* By the standard (RFC 6066 Sect. 4), the MFL extension
404 * only limits the maximum record payload size, so in theory
405 * we would be allowed to pack multiple records of payload size
406 * MFL into a single datagram. However, this would mean that there's
407 * no way to explicitly communicate MTU restrictions to the peer.
408 *
409 * The following reduction of max_len makes sure that we never
410 * write datagrams larger than MFL + Record Expansion Overhead.
411 */
412 if( max_len <= ssl->out_left )
413 return( 0 );
414
415 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100416#endif
417
418 ret = ssl_get_remaining_space_in_datagram( ssl );
419 if( ret < 0 )
420 return( ret );
421 remaining = (size_t) ret;
422
423 ret = mbedtls_ssl_get_record_expansion( ssl );
424 if( ret < 0 )
425 return( ret );
426 expansion = (size_t) ret;
427
428 if( remaining <= expansion )
429 return( 0 );
430
431 remaining -= expansion;
432 if( remaining >= max_len )
433 remaining = max_len;
434
435 return( (int) remaining );
436}
437
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200438/*
439 * Double the retransmit timeout value, within the allowed range,
440 * returning -1 if the maximum value has already been reached.
441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200443{
444 uint32_t new_timeout;
445
Hanno Becker1f835fa2019-06-13 10:14:59 +0100446 if( ssl->handshake->retransmit_timeout >=
447 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
448 {
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200449 return( -1 );
Hanno Becker1f835fa2019-06-13 10:14:59 +0100450 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200451
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200452 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
453 * in the following way: after the initial transmission and a first
454 * retransmission, back off to a temporary estimated MTU of 508 bytes.
455 * This value is guaranteed to be deliverable (if not guaranteed to be
456 * delivered) of any compliant IPv4 (and IPv6) network, and should work
457 * on most non-IP stacks too. */
Hanno Becker1f835fa2019-06-13 10:14:59 +0100458 if( ssl->handshake->retransmit_timeout !=
459 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400460 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200461 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
463 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200464
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200465 new_timeout = 2 * ssl->handshake->retransmit_timeout;
466
467 /* Avoid arithmetic overflow and range overflow */
468 if( new_timeout < ssl->handshake->retransmit_timeout ||
Hanno Becker1f835fa2019-06-13 10:14:59 +0100469 new_timeout > mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200470 {
Hanno Becker1f835fa2019-06-13 10:14:59 +0100471 new_timeout = mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200472 }
473
474 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200476 ssl->handshake->retransmit_timeout ) );
477
478 return( 0 );
479}
480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200482{
Hanno Becker1f835fa2019-06-13 10:14:59 +0100483 ssl->handshake->retransmit_timeout = mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200485 ssl->handshake->retransmit_timeout ) );
486}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200490/*
491 * Convert max_fragment_length codes to length.
492 * RFC 6066 says:
493 * enum{
494 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
495 * } MaxFragmentLength;
496 * and we add 0 -> extension unused
497 */
Angus Grattond8213d02016-05-25 20:56:48 +1000498static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200499{
Angus Grattond8213d02016-05-25 20:56:48 +1000500 switch( mfl )
501 {
502 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300503 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000504 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
505 return 512;
506 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
507 return 1024;
508 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
509 return 2048;
510 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
511 return 4096;
512 default:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300513 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000514 }
515}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200517
Hanno Becker58fccf22019-02-06 14:30:46 +0000518int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
519 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200520{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_ssl_session_free( dst );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300522 mbedtls_platform_memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000525
526#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200527 if( src->peer_cert != NULL )
528 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200529 int ret;
530
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200531 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200532 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200533 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200538 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200541 dst->peer_cert = NULL;
542 return( ret );
543 }
544 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100545#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000546 if( src->peer_cert_digest != NULL )
547 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000548 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000549 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000550 if( dst->peer_cert_digest == NULL )
551 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
552
Teppo Järvelin91d79382019-10-02 09:09:31 +0300553 mbedtls_platform_memcpy( dst->peer_cert_digest, src->peer_cert_digest,
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000554 src->peer_cert_digest_len );
555 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000556 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000557 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100558#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200561
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200562#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200563 if( src->ticket != NULL )
564 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200565 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200566 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200567 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200568
Teppo Järvelin91d79382019-10-02 09:09:31 +0300569 mbedtls_platform_memcpy( dst->ticket, src->ticket, src->ticket_len );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200570 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200571#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200572
573 return( 0 );
574}
575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
577int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200578 const unsigned char *key_enc, const unsigned char *key_dec,
579 size_t keylen,
580 const unsigned char *iv_enc, const unsigned char *iv_dec,
581 size_t ivlen,
582 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200583 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
585int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
586int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
587int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
588int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
589#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000590
Paul Bakker5121ce52009-01-03 21:22:43 +0000591/*
592 * Key material generation
593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100595MBEDTLS_NO_INLINE static int ssl3_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200596 const char *label,
597 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000598 unsigned char *dstbuf, size_t dlen )
599{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100600 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000601 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200602 mbedtls_md5_context md5;
603 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000604 unsigned char padding[16];
605 unsigned char sha1sum[20];
606 ((void)label);
607
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200608 mbedtls_md5_init( &md5 );
609 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200610
Paul Bakker5f70b252012-09-13 14:23:06 +0000611 /*
612 * SSLv3:
613 * block =
614 * MD5( secret + SHA1( 'A' + secret + random ) ) +
615 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
616 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
617 * ...
618 */
619 for( i = 0; i < dlen / 16; i++ )
620 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200621 mbedtls_platform_memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000622
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100623 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100624 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100625 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100626 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100627 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100628 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100629 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100630 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100631 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100632 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000633
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100634 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100635 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100636 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100637 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100638 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100639 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100640 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100641 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000642 }
643
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100644exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200645 mbedtls_md5_free( &md5 );
646 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000647
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500648 mbedtls_platform_zeroize( padding, sizeof( padding ) );
649 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000650
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100651 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000652}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100656MBEDTLS_NO_INLINE static int tls1_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200657 const char *label,
658 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000659 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000660{
Paul Bakker23986e52011-04-24 08:57:21 +0000661 size_t nb, hs;
662 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200663 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000664 unsigned char tmp[128];
665 unsigned char h_i[20];
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100666 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100668 int ret;
669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
675 hs = ( slen + 1 ) / 2;
676 S1 = secret;
677 S2 = secret + slen - hs;
678
679 nb = strlen( label );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300680 mbedtls_platform_memcpy( tmp + 20, label, nb );
681 mbedtls_platform_memcpy( tmp + 20 + nb, random, rlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000682 nb += rlen;
683
684 /*
685 * First compute P_md5(secret,label+random)[0..dlen]
686 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100687 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) ==
688 MBEDTLS_MD_INVALID_HANDLE )
689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100691 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100694 return( ret );
695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
697 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
698 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000699
700 for( i = 0; i < dlen; i += 16 )
701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_md_hmac_reset ( &md_ctx );
703 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
704 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_md_hmac_reset ( &md_ctx );
707 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
708 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000709
710 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
711
712 for( j = 0; j < k; j++ )
713 dstbuf[i + j] = h_i[j];
714 }
715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100717
Paul Bakker5121ce52009-01-03 21:22:43 +0000718 /*
719 * XOR out with P_sha1(secret,label+random)[0..dlen]
720 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100721 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) ==
722 MBEDTLS_MD_INVALID_HANDLE )
723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100725 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100728 return( ret );
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
731 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
732 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000733
734 for( i = 0; i < dlen; i += 20 )
735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_md_hmac_reset ( &md_ctx );
737 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
738 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 mbedtls_md_hmac_reset ( &md_ctx );
741 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
742 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000743
744 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
745
746 for( j = 0; j < k; j++ )
747 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
748 }
749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100751
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500752 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
753 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000754
755 return( 0 );
756}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerf6cc7422019-08-16 14:34:52 +0100760#if !( defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_SHA512_C) )
761MBEDTLS_ALWAYS_INLINE static inline
762#else
763static
764#endif
765int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100766 const unsigned char *secret, size_t slen,
767 const char *label,
768 const unsigned char *random, size_t rlen,
769 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000770{
771 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100772 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000773 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100775 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100777 int ret;
778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000780
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100781 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) ==
782 MBEDTLS_MD_INVALID_HANDLE )
783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100785 }
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100788
789 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000791
792 nb = strlen( label );
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200793 (void)mbedtls_platform_memcpy( tmp + md_len, label, nb );
794 (void)mbedtls_platform_memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000795 nb += rlen;
796
797 /*
798 * Compute P_<hash>(secret, label + random)[0..dlen]
799 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100801 return( ret );
802
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200803 if ( ( ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ) ) != 0 )
804 return( ret );
805 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ) ) != 0 )
806 return( ret );
807 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, tmp ) ) != 0 )
808 return( ret );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100809
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100810 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000811 {
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200812 if ( ( ret = mbedtls_md_hmac_reset ( &md_ctx ) ) != 0 )
813 return( ret );
814 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ) ) != 0 )
815 return( ret );
816 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, h_i ) ) != 0 )
817 return( ret );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100818
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200819 if ( ( ret = mbedtls_md_hmac_reset ( &md_ctx ) ) != 0 )
820 return( ret );
821 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ) ) != 0 )
822 return( ret );
823 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, tmp ) ) != 0 )
824 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000825
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100826 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000827
828 for( j = 0; j < k; j++ )
829 dstbuf[i + j] = h_i[j];
830 }
831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100833
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200834 (void)mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
835 (void)mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000836
837 return( 0 );
838}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100841MBEDTLS_NO_INLINE static int tls_prf_sha256(
842 const unsigned char *secret, size_t slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100843 const char *label,
844 const unsigned char *random, size_t rlen,
845 unsigned char *dstbuf, size_t dlen )
846{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100848 label, random, rlen, dstbuf, dlen ) );
849}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100853MBEDTLS_NO_INLINE static int tls_prf_sha384(
854 const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200855 const char *label,
856 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000857 unsigned char *dstbuf, size_t dlen )
858{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100860 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000861}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#endif /* MBEDTLS_SHA512_C */
863#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000864
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100865/*
866 * Call the appropriate PRF function
867 */
Hanno Becker2793f742019-08-16 14:28:43 +0100868MBEDTLS_ALWAYS_INLINE static inline int ssl_prf( int minor_ver,
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100869 mbedtls_md_type_t hash,
870 const unsigned char *secret, size_t slen,
871 const char *label,
872 const unsigned char *random, size_t rlen,
873 unsigned char *dstbuf, size_t dlen )
874{
875#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
876 (void) hash;
877#endif
878
879#if defined(MBEDTLS_SSL_PROTO_SSL3)
880 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
881 return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
882 else
883#endif
884#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +0100885 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100886 return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
887 else
888#endif
889#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
890#if defined(MBEDTLS_SHA512_C)
891 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
892 hash == MBEDTLS_MD_SHA384 )
893 {
894 return( tls_prf_sha384( secret, slen, label, random, rlen,
895 dstbuf, dlen ) );
896 }
897 else
898#endif
899#if defined(MBEDTLS_SHA256_C)
900 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
901 {
902 return( tls_prf_sha256( secret, slen, label, random, rlen,
903 dstbuf, dlen ) );
904 }
905#endif
906#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
907
908 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
909}
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200910
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100911#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100912MBEDTLS_NO_INLINE static void ssl_calc_finished_ssl(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100913 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
914{
915 const char *sender;
916 mbedtls_md5_context md5;
917 mbedtls_sha1_context sha1;
918
919 unsigned char padbuf[48];
920 unsigned char md5sum[16];
921 unsigned char sha1sum[20];
922
923 mbedtls_ssl_session *session = ssl->session_negotiate;
924 if( !session )
925 session = ssl->session;
926
927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
928
929 mbedtls_md5_init( &md5 );
930 mbedtls_sha1_init( &sha1 );
931
932 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
933 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
934
935 /*
936 * SSLv3:
937 * hash =
938 * MD5( master + pad2 +
939 * MD5( handshake + sender + master + pad1 ) )
940 * + SHA1( master + pad2 +
941 * SHA1( handshake + sender + master + pad1 ) )
942 */
943
944#if !defined(MBEDTLS_MD5_ALT)
945 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
946 md5.state, sizeof( md5.state ) );
947#endif
948
949#if !defined(MBEDTLS_SHA1_ALT)
950 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
951 sha1.state, sizeof( sha1.state ) );
952#endif
953
954 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
955 : "SRVR";
956
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200957 mbedtls_platform_memset( padbuf, 0x36, 48 );
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100958
959 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
960 mbedtls_md5_update_ret( &md5, session->master, 48 );
961 mbedtls_md5_update_ret( &md5, padbuf, 48 );
962 mbedtls_md5_finish_ret( &md5, md5sum );
963
964 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
965 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
966 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
967 mbedtls_sha1_finish_ret( &sha1, sha1sum );
968
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200969 mbedtls_platform_memset( padbuf, 0x5C, 48 );
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100970
971 mbedtls_md5_starts_ret( &md5 );
972 mbedtls_md5_update_ret( &md5, session->master, 48 );
973 mbedtls_md5_update_ret( &md5, padbuf, 48 );
974 mbedtls_md5_update_ret( &md5, md5sum, 16 );
975 mbedtls_md5_finish_ret( &md5, buf );
976
977 mbedtls_sha1_starts_ret( &sha1 );
978 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
979 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
980 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
981 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
982
983 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
984
985 mbedtls_md5_free( &md5 );
986 mbedtls_sha1_free( &sha1 );
987
988 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
989 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
990 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
991
992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
993}
994#endif /* MBEDTLS_SSL_PROTO_SSL3 */
995
996#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100997MBEDTLS_NO_INLINE static void ssl_calc_finished_tls(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100998 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
999{
1000 int len = 12;
1001 const char *sender;
1002 mbedtls_md5_context md5;
1003 mbedtls_sha1_context sha1;
1004 unsigned char padbuf[36];
1005
1006 mbedtls_ssl_session *session = ssl->session_negotiate;
1007 if( !session )
1008 session = ssl->session;
1009
1010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
1011
1012 mbedtls_md5_init( &md5 );
1013 mbedtls_sha1_init( &sha1 );
1014
1015 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1016 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1017
1018 /*
1019 * TLSv1:
1020 * hash = PRF( master, finished_label,
1021 * MD5( handshake ) + SHA1( handshake ) )[0..11]
1022 */
1023
1024#if !defined(MBEDTLS_MD5_ALT)
1025 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1026 md5.state, sizeof( md5.state ) );
1027#endif
1028
1029#if !defined(MBEDTLS_SHA1_ALT)
1030 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1031 sha1.state, sizeof( sha1.state ) );
1032#endif
1033
1034 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1035 ? "client finished"
1036 : "server finished";
1037
1038 mbedtls_md5_finish_ret( &md5, padbuf );
1039 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
1040
1041 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1042 mbedtls_ssl_suite_get_mac(
1043 mbedtls_ssl_ciphersuite_from_id(
1044 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1045 session->master, 48, sender,
1046 padbuf, 36, buf, len );
1047
1048 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1049
1050 mbedtls_md5_free( &md5 );
1051 mbedtls_sha1_free( &sha1 );
1052
1053 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1054
1055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1056}
1057#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1058
1059#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1060#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001061MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha256(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001062 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1063{
1064 int len = 12;
1065 const char *sender;
1066 mbedtls_sha256_context sha256;
1067 unsigned char padbuf[32];
1068
1069 mbedtls_ssl_session *session = ssl->session_negotiate;
1070 if( !session )
1071 session = ssl->session;
1072
1073 mbedtls_sha256_init( &sha256 );
1074
1075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
1076
1077 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1078
1079 /*
1080 * TLSv1.2:
1081 * hash = PRF( master, finished_label,
1082 * Hash( handshake ) )[0.11]
1083 */
1084
1085#if !defined(MBEDTLS_SHA256_ALT)
1086 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
1087 sha256.state, sizeof( sha256.state ) );
1088#endif
1089
1090 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1091 ? "client finished"
1092 : "server finished";
1093
1094 mbedtls_sha256_finish_ret( &sha256, padbuf );
1095
1096 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1097 mbedtls_ssl_suite_get_mac(
1098 mbedtls_ssl_ciphersuite_from_id(
1099 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1100 session->master, 48, sender,
1101 padbuf, 32, buf, len );
1102
1103 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1104
1105 mbedtls_sha256_free( &sha256 );
1106
1107 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1108
1109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1110}
1111#endif /* MBEDTLS_SHA256_C */
1112
1113#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001114MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha384(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001115 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1116{
1117 int len = 12;
1118 const char *sender;
1119 mbedtls_sha512_context sha512;
1120 unsigned char padbuf[48];
1121
1122 mbedtls_ssl_session *session = ssl->session_negotiate;
1123 if( !session )
1124 session = ssl->session;
1125
1126 mbedtls_sha512_init( &sha512 );
1127
1128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
1129
1130 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1131
1132 /*
1133 * TLSv1.2:
1134 * hash = PRF( master, finished_label,
1135 * Hash( handshake ) )[0.11]
1136 */
1137
1138#if !defined(MBEDTLS_SHA512_ALT)
1139 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
1140 sha512.state, sizeof( sha512.state ) );
1141#endif
1142
1143 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1144 ? "client finished"
1145 : "server finished";
1146
1147 mbedtls_sha512_finish_ret( &sha512, padbuf );
1148
1149 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1150 mbedtls_ssl_suite_get_mac(
1151 mbedtls_ssl_ciphersuite_from_id(
1152 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1153 session->master, 48, sender,
1154 padbuf, 48, buf, len );
1155
1156 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1157
1158 mbedtls_sha512_free( &sha512 );
1159
1160 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1161
1162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1163}
1164#endif /* MBEDTLS_SHA512_C */
1165#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1166
Hanno Becker2793f742019-08-16 14:28:43 +01001167MBEDTLS_ALWAYS_INLINE static inline int ssl_calc_finished(
1168 int minor_ver,
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001169 mbedtls_md_type_t hash,
1170 mbedtls_ssl_context *ssl,
1171 unsigned char *buf,
1172 int from )
1173{
1174#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1175 (void) hash;
1176#endif
1177
1178#if defined(MBEDTLS_SSL_PROTO_SSL3)
1179 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1180 ssl_calc_finished_ssl( ssl, buf, from );
1181 else
1182#endif
1183#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001184 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001185 ssl_calc_finished_tls( ssl, buf, from );
1186 else
1187#endif
1188#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1189#if defined(MBEDTLS_SHA512_C)
1190 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1191 hash == MBEDTLS_MD_SHA384 )
1192 {
1193 ssl_calc_finished_tls_sha384( ssl, buf, from );
1194 }
1195 else
1196#endif
1197#if defined(MBEDTLS_SHA256_C)
1198 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1199 ssl_calc_finished_tls_sha256( ssl, buf, from );
1200 else
1201#endif
1202#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1203 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1204
1205 return( 0 );
1206}
1207
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001208/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001209 * Populate a transform structure with session keys and all the other
1210 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001211 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001212 * Parameters:
1213 * - [in/out]: transform: structure to populate
1214 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001215 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001216 * - [in] ciphersuite
1217 * - [in] master
1218 * - [in] encrypt_then_mac
1219 * - [in] trunc_hmac
1220 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001221 * - [in] tls_prf: pointer to PRF to use for key derivation
1222 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001223 * - [in] minor_ver: SSL/TLS minor version
1224 * - [in] endpoint: client or server
1225 * - [in] ssl: optionally used for:
1226 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1227 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1228 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001229 */
Hanno Becker298a4702019-08-16 10:21:32 +01001230/* Force compilers to inline this function if it's used only
1231 * from one place, because at least ARMC5 doesn't do that
1232 * automatically. */
1233#if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1234MBEDTLS_ALWAYS_INLINE static inline
1235#else
1236static
1237#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1238int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001239 int ciphersuite,
1240 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001241#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001242#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1243 int encrypt_then_mac,
1244#endif
1245#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1246 int trunc_hmac,
1247#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001248#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001249#if defined(MBEDTLS_ZLIB_SUPPORT)
1250 int compression,
1251#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001252 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001253 int minor_ver,
1254 unsigned endpoint,
Manuel Pégourié-Gonnard13bebd02020-03-13 11:28:19 +01001255#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1256 const
1257#endif
1258 mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001259{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001260 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 unsigned char keyblk[256];
1262 unsigned char *key1;
1263 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001264 unsigned char *mac_enc;
1265 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001266 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001267 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001268 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001269 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 const mbedtls_cipher_info_t *cipher_info;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001271 mbedtls_md_handle_t md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001272
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001273#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1274 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1275 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001276 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001277 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001278#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001279
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001280 /*
1281 * Some data just needs copying into the structure
1282 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001283#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1284 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001285 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001286#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001287
1288#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001289 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001290#else
1291 ((void) minor_ver);
1292#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001294#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Teppo Järvelin91d79382019-10-02 09:09:31 +03001295 mbedtls_platform_memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001296#endif
1297
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001298 /*
1299 * Get various info structures
1300 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001301 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001302 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001303 {
1304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001305 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001306 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1307 }
1308
Hanno Becker473f98f2019-06-26 10:27:32 +01001309 cipher_info = mbedtls_cipher_info_from_type(
1310 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001311 if( cipher_info == NULL )
1312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001314 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001316 }
1317
Hanno Becker473f98f2019-06-26 10:27:32 +01001318 md_info = mbedtls_md_info_from_type(
1319 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001320 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Paul Bakker68884e32013-01-07 18:20:04 +01001321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001323 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001325 }
1326
Hanno Beckera5a2b082019-05-15 14:03:01 +01001327#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001328 /* Copy own and peer's CID if the use of the CID
1329 * extension has been negotiated. */
1330 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1331 {
1332 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001333
Hanno Becker4932f9f2019-05-03 15:23:51 +01001334 transform->in_cid_len = ssl->own_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001335 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1336 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001337 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001338 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001339
1340 transform->out_cid_len = ssl->handshake->peer_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001341 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1342 memcpy( transform->out_cid, ssl->handshake->peer_cid,
Hanno Beckere582d122019-05-15 10:21:55 +01001343 ssl->handshake->peer_cid_len );
1344 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1345 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001346 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001347#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001348
Paul Bakker5121ce52009-01-03 21:22:43 +00001349 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001350 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001351 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001352 ret = ssl_prf( minor_ver,
1353 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1354 master, 48, "key expansion", randbytes, 64,
1355 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001356 if( ret != 0 )
1357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001359 return( ret );
1360 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001363 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001364 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001365 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001367
Paul Bakker5121ce52009-01-03 21:22:43 +00001368 /*
1369 * Determine the appropriate key, IV and MAC length.
1370 */
Paul Bakker68884e32013-01-07 18:20:04 +01001371
Hanno Beckere7f2df02017-12-27 08:17:40 +00001372 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001373
Hanno Beckerf1229442018-01-03 15:32:31 +00001374#if defined(MBEDTLS_GCM_C) || \
1375 defined(MBEDTLS_CCM_C) || \
1376 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001378 cipher_info->mode == MBEDTLS_MODE_CCM ||
1379 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001380 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001381 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001382 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001383 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1384 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001385
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001386 /* All modes haves 96-bit IVs;
1387 * GCM and CCM has 4 implicit and 8 explicit bytes
1388 * ChachaPoly has all 12 bytes implicit
1389 */
Paul Bakker68884e32013-01-07 18:20:04 +01001390 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001391 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1392 transform->fixed_ivlen = 12;
1393 else
1394 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001395 }
1396 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001397#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1398#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1399 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1400 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001401 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001402 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1404 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001407 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001408 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001409
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001410 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001411 mac_key_len = mbedtls_md_get_size( md_info );
1412 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001415 /*
1416 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1417 * (rfc 6066 page 13 or rfc 2104 section 4),
1418 * so we only need to adjust the length here.
1419 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001420 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001423
1424#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1425 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001426 * HMAC implementation which also truncates the key
1427 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001428 mac_key_len = transform->maclen;
1429#endif
1430 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001432
1433 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001434 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001435 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001436 else
1437#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1438 {
1439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1440 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1441 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001442
Hanno Beckera9d5c452019-07-25 16:47:12 +01001443 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001444 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001445 (unsigned) transform->ivlen,
1446 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001447
1448 /*
1449 * Finally setup the cipher contexts, IVs and MAC secrets.
1450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001452 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001453 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001454 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001455 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001456
Paul Bakker68884e32013-01-07 18:20:04 +01001457 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001458 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001459
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001460 /*
1461 * This is not used in TLS v1.1.
1462 */
Paul Bakker48916f92012-09-16 19:57:18 +00001463 iv_copy_len = ( transform->fixed_ivlen ) ?
1464 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001465 mbedtls_platform_memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1466 mbedtls_platform_memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001467 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001468 }
1469 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#endif /* MBEDTLS_SSL_CLI_C */
1471#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001472 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001473 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001474 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001475 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001476
Hanno Becker81c7b182017-11-09 18:39:33 +00001477 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001478 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001479
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001480 /*
1481 * This is not used in TLS v1.1.
1482 */
Paul Bakker48916f92012-09-16 19:57:18 +00001483 iv_copy_len = ( transform->fixed_ivlen ) ?
1484 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001485 mbedtls_platform_memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1486 mbedtls_platform_memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001487 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001488 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001489 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1493 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001494 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001495
Hanno Becker92231322018-01-03 15:32:51 +00001496#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001498 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001499 {
Hanno Becker92231322018-01-03 15:32:51 +00001500 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1503 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001504 }
1505
Teppo Järvelin91d79382019-10-02 09:09:31 +03001506 mbedtls_platform_memcpy( transform->mac_enc, mac_enc, mac_key_len );
1507 mbedtls_platform_memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001508 }
1509 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1511#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1512 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001513 if( mbedtls_ssl_ver_geq( minor_ver, MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakker68884e32013-01-07 18:20:04 +01001514 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001515 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1516 For AEAD-based ciphersuites, there is nothing to do here. */
1517 if( mac_key_len != 0 )
1518 {
1519 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1520 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1521 }
Paul Bakker68884e32013-01-07 18:20:04 +01001522 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001523 else
1524#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1527 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001528 }
Hanno Becker92231322018-01-03 15:32:51 +00001529#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1532 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001535
Hanno Beckere7f2df02017-12-27 08:17:40 +00001536 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001537 transform->iv_enc, transform->iv_dec,
1538 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001539 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001540 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1543 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001544 }
1545 }
Hanno Becker92231322018-01-03 15:32:51 +00001546#else
1547 ((void) mac_dec);
1548 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001550
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001551#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1552 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001553 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001554 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001555 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001556 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001557 iv_copy_len );
1558 }
1559#endif
1560
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001561 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001562 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001563 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001564 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001565 return( ret );
1566 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001567
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001568 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001569 cipher_info ) ) != 0 )
1570 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001571 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001572 return( ret );
1573 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001576 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001580 return( ret );
1581 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001584 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001588 return( ret );
1589 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591#if defined(MBEDTLS_CIPHER_MODE_CBC)
1592 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1595 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001598 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001599 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1602 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001605 return( ret );
1606 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001607 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001609
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001610 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001611
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001612 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001614 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001617
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001618 mbedtls_platform_memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1619 mbedtls_platform_memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001620
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001621 if( deflateInit( &transform->ctx_deflate,
1622 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001623 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1626 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001627 }
1628 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001630
Paul Bakker5121ce52009-01-03 21:22:43 +00001631 return( 0 );
1632}
1633
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001634#if defined(MBEDTLS_SSL_PROTO_SSL3)
1635static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1636 unsigned char hash[36],
1637 size_t *hlen )
1638{
1639 mbedtls_md5_context md5;
1640 mbedtls_sha1_context sha1;
1641 unsigned char pad_1[48];
1642 unsigned char pad_2[48];
1643
1644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1645
1646 mbedtls_md5_init( &md5 );
1647 mbedtls_sha1_init( &sha1 );
1648
1649 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1650 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1651
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001652 mbedtls_platform_memset( pad_1, 0x36, 48 );
1653 mbedtls_platform_memset( pad_2, 0x5C, 48 );
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001654
1655 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1656 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1657 mbedtls_md5_finish_ret( &md5, hash );
1658
1659 mbedtls_md5_starts_ret( &md5 );
1660 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1661 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1662 mbedtls_md5_update_ret( &md5, hash, 16 );
1663 mbedtls_md5_finish_ret( &md5, hash );
1664
1665 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1666 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1667 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1668
1669 mbedtls_sha1_starts_ret( &sha1 );
1670 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1671 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1672 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1673 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1674
1675 *hlen = 36;
1676
1677 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1679
1680 mbedtls_md5_free( &md5 );
1681 mbedtls_sha1_free( &sha1 );
1682
1683 return;
1684}
1685#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1686
1687#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1688static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1689 unsigned char hash[36],
1690 size_t *hlen )
1691{
1692 mbedtls_md5_context md5;
1693 mbedtls_sha1_context sha1;
1694
1695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1696
1697 mbedtls_md5_init( &md5 );
1698 mbedtls_sha1_init( &sha1 );
1699
1700 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1701 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1702
1703 mbedtls_md5_finish_ret( &md5, hash );
1704 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1705
1706 *hlen = 36;
1707
1708 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1710
1711 mbedtls_md5_free( &md5 );
1712 mbedtls_sha1_free( &sha1 );
1713
1714 return;
1715}
1716#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1717
1718#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1719#if defined(MBEDTLS_SHA256_C)
1720static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1721 unsigned char hash[32],
1722 size_t *hlen )
1723{
1724 mbedtls_sha256_context sha256;
1725
1726 mbedtls_sha256_init( &sha256 );
1727
1728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1729
1730 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1731 mbedtls_sha256_finish_ret( &sha256, hash );
1732
1733 *hlen = 32;
1734
1735 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1737
1738 mbedtls_sha256_free( &sha256 );
1739
1740 return;
1741}
1742#endif /* MBEDTLS_SHA256_C */
1743
1744#if defined(MBEDTLS_SHA512_C)
1745static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1746 unsigned char hash[48],
1747 size_t *hlen )
1748{
1749 mbedtls_sha512_context sha512;
1750
1751 mbedtls_sha512_init( &sha512 );
1752
1753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1754
1755 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1756 mbedtls_sha512_finish_ret( &sha512, hash );
1757
1758 *hlen = 48;
1759
1760 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1762
1763 mbedtls_sha512_free( &sha512 );
1764
1765 return;
1766}
1767#endif /* MBEDTLS_SHA512_C */
1768#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1769
Hanno Becker2f41b242019-08-15 17:29:43 +01001770int mbedtls_ssl_calc_verify( int minor_ver,
1771 mbedtls_md_type_t hash,
1772 mbedtls_ssl_context const *ssl,
1773 unsigned char *dst,
1774 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001775{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001776#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1777 (void) hash;
1778#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001779
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001780#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001781 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001782 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001783 else
1784#endif
1785#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001786 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker2f41b242019-08-15 17:29:43 +01001787 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001788 else
1789#endif
1790#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1791#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001792 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1793 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001794 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001795 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001796 }
1797 else
1798#endif
1799#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001800 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001801 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001802 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001803 }
1804 else
1805#endif
1806#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1807 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001808 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1809 }
1810
1811 return( 0 );
1812}
1813
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001814/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001815 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001816 *
1817 * Parameters:
1818 * [in/out] handshake
1819 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1820 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001821 * [out] master
1822 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001823 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001824static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001825 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001826 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001827{
1828 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001829
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001830/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1831/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1832/* (void) ssl; */
1833/* #endif */
1834
1835 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1836 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001837
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001838#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02001839 if( handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001840 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001841 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1842 return( 0 );
1843 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001844#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001845
1846 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1847 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001848
1849#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001850 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1851 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001852 {
1853 unsigned char session_hash[48];
1854 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001855
Hanno Becker2f41b242019-08-15 17:29:43 +01001856 mbedtls_ssl_calc_verify(
1857 mbedtls_ssl_get_minor_ver( ssl ),
1858 mbedtls_ssl_suite_get_mac( ciphersuite ),
1859 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001860
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001861 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1862 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001863
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001864 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1865 mbedtls_ssl_suite_get_mac( ciphersuite ),
1866 handshake->premaster, handshake->pmslen,
1867 "extended master secret",
1868 session_hash, hash_len,
1869 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001870 }
1871 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001872#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001873 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001874 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1875 mbedtls_ssl_suite_get_mac( ciphersuite ),
1876 handshake->premaster, handshake->pmslen,
1877 "master secret",
1878 handshake->randbytes, 64,
1879 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001880 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001881 if( ret != 0 )
1882 {
1883 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1884 return( ret );
1885 }
1886
1887 mbedtls_platform_zeroize( handshake->premaster,
1888 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001889
1890 return( 0 );
1891}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001892
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001893int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1894{
Andrzej Kurekfd56f402020-05-25 11:52:05 -04001895 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001896
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001898 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_UNSET;
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001899 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001900 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001901 ssl->session_negotiate->master,
1902 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001903 if( ret != 0 )
1904 {
1905 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1906 return( ret );
1907 }
1908
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001909 /* Swap the client and server random values:
1910 * - MS derivation wanted client+server (RFC 5246 8.1)
1911 * - key derivation wants server+client (RFC 5246 6.3) */
1912 {
1913 unsigned char tmp[64];
Teppo Järvelin91d79382019-10-02 09:09:31 +03001914 mbedtls_platform_memcpy( tmp, ssl->handshake->randbytes, 64 );
1915 mbedtls_platform_memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1916 mbedtls_platform_memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001917 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1918 }
1919
1920 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001921 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001922 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1923 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001924#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001925#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001926 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001927#endif
1928#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001929 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001930#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001931#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001932#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001933 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001934#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001935 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001936 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001937 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1938 ssl );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001939 if( ret == 0 )
1940 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02001941 mbedtls_platform_random_delay();
Jarno Lamsa4031a452019-12-19 08:11:12 +02001942 if( ret == 0 )
1943 {
1944 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_SET;
1945 }
1946 else
1947 {
1948 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
1949 }
1950 }
1951 else
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001952 {
1953 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1954 return( ret );
1955 }
1956
1957 /* We no longer need Server/ClientHello.random values */
1958 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1959 sizeof( ssl->handshake->randbytes ) );
1960
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001961 /* Allocate compression buffer */
1962#if defined(MBEDTLS_ZLIB_SUPPORT)
jiblime92af9a92019-12-18 21:40:01 -08001963 if( ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001964 ssl->compress_buf == NULL )
1965 {
1966 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1967 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1968 if( ssl->compress_buf == NULL )
1969 {
1970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001971 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001972 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1973 }
1974 }
1975#endif
1976
Paul Bakker5121ce52009-01-03 21:22:43 +00001977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1978
1979 return( 0 );
1980}
1981
Hanno Becker09d23642019-07-22 17:18:18 +01001982int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1983{
Andrzej Kurekfd56f402020-05-25 11:52:05 -04001984 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Hanno Becker09d23642019-07-22 17:18:18 +01001985
1986 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1987 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Beckera3c2c172019-07-23 16:51:57 +01001988#if defined(MBEDTLS_USE_TINYCRYPT)
1989 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001990 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Hanno Beckera3c2c172019-07-23 16:51:57 +01001991 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001992 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1993 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1994 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1995 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1996 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001997 {
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01001998 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1999 ssl->handshake->ecdh_privkey,
2000 ssl->handshake->premaster );
2001 if( ret == UECC_FAULT_DETECTED )
2002 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2003 if( ret != UECC_SUCCESS )
Hanno Beckera3c2c172019-07-23 16:51:57 +01002004 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002005 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Hanno Beckera3c2c172019-07-23 16:51:57 +01002006
2007 ssl->handshake->pmslen = NUM_ECC_BYTES;
2008 }
2009 else
2010#endif
Hanno Becker09d23642019-07-22 17:18:18 +01002011#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2012 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2013 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
2014 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002015 ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002016 ssl->handshake->premaster,
2017 MBEDTLS_PREMASTER_SIZE,
2018 &ssl->handshake->pmslen,
2019 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002020 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2021 if( ret == 0 )
2022 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002023 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002024 if( ret == 0 )
2025 {
2026 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2027 }
2028 else
2029 {
2030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2031 return( ret );
2032 }
2033 }
2034 else
Hanno Becker09d23642019-07-22 17:18:18 +01002035 {
2036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2037 return( ret );
2038 }
2039
2040 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2041 }
2042 else
2043#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01002044#if defined(MBEDTLS_ECDH_C) && \
2045 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2046 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2047 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2048 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002049 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2050 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2051 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2052 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2053 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2054 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2055 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2056 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2057 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002058 ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002059 &ssl->handshake->pmslen,
2060 ssl->handshake->premaster,
2061 MBEDTLS_MPI_MAX_SIZE,
2062 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002063 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2064 if( ret == 0 )
2065 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002066 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002067 if( ret == 0 )
2068 {
2069 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2070 }
2071 else
2072 {
2073 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002074 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002075 }
2076 }
2077 else
Hanno Becker09d23642019-07-22 17:18:18 +01002078 {
2079 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2080 return( ret );
2081 }
2082
2083 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2084 }
2085 else
2086#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2087 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2088 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2089 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2090#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2091 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2092 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002093 ret = mbedtls_ssl_psk_derive_premaster( ssl,
2094 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) );
2095 if( ret == 0 )
2096 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002097 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002098 if( ret == 0 )
2099 {
2100 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2101 }
2102 else
2103 {
2104 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002105 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002106 }
2107 }
2108 else
Hanno Becker09d23642019-07-22 17:18:18 +01002109 {
2110 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2111 return( ret );
2112 }
2113 }
2114 else
2115#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2116#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2117 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2118 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2119 {
2120 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2121 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2122 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002123 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002124 if( ret == 0 )
2125 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002126 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002127 if( ret == 0 )
2128 {
2129 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2130 }
2131 else
2132 {
2133 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002134 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002135 }
2136 }
2137 else
Hanno Becker09d23642019-07-22 17:18:18 +01002138 {
2139 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2140 return( ret );
2141 }
2142 }
2143 else
2144#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2145#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2146 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2147 == MBEDTLS_KEY_EXCHANGE_RSA )
2148 {
2149 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002150 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002151 * ssl_rsa_generate_partial_pms(). Only the
2152 * PMS length needs to be set. */
2153 ssl->handshake->pmslen = 48;
2154 }
2155 else
2156#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2157 {
2158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2159 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2160 }
2161
2162 return( 0 );
2163}
2164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2166int 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 +02002167{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002168 unsigned char *p = ssl->handshake->premaster;
2169 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002170 const unsigned char *psk = ssl->conf->psk;
2171 size_t psk_len = ssl->conf->psk_len;
2172
2173 /* If the psk callback was called, use its result */
2174 if( ssl->handshake->psk != NULL )
2175 {
2176 psk = ssl->handshake->psk;
2177 psk_len = ssl->handshake->psk_len;
2178 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002179
2180 /*
2181 * PMS = struct {
2182 * opaque other_secret<0..2^16-1>;
2183 * opaque psk<0..2^16-1>;
2184 * };
2185 * with "other_secret" depending on the particular key exchange
2186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2188 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002189 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002190 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002192
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002193 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002194
2195 if( end < p || (size_t)( end - p ) < psk_len )
2196 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2197
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002198 mbedtls_platform_memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002199 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002200 }
2201 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2203#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2204 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002205 {
2206 /*
2207 * other_secret already set by the ClientKeyExchange message,
2208 * and is 48 bytes long
2209 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002210 if( end - p < 2 )
2211 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2212
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002213 *p++ = 0;
2214 *p++ = 48;
2215 p += 48;
2216 }
2217 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2219#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2220 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002221 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002222 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002223 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002224
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002225 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002227 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002228 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002229 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002232 return( ret );
2233 }
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002234 p = mbedtls_platform_put_uint16_be( p, len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002235 p += len;
2236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002238 }
2239 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2241#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2242 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002243 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002244 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002245 size_t zlen;
2246
Hanno Becker982da7e2019-09-02 09:47:39 +01002247#if defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01002248 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2249 ssl->handshake->ecdh_privkey,
2250 p + 2 );
2251 if( ret == UECC_FAULT_DETECTED )
2252 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2253 if( ret != UECC_SUCCESS )
Hanno Becker982da7e2019-09-02 09:47:39 +01002254 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Hanno Becker982da7e2019-09-02 09:47:39 +01002255
2256 zlen = NUM_ECC_BYTES;
2257#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002259 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002260 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002261 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002264 return( ret );
2265 }
2266
Hanno Becker982da7e2019-09-02 09:47:39 +01002267 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2268 MBEDTLS_DEBUG_ECDH_Z );
2269#endif /* MBEDTLS_USE_TINYCRYPT */
2270
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002271 p = mbedtls_platform_put_uint16_be( p, zlen );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002272 p += zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002273 }
2274 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2278 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002279 }
2280
2281 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002282 if( end - p < 2 )
2283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002284
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002285 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002286
2287 if( end < p || (size_t)( end - p ) < psk_len )
2288 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2289
Teppo Järvelin91d79382019-10-02 09:09:31 +03002290 mbedtls_platform_memcpy( p, psk, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002291 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002292
2293 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2294
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002295 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2296
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002297 return( 0 );
2298}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002302/*
2303 * SSLv3.0 MAC functions
2304 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002305#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002306static void ssl_mac( mbedtls_md_context_t *md_ctx,
2307 const unsigned char *secret,
2308 const unsigned char *buf, size_t len,
2309 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002310 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002311{
2312 unsigned char header[11];
2313 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002314 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2316 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002317
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002318 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002320 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002321 else
Paul Bakker68884e32013-01-07 18:20:04 +01002322 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002323
Teppo Järvelin91d79382019-10-02 09:09:31 +03002324 mbedtls_platform_memcpy( header, ctr, 8 );
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03002325 header[8] = (unsigned char) type;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002326 (void)mbedtls_platform_put_uint16_be( &header[9], len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002327
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002328 mbedtls_platform_memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 mbedtls_md_starts( md_ctx );
2330 mbedtls_md_update( md_ctx, secret, md_size );
2331 mbedtls_md_update( md_ctx, padding, padlen );
2332 mbedtls_md_update( md_ctx, header, 11 );
2333 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002334 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002335
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002336 mbedtls_platform_memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 mbedtls_md_starts( md_ctx );
2338 mbedtls_md_update( md_ctx, secret, md_size );
2339 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002340 mbedtls_md_update( md_ctx, out, md_size );
2341 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002342}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002344
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002345/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002346 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002347#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002348 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2349 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2350 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2351/* This function makes sure every byte in the memory region is accessed
2352 * (in ascending addresses order) */
2353static void ssl_read_memory( unsigned char *p, size_t len )
2354{
2355 unsigned char acc = 0;
2356 volatile unsigned char force;
2357
2358 for( ; len != 0; p++, len-- )
2359 acc ^= *p;
2360
2361 force = acc;
2362 (void) force;
2363}
2364#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2365
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002366/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002367 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002368 */
Hanno Becker3307b532017-12-27 21:37:21 +00002369
Hanno Beckera5a2b082019-05-15 14:03:01 +01002370#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002371/* This functions transforms a DTLS plaintext fragment and a record content
2372 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002373 *
2374 * struct {
2375 * opaque content[DTLSPlaintext.length];
2376 * ContentType real_type;
2377 * uint8 zeros[length_of_padding];
2378 * } DTLSInnerPlaintext;
2379 *
2380 * Input:
2381 * - `content`: The beginning of the buffer holding the
2382 * plaintext to be wrapped.
2383 * - `*content_size`: The length of the plaintext in Bytes.
2384 * - `max_len`: The number of Bytes available starting from
2385 * `content`. This must be `>= *content_size`.
2386 * - `rec_type`: The desired record content type.
2387 *
2388 * Output:
2389 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2390 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2391 *
2392 * Returns:
2393 * - `0` on success.
2394 * - A negative error code if `max_len` didn't offer enough space
2395 * for the expansion.
2396 */
2397static int ssl_cid_build_inner_plaintext( unsigned char *content,
2398 size_t *content_size,
2399 size_t remaining,
2400 uint8_t rec_type )
2401{
2402 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002403 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2404 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2405 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002406
2407 /* Write real content type */
2408 if( remaining == 0 )
2409 return( -1 );
2410 content[ len ] = rec_type;
2411 len++;
2412 remaining--;
2413
2414 if( remaining < pad )
2415 return( -1 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002416 mbedtls_platform_memset( content + len, 0, pad );
Hanno Becker92c930f2019-04-29 17:31:37 +01002417 len += pad;
2418 remaining -= pad;
2419
2420 *content_size = len;
2421 return( 0 );
2422}
2423
Hanno Becker7dc25772019-05-20 15:08:01 +01002424/* This function parses a DTLSInnerPlaintext structure.
2425 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002426static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2427 size_t *content_size,
2428 uint8_t *rec_type )
2429{
2430 size_t remaining = *content_size;
2431
2432 /* Determine length of padding by skipping zeroes from the back. */
2433 do
2434 {
2435 if( remaining == 0 )
2436 return( -1 );
2437 remaining--;
2438 } while( content[ remaining ] == 0 );
2439
2440 *content_size = remaining;
2441 *rec_type = content[ remaining ];
2442
2443 return( 0 );
2444}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002445#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002446
Hanno Becker99abf512019-05-20 14:50:53 +01002447/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002448 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002449static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002450 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002451 mbedtls_record *rec )
2452{
Hanno Becker99abf512019-05-20 14:50:53 +01002453 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002454 *
2455 * additional_data = seq_num + TLSCompressed.type +
2456 * TLSCompressed.version + TLSCompressed.length;
2457 *
Hanno Becker99abf512019-05-20 14:50:53 +01002458 * For the CID extension, this is extended as follows
2459 * (quoting draft-ietf-tls-dtls-connection-id-05,
2460 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002461 *
2462 * additional_data = seq_num + DTLSPlaintext.type +
2463 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002464 * cid +
2465 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002466 * length_of_DTLSInnerPlaintext;
2467 */
2468
Teppo Järvelin91d79382019-10-02 09:09:31 +03002469 mbedtls_platform_memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002470 add_data[8] = rec->type;
Teppo Järvelin91d79382019-10-02 09:09:31 +03002471 mbedtls_platform_memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002472
Hanno Beckera5a2b082019-05-15 14:03:01 +01002473#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002474 if( rec->cid_len != 0 )
2475 {
Teppo Järvelin91d79382019-10-02 09:09:31 +03002476 mbedtls_platform_memcpy( add_data + 11, rec->cid, rec->cid_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002477 add_data[11 + rec->cid_len + 0] = rec->cid_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002478 (void)mbedtls_platform_put_uint16_be( &add_data[11 + rec->cid_len + 1],
2479 rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002480 *add_data_len = 13 + 1 + rec->cid_len;
2481 }
2482 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002483#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002484 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002485 (void)mbedtls_platform_put_uint16_be( &add_data[11], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002486 *add_data_len = 13;
2487 }
Hanno Becker3307b532017-12-27 21:37:21 +00002488}
2489
Hanno Becker611a83b2018-01-03 14:27:32 +00002490int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2491 mbedtls_ssl_transform *transform,
2492 mbedtls_record *rec,
2493 int (*f_rng)(void *, unsigned char *, size_t),
2494 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002495{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002497 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002498 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002499 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002500 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002501 size_t post_avail;
2502
2503 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002504#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002505 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002506 ((void) ssl);
2507#endif
2508
2509 /* The PRNG is used for dynamic IV generation that's used
2510 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2511#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2512 ( defined(MBEDTLS_AES_C) || \
2513 defined(MBEDTLS_ARIA_C) || \
2514 defined(MBEDTLS_CAMELLIA_C) ) && \
2515 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2516 ((void) f_rng);
2517 ((void) p_rng);
2518#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
Hanno Becker3307b532017-12-27 21:37:21 +00002522 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002523 {
Hanno Becker3307b532017-12-27 21:37:21 +00002524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2525 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2526 }
Hanno Becker505089d2019-05-01 09:45:57 +01002527 if( rec == NULL
2528 || rec->buf == NULL
2529 || rec->buf_len < rec->data_offset
2530 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002531#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002532 || rec->cid_len != 0
2533#endif
2534 )
Hanno Becker3307b532017-12-27 21:37:21 +00002535 {
2536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002538 }
2539
Hanno Becker3307b532017-12-27 21:37:21 +00002540 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002541 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002543 data, rec->data_len );
2544
2545 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2546
2547 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2548 {
2549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2550 (unsigned) rec->data_len,
2551 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2553 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002554
Hanno Beckera5a2b082019-05-15 14:03:01 +01002555#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002556 /*
2557 * Add CID information
2558 */
2559 rec->cid_len = transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03002560 /* Not using more secure mbedtls_platform_memcpy as cid is public */
2561 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
Hanno Beckere83efe62019-04-29 13:52:53 +01002562 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002563
2564 if( rec->cid_len != 0 )
2565 {
2566 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002567 * Wrap plaintext into DTLSInnerPlaintext structure.
2568 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002569 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002570 * Note that this changes `rec->data_len`, and hence
2571 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002572 */
2573 if( ssl_cid_build_inner_plaintext( data,
2574 &rec->data_len,
2575 post_avail,
2576 rec->type ) != 0 )
2577 {
2578 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2579 }
2580
2581 rec->type = MBEDTLS_SSL_MSG_CID;
2582 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002583#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002584
Hanno Becker92c930f2019-04-29 17:31:37 +01002585 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2586
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002588 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002590#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591 if( mode == MBEDTLS_MODE_STREAM ||
2592 ( mode == MBEDTLS_MODE_CBC
2593#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002594 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002595#endif
2596 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 {
Hanno Becker3307b532017-12-27 21:37:21 +00002598 if( post_avail < transform->maclen )
2599 {
2600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2601 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2602 }
2603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002605 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2606 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002607 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002608 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002609 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2610 data, rec->data_len, rec->ctr, rec->type, mac );
Teppo Järvelin91d79382019-10-02 09:09:31 +03002611 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002612 }
2613 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002614#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2616 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002617 if( mbedtls_ssl_ver_geq(
2618 mbedtls_ssl_transform_get_minor_ver( transform ),
2619 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002620 {
Hanno Becker992b6872017-11-09 18:57:39 +00002621 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2622
Hanno Beckere83efe62019-04-29 13:52:53 +01002623 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002624
Hanno Becker3307b532017-12-27 21:37:21 +00002625 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002626 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002627 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2628 data, rec->data_len );
2629 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2630 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2631
Teppo Järvelin91d79382019-10-02 09:09:31 +03002632 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002633 }
2634 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002635#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2638 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002639 }
2640
Hanno Becker3307b532017-12-27 21:37:21 +00002641 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2642 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002643
Hanno Becker3307b532017-12-27 21:37:21 +00002644 rec->data_len += transform->maclen;
2645 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002646 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002647 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002648#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002649
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002650 /*
2651 * Encrypt
2652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2654 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002656 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002657 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002659 "including %d bytes of padding",
2660 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002661
Hanno Becker3307b532017-12-27 21:37:21 +00002662 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2663 transform->iv_enc, transform->ivlen,
2664 data, rec->data_len,
2665 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002668 return( ret );
2669 }
2670
Hanno Becker3307b532017-12-27 21:37:21 +00002671 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2674 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002675 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 }
Paul Bakker68884e32013-01-07 18:20:04 +01002677 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002679
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002680#if defined(MBEDTLS_GCM_C) || \
2681 defined(MBEDTLS_CCM_C) || \
2682 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002684 mode == MBEDTLS_MODE_CCM ||
2685 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002686 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002687 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002688 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002689 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002690
Hanno Becker3307b532017-12-27 21:37:21 +00002691 /* Check that there's space for both the authentication tag
2692 * and the explicit IV before and after the record content. */
2693 if( post_avail < transform->taglen ||
2694 rec->data_offset < explicit_iv_len )
2695 {
2696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2697 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2698 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002699
Paul Bakker68884e32013-01-07 18:20:04 +01002700 /*
2701 * Generate IV
2702 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002703 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2704 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002705 /* GCM and CCM: fixed || explicit (=seqnum) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002706 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2707 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, rec->ctr,
Hanno Becker3307b532017-12-27 21:37:21 +00002708 explicit_iv_len );
2709 /* Prefix record content with explicit IV. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002710 mbedtls_platform_memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002711 }
2712 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2713 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002714 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002715 unsigned char i;
2716
Teppo Järvelin91d79382019-10-02 09:09:31 +03002717 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002718
2719 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002720 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002721 }
2722 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002723 {
2724 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2726 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002727 }
2728
Hanno Beckere83efe62019-04-29 13:52:53 +01002729 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002730
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002731 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2732 iv, transform->ivlen );
2733 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002734 data - explicit_iv_len, explicit_iv_len );
2735 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002736 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002738 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002739 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002740
Paul Bakker68884e32013-01-07 18:20:04 +01002741 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002742 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002743 */
Hanno Becker3307b532017-12-27 21:37:21 +00002744
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002745 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002746 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002747 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002748 data, rec->data_len, /* source */
2749 data, &rec->data_len, /* destination */
2750 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002753 return( ret );
2754 }
2755
Hanno Becker3307b532017-12-27 21:37:21 +00002756 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2757 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002758
Hanno Becker3307b532017-12-27 21:37:21 +00002759 rec->data_len += transform->taglen + explicit_iv_len;
2760 rec->data_offset -= explicit_iv_len;
2761 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002762 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002763 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002764 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2766#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002767 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002769 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002770 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002771 size_t padlen, i;
2772 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002773
Hanno Becker3307b532017-12-27 21:37:21 +00002774 /* Currently we're always using minimal padding
2775 * (up to 255 bytes would be allowed). */
2776 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2777 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 padlen = 0;
2779
Hanno Becker3307b532017-12-27 21:37:21 +00002780 /* Check there's enough space in the buffer for the padding. */
2781 if( post_avail < padlen + 1 )
2782 {
2783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2784 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2785 }
2786
Paul Bakker5121ce52009-01-03 21:22:43 +00002787 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002788 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002789
Hanno Becker3307b532017-12-27 21:37:21 +00002790 rec->data_len += padlen + 1;
2791 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002794 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002795 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2796 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002797 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002798 if( mbedtls_ssl_ver_geq(
2799 mbedtls_ssl_transform_get_minor_ver( transform ),
2800 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002801 {
Hanno Becker3307b532017-12-27 21:37:21 +00002802 if( f_rng == NULL )
2803 {
2804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2805 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2806 }
2807
2808 if( rec->data_offset < transform->ivlen )
2809 {
2810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2811 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2812 }
2813
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002814 /*
2815 * Generate IV
2816 */
Hanno Becker3307b532017-12-27 21:37:21 +00002817 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002818 if( ret != 0 )
2819 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002820
Teppo Järvelin91d79382019-10-02 09:09:31 +03002821 mbedtls_platform_memcpy( data - transform->ivlen, transform->iv_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002822 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002823
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002824 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002828 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002829 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002830 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002831
Hanno Becker3307b532017-12-27 21:37:21 +00002832 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2833 transform->iv_enc,
2834 transform->ivlen,
2835 data, rec->data_len,
2836 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002839 return( ret );
2840 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002841
Hanno Becker3307b532017-12-27 21:37:21 +00002842 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2845 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002846 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002849 if( mbedtls_ssl_ver_lt(
2850 mbedtls_ssl_transform_get_minor_ver( transform ),
2851 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02002852 {
2853 /*
2854 * Save IV in SSL3 and TLS1
2855 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002856 mbedtls_platform_memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
Hanno Becker3307b532017-12-27 21:37:21 +00002857 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002858 }
Hanno Becker3307b532017-12-27 21:37:21 +00002859 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002860#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002861 {
2862 data -= transform->ivlen;
2863 rec->data_offset -= transform->ivlen;
2864 rec->data_len += transform->ivlen;
2865 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002868 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002869 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002870 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2871
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002872 /*
2873 * MAC(MAC_write_key, seq_num +
2874 * TLSCipherText.type +
2875 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002876 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002877 * IV + // except for TLS 1.0
2878 * ENC(content + padding + padding_length));
2879 */
Hanno Becker3307b532017-12-27 21:37:21 +00002880
2881 if( post_avail < transform->maclen)
2882 {
2883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2884 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2885 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002886
Hanno Beckere83efe62019-04-29 13:52:53 +01002887 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002890 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002891 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002892
Hanno Becker3307b532017-12-27 21:37:21 +00002893 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002894 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002895 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2896 data, rec->data_len );
2897 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2898 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002899
Teppo Järvelin91d79382019-10-02 09:09:31 +03002900 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002901
Hanno Becker3307b532017-12-27 21:37:21 +00002902 rec->data_len += transform->maclen;
2903 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002904 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002905 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002907 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002908 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002910 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2913 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002914 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002915
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002916 /* Make extra sure authentication was performed, exactly once */
2917 if( auth_done != 1 )
2918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2920 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002921 }
2922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002924
2925 return( 0 );
2926}
2927
Hanno Becker40478be2019-07-12 08:23:59 +01002928int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002929 mbedtls_ssl_transform *transform,
2930 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002931{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002932 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002934 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002935#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002936 size_t padlen = 0, correct = 1;
2937#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002938 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002939 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002940 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002941
Hanno Becker611a83b2018-01-03 14:27:32 +00002942#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002943 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002944 ((void) ssl);
2945#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002948 if( rec == NULL ||
2949 rec->buf == NULL ||
2950 rec->buf_len < rec->data_offset ||
2951 rec->buf_len - rec->data_offset < rec->data_len )
2952 {
2953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002955 }
2956
Hanno Becker4c6876b2017-12-27 21:28:58 +00002957 data = rec->buf + rec->data_offset;
2958 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002959
Hanno Beckera5a2b082019-05-15 14:03:01 +01002960#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002961 /*
2962 * Match record's CID with incoming CID.
2963 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002964 if( rec->cid_len != transform->in_cid_len ||
Teppo Järvelin0efac532019-10-04 13:21:08 +03002965 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) // use regular memcmp as CID is public
Hanno Beckerabd7c892019-05-08 13:02:22 +01002966 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002967 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002968 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002969#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2972 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002973 {
2974 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002975 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2976 transform->iv_dec,
2977 transform->ivlen,
2978 data, rec->data_len,
2979 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002982 return( ret );
2983 }
2984
Hanno Becker4c6876b2017-12-27 21:28:58 +00002985 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2988 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002989 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002990 }
Paul Bakker68884e32013-01-07 18:20:04 +01002991 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002993#if defined(MBEDTLS_GCM_C) || \
2994 defined(MBEDTLS_CCM_C) || \
2995 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002997 mode == MBEDTLS_MODE_CCM ||
2998 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002999 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003000 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003001 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003002
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003003 /*
3004 * Prepare IV from explicit and implicit data.
3005 */
3006
3007 /* Check that there's enough space for the explicit IV
3008 * (at the beginning of the record) and the MAC (at the
3009 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003010 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003013 "+ taglen (%d)", rec->data_len,
3014 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003016 }
Paul Bakker68884e32013-01-07 18:20:04 +01003017
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003018#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003019 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
3020 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003021 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01003022
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003023 /* Fixed */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003024 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003025 /* Explicit */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003026 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003027 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003028 else
3029#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3030#if defined(MBEDTLS_CHACHAPOLY_C)
3031 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003032 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02003033 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003034 unsigned char i;
3035
Teppo Järvelin91d79382019-10-02 09:09:31 +03003036 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003037
3038 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003039 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003040 }
3041 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003042#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003043 {
3044 /* Reminder if we ever add an AEAD mode with a different size */
3045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3046 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3047 }
3048
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003049 /* Group changes to data, data_len, and add_data, because
3050 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003051 data += explicit_iv_len;
3052 rec->data_offset += explicit_iv_len;
3053 rec->data_len -= explicit_iv_len + transform->taglen;
3054
Hanno Beckere83efe62019-04-29 13:52:53 +01003055 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003056 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01003057 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003058
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003059 /* Because of the check above, we know that there are
3060 * explicit_iv_len Bytes preceeding data, and taglen
3061 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01003062 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003063 * mbedtls_cipher_auth_decrypt() below. */
3064
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003065 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003066 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00003067 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01003068
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003069 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003070 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003071 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003072 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
3073 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01003074 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003075 data, rec->data_len,
3076 data, &olen,
3077 data + rec->data_len,
3078 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
3083 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003084
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003085 return( ret );
3086 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003087 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003088
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003089 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003090 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3093 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003094 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003095 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003096 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3098#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003099 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003101 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003102 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003103
Paul Bakker5121ce52009-01-03 21:22:43 +00003104 /*
Paul Bakker45829992013-01-03 14:52:21 +01003105 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003108 if( mbedtls_ssl_ver_geq(
3109 mbedtls_ssl_transform_get_minor_ver( transform ),
3110 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003111 {
3112 /* The ciphertext is prefixed with the CBC IV. */
3113 minlen += transform->ivlen;
3114 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003115#endif
Paul Bakker45829992013-01-03 14:52:21 +01003116
Hanno Becker4c6876b2017-12-27 21:28:58 +00003117 /* Size considerations:
3118 *
3119 * - The CBC cipher text must not be empty and hence
3120 * at least of size transform->ivlen.
3121 *
3122 * Together with the potential IV-prefix, this explains
3123 * the first of the two checks below.
3124 *
3125 * - The record must contain a MAC, either in plain or
3126 * encrypted, depending on whether Encrypt-then-MAC
3127 * is used or not.
3128 * - If it is, the message contains the IV-prefix,
3129 * the CBC ciphertext, and the MAC.
3130 * - If it is not, the padded plaintext, and hence
3131 * the CBC ciphertext, has at least length maclen + 1
3132 * because there is at least the padding length byte.
3133 *
3134 * As the CBC ciphertext is not empty, both cases give the
3135 * lower bound minlen + maclen + 1 on the record size, which
3136 * we test for in the second check below.
3137 */
3138 if( rec->data_len < minlen + transform->ivlen ||
3139 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003142 "+ 1 ) ( + expl IV )", rec->data_len,
3143 transform->ivlen,
3144 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003146 }
3147
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003148 /*
3149 * Authenticate before decrypt if enabled
3150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003152 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003153 {
Hanno Becker992b6872017-11-09 18:57:39 +00003154 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003157
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003158 /* Update data_len in tandem with add_data.
3159 *
3160 * The subtraction is safe because of the previous check
3161 * data_len >= minlen + maclen + 1.
3162 *
3163 * Afterwards, we know that data + data_len is followed by at
3164 * least maclen Bytes, which justifies the call to
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003165 * mbedtls_platform_memcmp() below.
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003166 *
3167 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003168 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003169 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003170
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003171 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003172 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3173 add_data_len );
3174 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3175 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003176 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3177 data, rec->data_len );
3178 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3179 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003180
Hanno Becker4c6876b2017-12-27 21:28:58 +00003181 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3182 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003183 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003184 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003185
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003186 /* Compare expected MAC with MAC at the end of the record. */
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003187 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003188 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003192 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003193 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003194 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003196
3197 /*
3198 * Check length sanity
3199 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003200
3201 /* We know from above that data_len > minlen >= 0,
3202 * so the following check in particular implies that
3203 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003204 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003207 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003209 }
3210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003212 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003213 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003214 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003215 if( mbedtls_ssl_ver_geq(
3216 mbedtls_ssl_transform_get_minor_ver( transform ),
3217 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003218 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003219 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003220 mbedtls_platform_memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003221
Hanno Becker4c6876b2017-12-27 21:28:58 +00003222 data += transform->ivlen;
3223 rec->data_offset += transform->ivlen;
3224 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003225 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003227
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003228 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3229
Hanno Becker4c6876b2017-12-27 21:28:58 +00003230 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3231 transform->iv_dec, transform->ivlen,
3232 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003235 return( ret );
3236 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003237
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003238 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003239 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003243 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003246 if( mbedtls_ssl_ver_lt(
3247 mbedtls_ssl_transform_get_minor_ver( transform ),
3248 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02003249 {
3250 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003251 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3252 * records is equivalent to CBC decryption of the concatenation
3253 * of the records; in other words, IVs are maintained across
3254 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003255 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003256 mbedtls_platform_memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003257 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003258 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003259#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003260
Hanno Becker4c6876b2017-12-27 21:28:58 +00003261 /* Safe since data_len >= minlen + maclen + 1, so after having
3262 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003263 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3264 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003265 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003266
Hanno Becker4c6876b2017-12-27 21:28:58 +00003267 if( auth_done == 1 )
3268 {
3269 correct *= ( rec->data_len >= padlen + 1 );
3270 padlen *= ( rec->data_len >= padlen + 1 );
3271 }
3272 else
Paul Bakker45829992013-01-03 14:52:21 +01003273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003274#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003275 if( rec->data_len < transform->maclen + padlen + 1 )
3276 {
3277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3278 rec->data_len,
3279 transform->maclen,
3280 padlen + 1 ) );
3281 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003282#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003283
3284 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3285 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003286 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003287
Hanno Becker4c6876b2017-12-27 21:28:58 +00003288 padlen++;
3289
3290 /* Regardless of the validity of the padding,
3291 * we have data_len >= padlen here. */
3292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003294 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3295 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003297 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299#if defined(MBEDTLS_SSL_DEBUG_ALL)
3300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003301 "should be no more than %d",
3302 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003303#endif
Paul Bakker45829992013-01-03 14:52:21 +01003304 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003305 }
3306 }
3307 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3309#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3310 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003311 if( mbedtls_ssl_ver_gt(
3312 mbedtls_ssl_transform_get_minor_ver( transform ),
3313 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003314 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003315 /* The padding check involves a series of up to 256
3316 * consecutive memory reads at the end of the record
3317 * plaintext buffer. In order to hide the length and
3318 * validity of the padding, always perform exactly
3319 * `min(256,plaintext_len)` reads (but take into account
3320 * only the last `padlen` bytes for the padding check). */
3321 size_t pad_count = 0;
3322 size_t real_count = 0;
3323 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003324
Hanno Becker4c6876b2017-12-27 21:28:58 +00003325 /* Index of first padding byte; it has been ensured above
3326 * that the subtraction is safe. */
3327 size_t const padding_idx = rec->data_len - padlen;
3328 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3329 size_t const start_idx = rec->data_len - num_checks;
3330 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003331
Hanno Becker4c6876b2017-12-27 21:28:58 +00003332 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003333 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003334 real_count |= ( idx >= padding_idx );
3335 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003336 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003337 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003340 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003342#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003343 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003344 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003345 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3347 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3350 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003351 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003352
Hanno Becker4c6876b2017-12-27 21:28:58 +00003353 /* If the padding was found to be invalid, padlen == 0
3354 * and the subtraction is safe. If the padding was found valid,
3355 * padlen hasn't been changed and the previous assertion
3356 * data_len >= padlen still holds. */
3357 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003358 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003359 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003361 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3364 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003365 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003366
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003367#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003369 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003370#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003371
3372 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003373 * Authenticate if not done yet.
3374 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003375 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003376#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003377 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003378 {
Hanno Becker992b6872017-11-09 18:57:39 +00003379 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003380
Hanno Becker4c6876b2017-12-27 21:28:58 +00003381 /* If the initial value of padlen was such that
3382 * data_len < maclen + padlen + 1, then padlen
3383 * got reset to 1, and the initial check
3384 * data_len >= minlen + maclen + 1
3385 * guarantees that at this point we still
3386 * have at least data_len >= maclen.
3387 *
3388 * If the initial value of padlen was such that
3389 * data_len >= maclen + padlen + 1, then we have
3390 * subtracted either padlen + 1 (if the padding was correct)
3391 * or 0 (if the padding was incorrect) since then,
3392 * hence data_len >= maclen in any case.
3393 */
3394 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003395 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003398 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3399 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003400 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003401 ssl_mac( &transform->md_ctx_dec,
3402 transform->mac_dec,
3403 data, rec->data_len,
3404 rec->ctr, rec->type,
3405 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003406 }
3407 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3409#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3410 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003411 if( mbedtls_ssl_ver_gt(
3412 mbedtls_ssl_transform_get_minor_ver( transform ),
3413 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003414 {
3415 /*
3416 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003417 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003418 *
3419 * Known timing attacks:
3420 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3421 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003422 * To compensate for different timings for the MAC calculation
3423 * depending on how much padding was removed (which is determined
3424 * by padlen), process extra_run more blocks through the hash
3425 * function.
3426 *
3427 * The formula in the paper is
3428 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3429 * where L1 is the size of the header plus the decrypted message
3430 * plus CBC padding and L2 is the size of the header plus the
3431 * decrypted message. This is for an underlying hash function
3432 * with 64-byte blocks.
3433 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3434 * correctly. We round down instead of up, so -56 is the correct
3435 * value for our calculations instead of -55.
3436 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003437 * Repeat the formula rather than defining a block_size variable.
3438 * This avoids requiring division by a variable at runtime
3439 * (which would be marginally less efficient and would require
3440 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003441 */
3442 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003443 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003444
3445 /*
3446 * The next two sizes are the minimum and maximum values of
3447 * in_msglen over all padlen values.
3448 *
3449 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003450 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003451 *
3452 * Note that max_len + maclen is never more than the buffer
3453 * length, as we previously did in_msglen -= maclen too.
3454 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003455 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003456 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3457
Hanno Becker4c6876b2017-12-27 21:28:58 +00003458 memset( tmp, 0, sizeof( tmp ) );
3459
Hanno Beckera5cedbc2019-07-17 11:21:02 +01003460 switch( mbedtls_md_get_type(
3461 mbedtls_md_get_handle( &transform->md_ctx_dec ) ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003462 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003463#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3464 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003465 case MBEDTLS_MD_MD5:
3466 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003467 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003468 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003469 extra_run =
3470 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3471 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003472 break;
3473#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003474#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003475 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003476 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003477 extra_run =
3478 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3479 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003480 break;
3481#endif
3482 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003484 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3485 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003486
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003487 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003488
Hanno Beckere83efe62019-04-29 13:52:53 +01003489 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3490 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003491 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3492 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003493 /* Make sure we access everything even when padlen > 0. This
3494 * makes the synchronisation requirements for just-in-time
3495 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003496 ssl_read_memory( data + rec->data_len, padlen );
3497 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003498
3499 /* Call mbedtls_md_process at least once due to cache attacks
3500 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003501 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003502 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003503
Hanno Becker4c6876b2017-12-27 21:28:58 +00003504 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003505
3506 /* Make sure we access all the memory that could contain the MAC,
3507 * before we check it in the next code block. This makes the
3508 * synchronisation requirements for just-in-time Prime+Probe
3509 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003510 ssl_read_memory( data + min_len,
3511 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003512 }
3513 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3515 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3518 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003519 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003520
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003521#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003522 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3523 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003524#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003525
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003526 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003527 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003529#if defined(MBEDTLS_SSL_DEBUG_ALL)
3530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003531#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003532 correct = 0;
3533 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003534 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003535 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003536
3537 /*
3538 * Finally check the correct flag
3539 */
3540 if( correct == 0 )
3541 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003542#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003543
3544 /* Make extra sure authentication was performed, exactly once */
3545 if( auth_done != 1 )
3546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003549 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003550
Hanno Beckera5a2b082019-05-15 14:03:01 +01003551#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003552 if( rec->cid_len != 0 )
3553 {
3554 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3555 &rec->type );
3556 if( ret != 0 )
3557 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3558 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003559#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003562
3563 return( 0 );
3564}
3565
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003566#undef MAC_NONE
3567#undef MAC_PLAINTEXT
3568#undef MAC_CIPHERTEXT
3569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003570#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003571/*
3572 * Compression/decompression functions
3573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003575{
3576 int ret;
3577 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003578 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003579 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003580 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003583
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003584 if( len_pre == 0 )
3585 return( 0 );
3586
Teppo Järvelin91d79382019-10-02 09:09:31 +03003587 mbedtls_platform_memcpy( msg_pre, ssl->out_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003590 ssl->out_msglen ) );
3591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003593 ssl->out_msg, ssl->out_msglen );
3594
Paul Bakker48916f92012-09-16 19:57:18 +00003595 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3596 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3597 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003598 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003599
Paul Bakker48916f92012-09-16 19:57:18 +00003600 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003601 if( ret != Z_OK )
3602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3604 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003605 }
3606
Angus Grattond8213d02016-05-25 20:56:48 +10003607 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003608 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003611 ssl->out_msglen ) );
3612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003614 ssl->out_msg, ssl->out_msglen );
3615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003617
3618 return( 0 );
3619}
3620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003622{
3623 int ret;
3624 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003625 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003626 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003627 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003630
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003631 if( len_pre == 0 )
3632 return( 0 );
3633
Teppo Järvelin91d79382019-10-02 09:09:31 +03003634 mbedtls_platform_memcpy( msg_pre, ssl->in_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003637 ssl->in_msglen ) );
3638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003639 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003640 ssl->in_msg, ssl->in_msglen );
3641
Paul Bakker48916f92012-09-16 19:57:18 +00003642 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3643 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3644 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003645 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003646 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003647
Paul Bakker48916f92012-09-16 19:57:18 +00003648 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003649 if( ret != Z_OK )
3650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3652 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003653 }
3654
Angus Grattond8213d02016-05-25 20:56:48 +10003655 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003656 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003659 ssl->in_msglen ) );
3660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003662 ssl->in_msg, ssl->in_msglen );
3663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003665
3666 return( 0 );
3667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3671static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673#if defined(MBEDTLS_SSL_PROTO_DTLS)
3674static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003675{
3676 /* If renegotiation is not enforced, retransmit until we would reach max
3677 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003678 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003679 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003680 uint32_t ratio =
3681 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3682 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003683 unsigned char doublings = 1;
3684
3685 while( ratio != 0 )
3686 {
3687 ++doublings;
3688 ratio >>= 1;
3689 }
3690
3691 if( ++ssl->renego_records_seen > doublings )
3692 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003694 return( 0 );
3695 }
3696 }
3697
3698 return( ssl_write_hello_request( ssl ) );
3699}
3700#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003701#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003702
Paul Bakker5121ce52009-01-03 21:22:43 +00003703/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003704 * Fill the input message buffer by appending data to it.
3705 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003706 *
3707 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3708 * available (from this read and/or a previous one). Otherwise, an error code
3709 * is returned (possibly EOF or WANT_READ).
3710 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003711 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3712 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3713 * since we always read a whole datagram at once.
3714 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003715 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003716 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003719{
Paul Bakker23986e52011-04-24 08:57:21 +00003720 int ret;
3721 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003724
Hanno Beckera58a8962019-06-13 16:11:15 +01003725 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3726 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003729 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003731 }
3732
Angus Grattond8213d02016-05-25 20:56:48 +10003733 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3736 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003737 }
3738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003739#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003740 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003741 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003742 uint32_t timeout;
3743
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003744 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003745 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3746 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003747 {
3748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3749 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3750 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3751 }
3752
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003753 /*
3754 * The point is, we need to always read a full datagram at once, so we
3755 * sometimes read more then requested, and handle the additional data.
3756 * It could be the rest of the current record (while fetching the
3757 * header) and/or some other records in the same datagram.
3758 */
3759
3760 /*
3761 * Move to the next record in the already read datagram if applicable
3762 */
3763 if( ssl->next_record_offset != 0 )
3764 {
3765 if( ssl->in_left < ssl->next_record_offset )
3766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3768 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003769 }
3770
3771 ssl->in_left -= ssl->next_record_offset;
3772
3773 if( ssl->in_left != 0 )
3774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003776 ssl->next_record_offset ) );
3777 memmove( ssl->in_hdr,
3778 ssl->in_hdr + ssl->next_record_offset,
3779 ssl->in_left );
3780 }
3781
3782 ssl->next_record_offset = 0;
3783 }
3784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003786 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003787
3788 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003789 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003790 */
3791 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003794 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003795 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003796
3797 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003798 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003799 * are not at the beginning of a new record, the caller did something
3800 * wrong.
3801 */
3802 if( ssl->in_left != 0 )
3803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3805 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003806 }
3807
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003808 /*
3809 * Don't even try to read if time's out already.
3810 * This avoids by-passing the timer when repeatedly receiving messages
3811 * that will end up being dropped.
3812 */
3813 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003814 {
3815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003816 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003817 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003818 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003819 {
Angus Grattond8213d02016-05-25 20:56:48 +10003820 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003822 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003823 timeout = ssl->handshake->retransmit_timeout;
3824 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003825 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003827 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003828
Hanno Beckera58a8962019-06-13 16:11:15 +01003829 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3830 {
3831 ret = mbedtls_ssl_get_recv_timeout( ssl )
3832 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3833 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003834 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003835 {
3836 ret = mbedtls_ssl_get_recv( ssl )
3837 ( ssl->p_bio, ssl->in_hdr, len );
3838 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003841
3842 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003844 }
3845
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003846 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003849 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003851 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003852 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003853 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003856 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003857 }
3858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003859 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003862 return( ret );
3863 }
3864
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003865 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003866 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003868 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3869 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003870 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003871 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003872 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003875 return( ret );
3876 }
3877
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003878 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003879 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003881 }
3882
Paul Bakker5121ce52009-01-03 21:22:43 +00003883 if( ret < 0 )
3884 return( ret );
3885
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003886 ssl->in_left = ret;
3887 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003888 MBEDTLS_SSL_TRANSPORT_ELSE
3889#endif /* MBEDTLS_SSL_PROTO_DTLS */
3890#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003893 ssl->in_left, nb_want ) );
3894
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003895 while( ssl->in_left < nb_want )
3896 {
3897 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003898
3899 if( ssl_check_timer( ssl ) != 0 )
3900 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3901 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003902 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003903 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003904 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003905 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3906 ssl->in_hdr + ssl->in_left, len,
3907 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003908 }
3909 else
3910 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003911 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003912 ssl->in_hdr + ssl->in_left, len );
3913 }
3914 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003917 ssl->in_left, nb_want ) );
3918 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003919
3920 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003921 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003922
3923 if( ret < 0 )
3924 return( ret );
3925
mohammad160352aecb92018-03-28 23:41:40 -07003926 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003927 {
Darryl Green11999bb2018-03-13 15:22:58 +00003928 MBEDTLS_SSL_DEBUG_MSG( 1,
3929 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003930 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003931 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3932 }
3933
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003934 ssl->in_left += ret;
3935 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003936 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003937#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003940
3941 return( 0 );
3942}
3943
3944/*
3945 * Flush any data not yet written
3946 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003947int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003948{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003949 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003950 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003953
Hanno Beckera58a8962019-06-13 16:11:15 +01003954 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003957 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003958 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003959 }
3960
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003961 /* Avoid incrementing counter if data is flushed */
3962 if( ssl->out_left == 0 )
3963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003965 return( 0 );
3966 }
3967
Paul Bakker5121ce52009-01-03 21:22:43 +00003968 while( ssl->out_left > 0 )
3969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003971 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003972
Hanno Becker2b1e3542018-08-06 11:19:13 +01003973 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003974 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003976 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003977
3978 if( ret <= 0 )
3979 return( ret );
3980
mohammad160352aecb92018-03-28 23:41:40 -07003981 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003982 {
Darryl Green11999bb2018-03-13 15:22:58 +00003983 MBEDTLS_SSL_DEBUG_MSG( 1,
3984 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003985 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003986 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3987 }
3988
Paul Bakker5121ce52009-01-03 21:22:43 +00003989 ssl->out_left -= ret;
3990 }
3991
Hanno Becker2b1e3542018-08-06 11:19:13 +01003992#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003993 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003994 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003995 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003996 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003997 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003998#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003999#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01004000 {
4001 ssl->out_hdr = ssl->out_buf + 8;
4002 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004003#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01004004 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004007
4008 return( 0 );
4009}
4010
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004011/*
4012 * Functions to handle the DTLS retransmission state machine
4013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004015/*
4016 * Append current handshake message to current outgoing flight
4017 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004019{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004020 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01004021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
4022 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
4023 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004024
4025 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004026 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004027 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004029 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004030 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004031 }
4032
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004033 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004034 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004036 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004037 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004038 }
4039
4040 /* Copy current handshake message with headers */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004041 mbedtls_platform_memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004042 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004043 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004044 msg->next = NULL;
4045
4046 /* Append to the current flight */
4047 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004048 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004049 else
4050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004051 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004052 while( cur->next != NULL )
4053 cur = cur->next;
4054 cur->next = msg;
4055 }
4056
Hanno Becker3b235902018-08-06 09:54:53 +01004057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004058 return( 0 );
4059}
4060
4061/*
4062 * Free the current flight of handshake messages
4063 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004065{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004066 mbedtls_ssl_flight_item *cur = flight;
4067 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004068
4069 while( cur != NULL )
4070 {
4071 next = cur->next;
4072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004073 mbedtls_free( cur->p );
4074 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004075
4076 cur = next;
4077 }
4078}
4079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4081static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004082#endif
4083
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004084/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004085 * Swap transform_out and out_ctr with the alternative ones
4086 */
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004087static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004090 unsigned char tmp_out_ctr[8];
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004091#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4092 int ret;
4093#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004094
4095 if( ssl->transform_out == ssl->handshake->alt_transform_out )
4096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004097 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004098 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004099 }
4100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004102
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004103 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004104 tmp_transform = ssl->transform_out;
4105 ssl->transform_out = ssl->handshake->alt_transform_out;
4106 ssl->handshake->alt_transform_out = tmp_transform;
4107
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004108 /* Swap epoch + sequence_number */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004109 mbedtls_platform_memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4110 mbedtls_platform_memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
4111 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004112
4113 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004114 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004116#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4117 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004121 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4122 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004123 }
4124 }
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004125#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
4126
4127 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004128}
4129
4130/*
4131 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004132 */
4133int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4134{
4135 int ret = 0;
4136
4137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4138
4139 ret = mbedtls_ssl_flight_transmit( ssl );
4140
4141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4142
4143 return( ret );
4144}
4145
4146/*
4147 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004148 *
4149 * Need to remember the current message in case flush_output returns
4150 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004151 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004152 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004153int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004154{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004155 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004158 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004159 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004160 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004161
4162 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004163 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004164 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4165 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004167 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004168 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004169
4170 while( ssl->handshake->cur_msg != NULL )
4171 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004172 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004173 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004174
Hanno Beckere1dcb032018-08-17 16:47:58 +01004175 int const is_finished =
4176 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4177 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4178
Hanno Becker04da1892018-08-14 13:22:10 +01004179 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4180 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4181
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004182 /* Swap epochs before sending Finished: we can't do it after
4183 * sending ChangeCipherSpec, in case write returns WANT_READ.
4184 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004185 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004186 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004188 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4189 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004190 }
4191
Hanno Becker67bc7c32018-08-06 11:33:50 +01004192 ret = ssl_get_remaining_payload_in_datagram( ssl );
4193 if( ret < 0 )
4194 return( ret );
4195 max_frag_len = (size_t) ret;
4196
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004197 /* CCS is copied as is, while HS messages may need fragmentation */
4198 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4199 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004200 if( max_frag_len == 0 )
4201 {
4202 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4203 return( ret );
4204
4205 continue;
4206 }
4207
Teppo Järvelin91d79382019-10-02 09:09:31 +03004208 mbedtls_platform_memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004209 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004210 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004211
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004212 /* Update position inside current message */
4213 ssl->handshake->cur_msg_p += cur->len;
4214 }
4215 else
4216 {
4217 const unsigned char * const p = ssl->handshake->cur_msg_p;
4218 const size_t hs_len = cur->len - 12;
4219 const size_t frag_off = p - ( cur->p + 12 );
4220 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004221 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004222
Hanno Beckere1dcb032018-08-17 16:47:58 +01004223 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004224 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004225 if( is_finished )
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004226 {
4227 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4228 return( ret );
4229 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004230
Hanno Becker67bc7c32018-08-06 11:33:50 +01004231 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4232 return( ret );
4233
4234 continue;
4235 }
4236 max_hs_frag_len = max_frag_len - 12;
4237
4238 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4239 max_hs_frag_len : rem_len;
4240
4241 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004242 {
4243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004244 (unsigned) cur_hs_frag_len,
4245 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004246 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004247
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004248 /* Messages are stored with handshake headers as if not fragmented,
4249 * copy beginning of headers then fill fragmentation fields.
4250 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004251 mbedtls_platform_memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004252
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004253 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[6], frag_off );
4254 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[9],
4255 cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004256
4257 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4258
Hanno Becker3f7b9732018-08-28 09:53:25 +01004259 /* Copy the handshake message content and set records fields */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004260 mbedtls_platform_memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004261 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004262 ssl->out_msgtype = cur->type;
4263
4264 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004265 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004266 }
4267
4268 /* If done with the current message move to the next one if any */
4269 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4270 {
4271 if( cur->next != NULL )
4272 {
4273 ssl->handshake->cur_msg = cur->next;
4274 ssl->handshake->cur_msg_p = cur->next->p + 12;
4275 }
4276 else
4277 {
4278 ssl->handshake->cur_msg = NULL;
4279 ssl->handshake->cur_msg_p = NULL;
4280 }
4281 }
4282
4283 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004284 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004286 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004287 return( ret );
4288 }
4289 }
4290
Hanno Becker67bc7c32018-08-06 11:33:50 +01004291 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4292 return( ret );
4293
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004294 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004295 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4296 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004297 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004299 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004300 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4301 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004302
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004304
4305 return( 0 );
4306}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004307
4308/*
4309 * To be called when the last message of an incoming flight is received.
4310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004312{
4313 /* We won't need to resend that one any more */
4314 ssl_flight_free( ssl->handshake->flight );
4315 ssl->handshake->flight = NULL;
4316 ssl->handshake->cur_msg = NULL;
4317
4318 /* The next incoming flight will start with this msg_seq */
4319 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4320
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004321 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004322 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004323
Hanno Becker0271f962018-08-16 13:23:47 +01004324 /* Clear future message buffering structure. */
4325 ssl_buffering_free( ssl );
4326
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004327 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004328 ssl_set_timer( ssl, 0 );
4329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004330 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4331 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004334 }
4335 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004336 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004337}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004338
4339/*
4340 * To be called when the last message of an outgoing flight is send.
4341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004342void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004343{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004344 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004345 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004347 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4348 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004350 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004351 }
4352 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004353 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004355#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004356
Paul Bakker5121ce52009-01-03 21:22:43 +00004357/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004358 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004359 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004360
4361/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004362 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004363 *
4364 * - fill in handshake headers
4365 * - update handshake checksum
4366 * - DTLS: save message for resending
4367 * - then pass to the record layer
4368 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004369 * DTLS: except for HelloRequest, messages are only queued, and will only be
4370 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004371 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004372 * Inputs:
4373 * - ssl->out_msglen: 4 + actual handshake message len
4374 * (4 is the size of handshake headers for TLS)
4375 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4376 * - ssl->out_msg + 4: the handshake message body
4377 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004378 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004379 * - ssl->out_msglen: the length of the record contents
4380 * (including handshake headers but excluding record headers)
4381 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004382 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004383int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004384{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004385 int ret;
4386 const size_t hs_len = ssl->out_msglen - 4;
4387 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004388
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4390
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004391 /*
4392 * Sanity checks
4393 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004394 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004395 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4396 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004397 /* In SSLv3, the client might send a NoCertificate alert. */
4398#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004399 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004400 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004401 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4402 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004403#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4404 {
4405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4406 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4407 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004408 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004409
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004410 /* Whenever we send anything different from a
4411 * HelloRequest we should be in a handshake - double check. */
4412 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4413 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004414 ssl->handshake == NULL )
4415 {
4416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4417 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4418 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004420#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004421 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004422 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004423 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004424 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4426 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004427 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004428#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004429
Hanno Beckerb50a2532018-08-06 11:52:54 +01004430 /* Double-check that we did not exceed the bounds
4431 * of the outgoing record buffer.
4432 * This should never fail as the various message
4433 * writing functions must obey the bounds of the
4434 * outgoing record buffer, but better be safe.
4435 *
4436 * Note: We deliberately do not check for the MTU or MFL here.
4437 */
4438 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4439 {
4440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4441 "size %u, maximum %u",
4442 (unsigned) ssl->out_msglen,
4443 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4444 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4445 }
4446
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004447 /*
4448 * Fill handshake headers
4449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004450 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004451 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004452 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[1], hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004453
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004454 /*
4455 * DTLS has additional fields in the Handshake layer,
4456 * between the length field and the actual payload:
4457 * uint16 message_seq;
4458 * uint24 fragment_offset;
4459 * uint24 fragment_length;
4460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004462 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004463 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004464 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004465 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004466 {
4467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4468 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004469 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004470 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004471 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4472 }
4473
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004474 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004475 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004476
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004477 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004478 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004479 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004480 (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[4],
4481 ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004482 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004483 }
4484 else
4485 {
4486 ssl->out_msg[4] = 0;
4487 ssl->out_msg[5] = 0;
4488 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004489
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004490 /* Handshake hashes are computed without fragmentation,
4491 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004492 mbedtls_platform_memset( ssl->out_msg + 6, 0x00, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03004493 mbedtls_platform_memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004494 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004495#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004496
Hanno Becker0207e532018-08-28 10:28:28 +01004497 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004498 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004499 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004500 }
4501
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004502 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004503#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004504 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004505 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4506 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004507 {
4508 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004510 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004511 return( ret );
4512 }
4513 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004514 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004515#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004516 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004517 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004518 {
4519 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4520 return( ret );
4521 }
4522 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004523
4524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4525
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004526 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004527}
4528
4529/*
4530 * Record layer functions
4531 */
4532
4533/*
4534 * Write current record.
4535 *
4536 * Uses:
4537 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4538 * - ssl->out_msglen: length of the record content (excl headers)
4539 * - ssl->out_msg: record content
4540 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004541int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004542{
4543 int ret, done = 0;
4544 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004545 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004546
4547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004549#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004550 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004552 {
4553 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004555 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004556 return( ret );
4557 }
4558
4559 len = ssl->out_msglen;
4560 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004561#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004563#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4564 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568 ret = mbedtls_ssl_hw_record_write( ssl );
4569 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004571 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4572 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004573 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004574
4575 if( ret == 0 )
4576 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004577 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004578#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004579 if( !done )
4580 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004581 unsigned i;
4582 size_t protected_record_size;
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004583 volatile int encrypted_fi = 0;
Hanno Becker2b1e3542018-08-06 11:19:13 +01004584
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004585 /* Skip writing the record content type to after the encryption,
4586 * as it may change when using the CID extension. */
4587
Hanno Becker2881d802019-05-22 14:44:53 +01004588 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4589 mbedtls_ssl_get_minor_ver( ssl ),
4590 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004591
Teppo Järvelin91d79382019-10-02 09:09:31 +03004592 mbedtls_platform_memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004593 (void)mbedtls_platform_put_uint16_be( ssl->out_len, len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004594
Paul Bakker48916f92012-09-16 19:57:18 +00004595 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004596 {
Hanno Becker3307b532017-12-27 21:37:21 +00004597 mbedtls_record rec;
4598
4599 rec.buf = ssl->out_iv;
4600 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4601 ( ssl->out_iv - ssl->out_buf );
4602 rec.data_len = ssl->out_msglen;
4603 rec.data_offset = ssl->out_msg - rec.buf;
4604
Teppo Järvelin91d79382019-10-02 09:09:31 +03004605 mbedtls_platform_memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004606 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4607 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004608 ssl->conf->transport, rec.ver );
4609 rec.type = ssl->out_msgtype;
4610
Hanno Beckera5a2b082019-05-15 14:03:01 +01004611#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004612 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004613 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004614#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004615
Hanno Becker611a83b2018-01-03 14:27:32 +00004616 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004617 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004618 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004620 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004621 return( ret );
4622 }
4623
Hanno Becker3307b532017-12-27 21:37:21 +00004624 if( rec.data_offset != 0 )
4625 {
4626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4627 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4628 }
4629
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004630 /* Update the record content type and CID. */
4631 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004632#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03004633 /* Not using more secure mbedtls_platform_memcpy as cid is public */
4634 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004635#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004636 ssl->out_msglen = len = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004637 (void)mbedtls_platform_put_uint16_be( ssl->out_len, rec.data_len );
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004638 encrypted_fi = 1;
4639 }
4640
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004641 /* Double check to ensure the encryption has been done */
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004642 if( ssl->transform_out != NULL && encrypted_fi == 0 )
4643 {
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004644 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004645 }
4646
Hanno Becker43395762019-05-03 14:46:38 +01004647 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004648
4649#if defined(MBEDTLS_SSL_PROTO_DTLS)
4650 /* In case of DTLS, double-check that we don't exceed
4651 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004652 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004653 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004654 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004655 if( ret < 0 )
4656 return( ret );
4657
4658 if( protected_record_size > (size_t) ret )
4659 {
4660 /* Should never happen */
4661 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4662 }
4663 }
4664#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004665
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004666 /* Now write the potentially updated record content type. */
4667 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004669 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004670 "version = [%d:%d], msglen = %d",
4671 ssl->out_hdr[0], ssl->out_hdr[1],
4672 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004674 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004675 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004676
4677 ssl->out_left += protected_record_size;
4678 ssl->out_hdr += protected_record_size;
4679 ssl_update_out_pointers( ssl, ssl->transform_out );
4680
Hanno Becker04484622018-08-06 09:49:38 +01004681 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4682 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4683 break;
4684
4685 /* The loop goes to its end iff the counter is wrapping */
4686 if( i == ssl_ep_len( ssl ) )
4687 {
4688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4689 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4690 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004691 }
4692
Hanno Becker67bc7c32018-08-06 11:33:50 +01004693#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004694 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004695 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004696 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004697 size_t remaining;
4698 ret = ssl_get_remaining_payload_in_datagram( ssl );
4699 if( ret < 0 )
4700 {
4701 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4702 ret );
4703 return( ret );
4704 }
4705
4706 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004707 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004708 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004709 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004710 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004711 else
4712 {
Hanno Becker513815a2018-08-20 11:56:09 +01004713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004714 }
4715 }
4716#endif /* MBEDTLS_SSL_PROTO_DTLS */
4717
4718 if( ( flush == SSL_FORCE_FLUSH ) &&
4719 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004721 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004722 return( ret );
4723 }
4724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004726
4727 return( 0 );
4728}
4729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004730#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004731
4732static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4733{
4734 if( ssl->in_msglen < ssl->in_hslen ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +03004735 mbedtls_platform_memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4736 mbedtls_platform_memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
Hanno Beckere25e3b72018-08-16 09:30:53 +01004737 {
4738 return( 1 );
4739 }
4740 return( 0 );
4741}
Hanno Becker44650b72018-08-16 12:51:11 +01004742
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004743static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004744{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004745 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[9] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004746}
4747
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004748static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004749{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004750 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[6] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004751}
4752
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004753static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004754{
4755 uint32_t msg_len, frag_off, frag_len;
4756
4757 msg_len = ssl_get_hs_total_len( ssl );
4758 frag_off = ssl_get_hs_frag_off( ssl );
4759 frag_len = ssl_get_hs_frag_len( ssl );
4760
4761 if( frag_off > msg_len )
4762 return( -1 );
4763
4764 if( frag_len > msg_len - frag_off )
4765 return( -1 );
4766
4767 if( frag_len + 12 > ssl->in_msglen )
4768 return( -1 );
4769
4770 return( 0 );
4771}
4772
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004773/*
4774 * Mark bits in bitmask (used for DTLS HS reassembly)
4775 */
4776static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4777{
4778 unsigned int start_bits, end_bits;
4779
4780 start_bits = 8 - ( offset % 8 );
4781 if( start_bits != 8 )
4782 {
4783 size_t first_byte_idx = offset / 8;
4784
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004785 /* Special case */
4786 if( len <= start_bits )
4787 {
4788 for( ; len != 0; len-- )
4789 mask[first_byte_idx] |= 1 << ( start_bits - len );
4790
4791 /* Avoid potential issues with offset or len becoming invalid */
4792 return;
4793 }
4794
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004795 offset += start_bits; /* Now offset % 8 == 0 */
4796 len -= start_bits;
4797
4798 for( ; start_bits != 0; start_bits-- )
4799 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4800 }
4801
4802 end_bits = len % 8;
4803 if( end_bits != 0 )
4804 {
4805 size_t last_byte_idx = ( offset + len ) / 8;
4806
4807 len -= end_bits; /* Now len % 8 == 0 */
4808
4809 for( ; end_bits != 0; end_bits-- )
4810 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4811 }
4812
4813 memset( mask + offset / 8, 0xFF, len / 8 );
4814}
4815
4816/*
4817 * Check that bitmask is full
4818 */
4819static int ssl_bitmask_check( unsigned char *mask, size_t len )
4820{
4821 size_t i;
4822
4823 for( i = 0; i < len / 8; i++ )
4824 if( mask[i] != 0xFF )
4825 return( -1 );
4826
4827 for( i = 0; i < len % 8; i++ )
4828 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4829 return( -1 );
4830
4831 return( 0 );
4832}
4833
Hanno Becker56e205e2018-08-16 09:06:12 +01004834/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004835static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004836 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004837{
Hanno Becker56e205e2018-08-16 09:06:12 +01004838 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004839
Hanno Becker56e205e2018-08-16 09:06:12 +01004840 alloc_len = 12; /* Handshake header */
4841 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004842
Hanno Beckerd07df862018-08-16 09:14:58 +01004843 if( add_bitmap )
4844 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004845
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004846 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004847}
Hanno Becker56e205e2018-08-16 09:06:12 +01004848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004849#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004850
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004851static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004852{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004853 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[1] ) );
Hanno Becker12555c62018-08-16 12:47:53 +01004854}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004855
Simon Butcher99000142016-10-13 17:21:01 +01004856int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004857{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004858 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004861 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004862 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004863 }
4864
Hanno Becker12555c62018-08-16 12:47:53 +01004865 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004867 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004868 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004869 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004871#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004872 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004873 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004874 int ret;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004875 unsigned int recv_msg_seq = (unsigned int)
4876 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004877
Hanno Becker44650b72018-08-16 12:51:11 +01004878 if( ssl_check_hs_header( ssl ) != 0 )
4879 {
4880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4881 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4882 }
4883
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004884 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004885 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4886 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4887 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4888 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004889 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004890 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4891 {
4892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4893 recv_msg_seq,
4894 ssl->handshake->in_msg_seq ) );
4895 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4896 }
4897
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004898 /* Retransmit only on last message from previous flight, to avoid
4899 * too many retransmissions.
4900 * Besides, No sane server ever retransmits HelloVerifyRequest */
4901 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004902 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004905 "message_seq = %d, start_of_flight = %d",
4906 recv_msg_seq,
4907 ssl->handshake->in_flight_start_seq ) );
4908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004909 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004912 return( ret );
4913 }
4914 }
4915 else
4916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004918 "message_seq = %d, expected = %d",
4919 recv_msg_seq,
4920 ssl->handshake->in_msg_seq ) );
4921 }
4922
Hanno Becker90333da2017-10-10 11:27:13 +01004923 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004924 }
4925 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004926
Hanno Becker6d97ef52018-08-16 13:09:04 +01004927 /* Message reassembly is handled alongside buffering of future
4928 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004929 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004930 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004931 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004934 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004935 }
4936 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004937 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004938#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004939#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004940 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004941 /* With TLS we don't handle fragmentation (for now) */
4942 if( ssl->in_msglen < ssl->in_hslen )
4943 {
4944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4945 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4946 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004947 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004948#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004949
Simon Butcher99000142016-10-13 17:21:01 +01004950 return( 0 );
4951}
4952
4953void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4954{
Hanno Becker0271f962018-08-16 13:23:47 +01004955 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004956
Hanno Becker0271f962018-08-16 13:23:47 +01004957 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004958 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004959
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004960 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004961#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004962 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004963 ssl->handshake != NULL )
4964 {
Hanno Becker0271f962018-08-16 13:23:47 +01004965 unsigned offset;
4966 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004967
Hanno Becker0271f962018-08-16 13:23:47 +01004968 /* Increment handshake sequence number */
4969 hs->in_msg_seq++;
4970
4971 /*
4972 * Clear up handshake buffering and reassembly structure.
4973 */
4974
4975 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004976 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004977
4978 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004979 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4980 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004981 offset++, hs_buf++ )
4982 {
4983 *hs_buf = *(hs_buf + 1);
4984 }
4985
4986 /* Create a fresh last entry */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004987 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004988 }
4989#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004990}
4991
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004992/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004993 * DTLS anti-replay: RFC 6347 4.1.2.6
4994 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004995 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4996 * Bit n is set iff record number in_window_top - n has been seen.
4997 *
4998 * Usually, in_window_top is the last record number seen and the lsb of
4999 * in_window is set. The only exception is the initial state (record number 0
5000 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005001 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005002#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5003static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005004{
5005 ssl->in_window_top = 0;
5006 ssl->in_window = 0;
5007}
5008
5009static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
5010{
5011 return( ( (uint64_t) buf[0] << 40 ) |
5012 ( (uint64_t) buf[1] << 32 ) |
5013 ( (uint64_t) buf[2] << 24 ) |
5014 ( (uint64_t) buf[3] << 16 ) |
5015 ( (uint64_t) buf[4] << 8 ) |
5016 ( (uint64_t) buf[5] ) );
5017}
5018
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005019static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
5020{
5021 int ret;
5022 unsigned char *original_in_ctr;
5023
5024 // save original in_ctr
5025 original_in_ctr = ssl->in_ctr;
5026
5027 // use counter from record
5028 ssl->in_ctr = record_in_ctr;
5029
5030 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
5031
5032 // restore the counter
5033 ssl->in_ctr = original_in_ctr;
5034
5035 return ret;
5036}
5037
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005038/*
5039 * Return 0 if sequence number is acceptable, -1 otherwise
5040 */
Hanno Beckerfc551722019-07-12 08:50:37 +01005041int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005042{
5043 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5044 uint64_t bit;
5045
Hanno Becker7f376f42019-06-12 16:20:48 +01005046 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5047 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5048 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005049 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01005050 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005051
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005052 if( rec_seqnum > ssl->in_window_top )
5053 return( 0 );
5054
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005055 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005056
5057 if( bit >= 64 )
5058 return( -1 );
5059
5060 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
5061 return( -1 );
5062
5063 return( 0 );
5064}
5065
5066/*
5067 * Update replay window on new validated record
5068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005069void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005070{
5071 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5072
Hanno Becker7f376f42019-06-12 16:20:48 +01005073 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5074 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5075 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005076 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01005077 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005078
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005079 if( rec_seqnum > ssl->in_window_top )
5080 {
5081 /* Update window_top and the contents of the window */
5082 uint64_t shift = rec_seqnum - ssl->in_window_top;
5083
5084 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005085 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005086 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005087 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005088 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005089 ssl->in_window |= 1;
5090 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005091
5092 ssl->in_window_top = rec_seqnum;
5093 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005094 else
5095 {
5096 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005097 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005098
5099 if( bit < 64 ) /* Always true, but be extra sure */
5100 ssl->in_window |= (uint64_t) 1 << bit;
5101 }
5102}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005103#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005104
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005105#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005106/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005107static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
5108
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005109/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005110 * Without any SSL context, check if a datagram looks like a ClientHello with
5111 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01005112 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005113 *
5114 * - if cookie is valid, return 0
5115 * - if ClientHello looks superficially valid but cookie is not,
5116 * fill obuf and set olen, then
5117 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
5118 * - otherwise return a specific error code
5119 */
5120static int ssl_check_dtls_clihlo_cookie(
5121 mbedtls_ssl_cookie_write_t *f_cookie_write,
5122 mbedtls_ssl_cookie_check_t *f_cookie_check,
5123 void *p_cookie,
5124 const unsigned char *cli_id, size_t cli_id_len,
5125 const unsigned char *in, size_t in_len,
5126 unsigned char *obuf, size_t buf_len, size_t *olen )
5127{
5128 size_t sid_len, cookie_len;
5129 unsigned char *p;
5130
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005131 /*
5132 * Structure of ClientHello with record and handshake headers,
5133 * and expected values. We don't need to check a lot, more checks will be
5134 * done when actually parsing the ClientHello - skipping those checks
5135 * avoids code duplication and does not make cookie forging any easier.
5136 *
5137 * 0-0 ContentType type; copied, must be handshake
5138 * 1-2 ProtocolVersion version; copied
5139 * 3-4 uint16 epoch; copied, must be 0
5140 * 5-10 uint48 sequence_number; copied
5141 * 11-12 uint16 length; (ignored)
5142 *
5143 * 13-13 HandshakeType msg_type; (ignored)
5144 * 14-16 uint24 length; (ignored)
5145 * 17-18 uint16 message_seq; copied
5146 * 19-21 uint24 fragment_offset; copied, must be 0
5147 * 22-24 uint24 fragment_length; (ignored)
5148 *
5149 * 25-26 ProtocolVersion client_version; (ignored)
5150 * 27-58 Random random; (ignored)
5151 * 59-xx SessionID session_id; 1 byte len + sid_len content
5152 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5153 * ...
5154 *
5155 * Minimum length is 61 bytes.
5156 */
5157 if( in_len < 61 ||
5158 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5159 in[3] != 0 || in[4] != 0 ||
5160 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5161 {
5162 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5163 }
5164
5165 sid_len = in[59];
5166 if( sid_len > in_len - 61 )
5167 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5168
5169 cookie_len = in[60 + sid_len];
5170 if( cookie_len > in_len - 60 )
5171 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5172
5173 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5174 cli_id, cli_id_len ) == 0 )
5175 {
5176 /* Valid cookie */
5177 return( 0 );
5178 }
5179
5180 /*
5181 * If we get here, we've got an invalid cookie, let's prepare HVR.
5182 *
5183 * 0-0 ContentType type; copied
5184 * 1-2 ProtocolVersion version; copied
5185 * 3-4 uint16 epoch; copied
5186 * 5-10 uint48 sequence_number; copied
5187 * 11-12 uint16 length; olen - 13
5188 *
5189 * 13-13 HandshakeType msg_type; hello_verify_request
5190 * 14-16 uint24 length; olen - 25
5191 * 17-18 uint16 message_seq; copied
5192 * 19-21 uint24 fragment_offset; copied
5193 * 22-24 uint24 fragment_length; olen - 25
5194 *
5195 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5196 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5197 *
5198 * Minimum length is 28.
5199 */
5200 if( buf_len < 28 )
5201 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5202
5203 /* Copy most fields and adapt others */
Teppo Järvelin91d79382019-10-02 09:09:31 +03005204 mbedtls_platform_memcpy( obuf, in, 25 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005205 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5206 obuf[25] = 0xfe;
5207 obuf[26] = 0xff;
5208
5209 /* Generate and write actual cookie */
5210 p = obuf + 28;
5211 if( f_cookie_write( p_cookie,
5212 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5213 {
5214 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5215 }
5216
5217 *olen = p - obuf;
5218
5219 /* Go back and fill length fields */
5220 obuf[27] = (unsigned char)( *olen - 28 );
5221
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005222 (void)mbedtls_platform_put_uint24_be( &obuf[14], ( *olen - 25 ) );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005223 obuf[22] = obuf[14];
5224 obuf[23] = obuf[15];
5225 obuf[24] = obuf[16];
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005226
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005227 (void)mbedtls_platform_put_uint16_be( &obuf[11], ( *olen - 13 ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005228
5229 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5230}
5231
5232/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005233 * Handle possible client reconnect with the same UDP quadruplet
5234 * (RFC 6347 Section 4.2.8).
5235 *
5236 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5237 * that looks like a ClientHello.
5238 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005239 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005240 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005241 * - if the input looks like a ClientHello with a valid cookie,
5242 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005243 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005244 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005245 *
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005246 * This function is called (through ssl_check_client_reconnect()) when an
5247 * unexpected record is found in ssl_get_next_record(), which will discard the
5248 * record if we return 0, and bubble up the return value otherwise (this
5249 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
5250 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005251 */
5252static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5253{
5254 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005255 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005256
Hanno Becker87b56262019-07-10 14:37:41 +01005257 if( ssl->conf->f_cookie_write == NULL ||
5258 ssl->conf->f_cookie_check == NULL )
5259 {
5260 /* If we can't use cookies to verify reachability of the peer,
5261 * drop the record. */
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
5263 "can't check reconnect validity" ) );
Hanno Becker87b56262019-07-10 14:37:41 +01005264 return( 0 );
5265 }
5266
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005267 ret = ssl_check_dtls_clihlo_cookie(
5268 ssl->conf->f_cookie_write,
5269 ssl->conf->f_cookie_check,
5270 ssl->conf->p_cookie,
5271 ssl->cli_id, ssl->cli_id_len,
5272 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005273 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005274
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005275 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5276
5277 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005278 {
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005279 int send_ret;
5280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
5281 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
5282 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08005283 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005284 * If the error is permanent we'll catch it later,
5285 * if it's not, then hopefully it'll work next time. */
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005286 send_ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
5287 MBEDTLS_SSL_DEBUG_RET( 2, "mbedtls_ssl_get_send", send_ret );
5288 (void) send_ret;
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005289 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005290 }
5291
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005292 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005293 {
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005295 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5296 {
5297 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5298 return( ret );
5299 }
5300
5301 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005302 }
5303
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005304 return( ret );
5305}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005306#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005307
Hanno Becker46483f12019-05-03 13:25:54 +01005308static int ssl_check_record_type( uint8_t record_type )
5309{
5310 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5311 record_type != MBEDTLS_SSL_MSG_ALERT &&
5312 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5313 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5314 {
5315 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5316 }
5317
5318 return( 0 );
5319}
5320
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005321/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005322 * ContentType type;
5323 * ProtocolVersion version;
5324 * uint16 epoch; // DTLS only
5325 * uint48 sequence_number; // DTLS only
5326 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005327 *
5328 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005329 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005330 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5331 *
5332 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005333 * 1. proceed with the record if this function returns 0
5334 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5335 * 3. return CLIENT_RECONNECT if this function return that value
5336 * 4. drop the whole datagram if this function returns anything else.
5337 * Point 2 is needed when the peer is resending, and we have already received
5338 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005339 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005340static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005341 unsigned char *buf,
5342 size_t len,
5343 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005344{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005345 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005346
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005347 size_t const rec_hdr_type_offset = 0;
5348 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005349
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005350 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5351 rec_hdr_type_len;
5352 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005353
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005354 size_t const rec_hdr_ctr_len = 8;
5355#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005356 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005357 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5358 rec_hdr_version_len;
5359
Hanno Beckera5a2b082019-05-15 14:03:01 +01005360#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005361 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5362 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005363 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005364#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5365#endif /* MBEDTLS_SSL_PROTO_DTLS */
5366
5367 size_t rec_hdr_len_offset; /* To be determined */
5368 size_t const rec_hdr_len_len = 2;
5369
5370 /*
5371 * Check minimum lengths for record header.
5372 */
5373
5374#if defined(MBEDTLS_SSL_PROTO_DTLS)
5375 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5376 {
5377 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5378 }
5379 MBEDTLS_SSL_TRANSPORT_ELSE
5380#endif /* MBEDTLS_SSL_PROTO_DTLS */
5381#if defined(MBEDTLS_SSL_PROTO_TLS)
5382 {
5383 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5384 }
5385#endif /* MBEDTLS_SSL_PROTO_DTLS */
5386
5387 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5388 {
5389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5390 (unsigned) len,
5391 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5392 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5393 }
5394
5395 /*
5396 * Parse and validate record content type
5397 */
5398
5399 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005400
5401 /* Check record content type */
5402#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5403 rec->cid_len = 0;
5404
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005405 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005406 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5407 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005408 {
5409 /* Shift pointers to account for record header including CID
5410 * struct {
5411 * ContentType special_type = tls12_cid;
5412 * ProtocolVersion version;
5413 * uint16 epoch;
5414 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005415 * opaque cid[cid_length]; // Additional field compared to
5416 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005417 * uint16 length;
5418 * opaque enc_content[DTLSCiphertext.length];
5419 * } DTLSCiphertext;
5420 */
5421
5422 /* So far, we only support static CID lengths
5423 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005424 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5425 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005426
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005427 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005428 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005429 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5430 (unsigned) len,
5431 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005432 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005433 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005434
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005435 /* configured CID len is guaranteed at most 255, see
5436 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5437 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005438 /* Not using more secure mbedtls_platform_memcpy as cid is public */
5439 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005440 }
5441 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005442#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005443 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005444 if( ssl_check_record_type( rec->type ) )
5445 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5447 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005448 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5449 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005450 }
5451
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005452 /*
5453 * Parse and validate record version
5454 */
5455
Hanno Becker8061c6e2019-07-26 08:07:03 +01005456 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5457 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005458 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5459 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005460 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005461
Hanno Becker2881d802019-05-22 14:44:53 +01005462 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5465 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005466 }
5467
Hanno Becker7bcf2b52019-07-26 09:02:40 +01005468 if( mbedtls_ssl_ver_gt( minor_ver,
5469 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5472 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005473 }
5474
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005475 /*
5476 * Parse/Copy record sequence number.
5477 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005478
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005479#if defined(MBEDTLS_SSL_PROTO_DTLS)
5480 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5481 {
5482 /* Copy explicit record sequence number from input buffer. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005483 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5484 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005485 rec_hdr_ctr_len );
5486 }
5487 MBEDTLS_SSL_TRANSPORT_ELSE
5488#endif /* MBEDTLS_SSL_PROTO_DTLS */
5489#if defined(MBEDTLS_SSL_PROTO_TLS)
5490 {
5491 /* Copy implicit record sequence number from SSL context structure. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005492 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5493 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005494 }
5495#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005496
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005497 /*
5498 * Parse record length.
5499 */
5500
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005501 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005502 rec->data_len = mbedtls_platform_get_uint16_be( &buf[rec_hdr_len_offset] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005503 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5504
Hanno Becker8b09b732019-05-08 12:03:28 +01005505 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005506 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005507 rec->type,
5508 major_ver, minor_ver, rec->data_len ) );
5509
5510 rec->buf = buf;
5511 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005512
Hanno Beckerec014082019-07-26 08:20:27 +01005513 if( rec->data_len == 0 )
5514 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5515
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005516 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005517 * DTLS-related tests.
5518 * Check epoch before checking length constraint because
5519 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5520 * message gets duplicated before the corresponding Finished message,
5521 * the second ChangeCipherSpec should be discarded because it belongs
5522 * to an old epoch, but not because its length is shorter than
5523 * the minimum record length for packets using the new record transform.
5524 * Note that these two kinds of failures are handled differently,
5525 * as an unexpected record is silently skipped but an invalid
5526 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005527 */
5528#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005529 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005530 {
Arto Kinnunena3fa06e2019-09-09 12:22:51 +03005531 rec_epoch = (uint32_t)mbedtls_platform_get_uint16_be( rec->ctr );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005532
Hanno Beckere0452772019-07-10 17:12:07 +01005533 /* Check that the datagram is large enough to contain a record
5534 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005535 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005536 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5538 (unsigned) len,
5539 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005540 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5541 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005542
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005543 /* Records from other, non-matching epochs are silently discarded.
5544 * (The case of same-port Client reconnects must be considered in
5545 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005546 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005547 {
5548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5549 "expected %d, received %d",
5550 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005551
5552 /* Records from the next epoch are considered for buffering
5553 * (concretely: early Finished messages). */
5554 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5555 {
5556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5557 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5558 }
5559
Hanno Becker87b56262019-07-10 14:37:41 +01005560 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005561 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005562#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005563 /* For records from the correct epoch, check whether their
5564 * sequence number has been seen before. */
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005565 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
5566 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005567 {
5568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5569 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5570 }
5571#endif
5572 }
5573#endif /* MBEDTLS_SSL_PROTO_DTLS */
5574
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005575 return( 0 );
5576}
Paul Bakker5121ce52009-01-03 21:22:43 +00005577
Hanno Becker87b56262019-07-10 14:37:41 +01005578
5579#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5580static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5581{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005582 unsigned int rec_epoch = (unsigned int)
5583 mbedtls_platform_get_uint16_be( &ssl->in_ctr[0] );
Hanno Becker87b56262019-07-10 14:37:41 +01005584
5585 /*
5586 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5587 * access the first byte of record content (handshake type), as we
5588 * have an active transform (possibly iv_len != 0), so use the
5589 * fact that the record header len is 13 instead.
5590 */
5591 if( rec_epoch == 0 &&
5592 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5593 MBEDTLS_SSL_IS_SERVER &&
5594 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5595 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5596 ssl->in_left > 13 &&
5597 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5598 {
5599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5600 "from the same port" ) );
5601 return( ssl_handle_possible_reconnect( ssl ) );
5602 }
5603
5604 return( 0 );
5605}
5606#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5607
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005608/*
Manuel Pégourié-Gonnardae48d862020-01-03 12:18:49 +01005609 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005610 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005611static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5612 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005613{
5614 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005617 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005619#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5620 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005624 ret = mbedtls_ssl_hw_record_read( ssl );
5625 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5628 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005629 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005630
5631 if( ret == 0 )
5632 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005633 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005634#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005635 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005636 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005637 unsigned char const old_msg_type = rec->type;
5638
Hanno Becker611a83b2018-01-03 14:27:32 +00005639 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005640 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005642 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005643
Hanno Beckera5a2b082019-05-15 14:03:01 +01005644#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005645 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005646 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5647 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005648 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005649 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005650 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005651 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005652#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005653
Paul Bakker5121ce52009-01-03 21:22:43 +00005654 return( ret );
5655 }
5656
Hanno Becker106f3da2019-07-12 09:35:58 +01005657 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005658 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005659 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005660 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005661 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005662
Paul Bakker5121ce52009-01-03 21:22:43 +00005663 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005664 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005665
Hanno Beckera5a2b082019-05-15 14:03:01 +01005666#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005667 /* We have already checked the record content type
5668 * in ssl_parse_record_header(), failing or silently
5669 * dropping the record in the case of an unknown type.
5670 *
5671 * Since with the use of CIDs, the record content type
5672 * might change during decryption, re-check the record
5673 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005674 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005675 {
5676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5677 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5678 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005679#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005680
Hanno Becker106f3da2019-07-12 09:35:58 +01005681 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005682 {
5683#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005684 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005685 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005686 {
5687 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5689 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5690 }
5691#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5692
5693 ssl->nb_zero++;
5694
5695 /*
5696 * Three or more empty messages may be a DoS attack
5697 * (excessive CPU consumption).
5698 */
5699 if( ssl->nb_zero > 3 )
5700 {
5701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005702 "messages, possible DoS attack" ) );
5703 /* Treat the records as if they were not properly authenticated,
5704 * thereby failing the connection if we see more than allowed
5705 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005706 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5707 }
5708 }
5709 else
5710 ssl->nb_zero = 0;
5711
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005712 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5713#if defined(MBEDTLS_SSL_PROTO_TLS)
5714 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005715 {
5716 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005717 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005718 if( ++ssl->in_ctr[i - 1] != 0 )
5719 break;
5720
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005721 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005722 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005723 {
5724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5725 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5726 }
5727 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005728#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005729
Paul Bakker5121ce52009-01-03 21:22:43 +00005730 }
5731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005733 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005735 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005736 }
5737#endif
5738
Hanno Beckerf0242852019-07-09 17:30:02 +01005739 /* Check actual (decrypted) record content length against
5740 * configured maximum. */
5741 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5742 {
5743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5744 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5745 }
5746
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005747 return( 0 );
5748}
5749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005750static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005751
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005752/*
5753 * Read a record.
5754 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005755 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5756 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5757 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005758 */
Hanno Becker1097b342018-08-15 14:09:41 +01005759
5760/* Helper functions for mbedtls_ssl_read_record(). */
5761static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005762static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5763static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005764
Hanno Becker327c93b2018-08-15 13:56:18 +01005765int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005766 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005767{
5768 int ret;
5769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005770 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005771
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005772 if( ssl->keep_current_message == 0 )
5773 {
5774 do {
Simon Butcher99000142016-10-13 17:21:01 +01005775
Hanno Becker26994592018-08-15 14:14:59 +01005776 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005777 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005778 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005779
Hanno Beckere74d5562018-08-15 14:26:08 +01005780 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005781 {
Hanno Becker40f50842018-08-15 14:48:01 +01005782#if defined(MBEDTLS_SSL_PROTO_DTLS)
5783 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005784
Hanno Becker40f50842018-08-15 14:48:01 +01005785 /* We only check for buffered messages if the
5786 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005787 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005788 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005789 {
Hanno Becker40f50842018-08-15 14:48:01 +01005790 if( ssl_load_buffered_message( ssl ) == 0 )
5791 have_buffered = 1;
5792 }
5793
5794 if( have_buffered == 0 )
5795#endif /* MBEDTLS_SSL_PROTO_DTLS */
5796 {
5797 ret = ssl_get_next_record( ssl );
5798 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5799 continue;
5800
5801 if( ret != 0 )
5802 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005803 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005804 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005805 return( ret );
5806 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005807 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005808 }
5809
5810 ret = mbedtls_ssl_handle_message_type( ssl );
5811
Hanno Becker40f50842018-08-15 14:48:01 +01005812#if defined(MBEDTLS_SSL_PROTO_DTLS)
5813 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5814 {
5815 /* Buffer future message */
5816 ret = ssl_buffer_message( ssl );
5817 if( ret != 0 )
5818 return( ret );
5819
5820 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5821 }
5822#endif /* MBEDTLS_SSL_PROTO_DTLS */
5823
Hanno Becker90333da2017-10-10 11:27:13 +01005824 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5825 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005826
5827 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005828 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005829 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005830 return( ret );
5831 }
5832
Hanno Becker327c93b2018-08-15 13:56:18 +01005833 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005834 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005835 {
5836 mbedtls_ssl_update_handshake_status( ssl );
5837 }
Simon Butcher99000142016-10-13 17:21:01 +01005838 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005839 else
Simon Butcher99000142016-10-13 17:21:01 +01005840 {
Hanno Becker02f59072018-08-15 14:00:24 +01005841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005842 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005843 }
5844
5845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5846
5847 return( 0 );
5848}
5849
Hanno Becker40f50842018-08-15 14:48:01 +01005850#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005851static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005852{
Hanno Becker40f50842018-08-15 14:48:01 +01005853 if( ssl->in_left > ssl->next_record_offset )
5854 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005855
Hanno Becker40f50842018-08-15 14:48:01 +01005856 return( 0 );
5857}
5858
5859static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5860{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005861 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005862 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005863 int ret = 0;
5864
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005865 if( hs == NULL )
5866 return( -1 );
5867
Hanno Beckere00ae372018-08-20 09:39:42 +01005868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5869
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005870 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5871 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5872 {
5873 /* Check if we have seen a ChangeCipherSpec before.
5874 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005875 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005876 {
5877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5878 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005879 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005880 }
5881
Hanno Becker39b8bc92018-08-28 17:17:13 +01005882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005883 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5884 ssl->in_msglen = 1;
5885 ssl->in_msg[0] = 1;
5886
5887 /* As long as they are equal, the exact value doesn't matter. */
5888 ssl->in_left = 0;
5889 ssl->next_record_offset = 0;
5890
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005891 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005892 goto exit;
5893 }
Hanno Becker37f95322018-08-16 13:55:32 +01005894
Hanno Beckerb8f50142018-08-28 10:01:34 +01005895#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005896 /* Debug only */
5897 {
5898 unsigned offset;
5899 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5900 {
5901 hs_buf = &hs->buffering.hs[offset];
5902 if( hs_buf->is_valid == 1 )
5903 {
5904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5905 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005906 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005907 }
5908 }
5909 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005910#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005911
5912 /* Check if we have buffered and/or fully reassembled the
5913 * next handshake message. */
5914 hs_buf = &hs->buffering.hs[0];
5915 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5916 {
5917 /* Synthesize a record containing the buffered HS message. */
Arto Kinnunen84eeb4f2019-09-10 10:32:30 +03005918 size_t msg_len = mbedtls_platform_get_uint24_be( &hs_buf->data[1] );
Hanno Becker37f95322018-08-16 13:55:32 +01005919
5920 /* Double-check that we haven't accidentally buffered
5921 * a message that doesn't fit into the input buffer. */
5922 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5923 {
5924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5925 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5926 }
5927
5928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5929 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5930 hs_buf->data, msg_len + 12 );
5931
5932 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5933 ssl->in_hslen = msg_len + 12;
5934 ssl->in_msglen = msg_len + 12;
Teppo Järvelin91d79382019-10-02 09:09:31 +03005935 mbedtls_platform_memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
Hanno Becker37f95322018-08-16 13:55:32 +01005936
5937 ret = 0;
5938 goto exit;
5939 }
5940 else
5941 {
5942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5943 hs->in_msg_seq ) );
5944 }
5945
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005946 ret = -1;
5947
5948exit:
5949
5950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5951 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005952}
5953
Hanno Beckera02b0b42018-08-21 17:20:27 +01005954static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5955 size_t desired )
5956{
5957 int offset;
5958 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5960 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005961
Hanno Becker01315ea2018-08-21 17:22:17 +01005962 /* Get rid of future records epoch first, if such exist. */
5963 ssl_free_buffered_record( ssl );
5964
5965 /* Check if we have enough space available now. */
5966 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5967 hs->buffering.total_bytes_buffered ) )
5968 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005970 return( 0 );
5971 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005972
Hanno Becker4f432ad2018-08-28 10:02:32 +01005973 /* We don't have enough space to buffer the next expected handshake
5974 * message. Remove buffers used for future messages to gain space,
5975 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005976 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5977 offset >= 0; offset-- )
5978 {
5979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5980 offset ) );
5981
Hanno Beckerb309b922018-08-23 13:18:05 +01005982 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005983
5984 /* Check if we have enough space available now. */
5985 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5986 hs->buffering.total_bytes_buffered ) )
5987 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005989 return( 0 );
5990 }
5991 }
5992
5993 return( -1 );
5994}
5995
Hanno Becker40f50842018-08-15 14:48:01 +01005996static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5997{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005998 int ret = 0;
5999 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6000
6001 if( hs == NULL )
6002 return( 0 );
6003
6004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
6005
6006 switch( ssl->in_msgtype )
6007 {
6008 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
6009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01006010
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01006011 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006012 break;
6013
6014 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01006015 {
6016 unsigned recv_msg_seq_offset;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03006017 unsigned recv_msg_seq = (unsigned)
6018 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006019
Hanno Becker37f95322018-08-16 13:55:32 +01006020 mbedtls_ssl_hs_buffer *hs_buf;
6021 size_t msg_len = ssl->in_hslen - 12;
6022
6023 /* We should never receive an old handshake
6024 * message - double-check nonetheless. */
6025 if( recv_msg_seq < ssl->handshake->in_msg_seq )
6026 {
6027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6028 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6029 }
6030
6031 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
6032 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
6033 {
6034 /* Silently ignore -- message too far in the future */
6035 MBEDTLS_SSL_DEBUG_MSG( 2,
6036 ( "Ignore future HS message with sequence number %u, "
6037 "buffering window %u - %u",
6038 recv_msg_seq, ssl->handshake->in_msg_seq,
6039 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
6040
6041 goto exit;
6042 }
6043
6044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
6045 recv_msg_seq, recv_msg_seq_offset ) );
6046
6047 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
6048
6049 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01006050 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01006051 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006052 size_t reassembly_buf_sz;
6053
Hanno Becker37f95322018-08-16 13:55:32 +01006054 hs_buf->is_fragmented =
6055 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
6056
6057 /* We copy the message back into the input buffer
6058 * after reassembly, so check that it's not too large.
6059 * This is an implementation-specific limitation
6060 * and not one from the standard, hence it is not
6061 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01006062 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01006063 {
6064 /* Ignore message */
6065 goto exit;
6066 }
6067
Hanno Beckere0b150f2018-08-21 15:51:03 +01006068 /* Check if we have enough space to buffer the message. */
6069 if( hs->buffering.total_bytes_buffered >
6070 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
6071 {
6072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6073 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6074 }
6075
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006076 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
6077 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01006078
6079 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
6080 hs->buffering.total_bytes_buffered ) )
6081 {
6082 if( recv_msg_seq_offset > 0 )
6083 {
6084 /* If we can't buffer a future message because
6085 * of space limitations -- ignore. */
6086 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",
6087 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6088 (unsigned) hs->buffering.total_bytes_buffered ) );
6089 goto exit;
6090 }
Hanno Beckere1801392018-08-21 16:51:05 +01006091 else
6092 {
6093 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",
6094 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6095 (unsigned) hs->buffering.total_bytes_buffered ) );
6096 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006097
Hanno Beckera02b0b42018-08-21 17:20:27 +01006098 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006099 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01006100 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",
6101 (unsigned) msg_len,
6102 (unsigned) reassembly_buf_sz,
6103 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01006104 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006105 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6106 goto exit;
6107 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006108 }
6109
6110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
6111 msg_len ) );
6112
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006113 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
6114 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01006115 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01006116 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01006117 goto exit;
6118 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006119 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006120
6121 /* Prepare final header: copy msg_type, length and message_seq,
6122 * then add standardised fragment_offset and fragment_length */
Teppo Järvelin91d79382019-10-02 09:09:31 +03006123 mbedtls_platform_memcpy( hs_buf->data, ssl->in_msg, 6 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02006124 mbedtls_platform_memset( hs_buf->data + 6, 0, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006125 mbedtls_platform_memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
Hanno Becker37f95322018-08-16 13:55:32 +01006126
6127 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01006128
6129 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006130 }
6131 else
6132 {
6133 /* Make sure msg_type and length are consistent */
Teppo Järvelin0efac532019-10-04 13:21:08 +03006134 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 +01006135 {
6136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6137 /* Ignore */
6138 goto exit;
6139 }
6140 }
6141
Hanno Becker4422bbb2018-08-20 09:40:19 +01006142 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006143 {
6144 size_t frag_len, frag_off;
6145 unsigned char * const msg = hs_buf->data + 12;
6146
6147 /*
6148 * Check and copy current fragment
6149 */
6150
6151 /* Validation of header fields already done in
6152 * mbedtls_ssl_prepare_handshake_record(). */
6153 frag_off = ssl_get_hs_frag_off( ssl );
6154 frag_len = ssl_get_hs_frag_len( ssl );
6155
6156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6157 frag_off, frag_len ) );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006158 mbedtls_platform_memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
Hanno Becker37f95322018-08-16 13:55:32 +01006159
6160 if( hs_buf->is_fragmented )
6161 {
6162 unsigned char * const bitmask = msg + msg_len;
6163 ssl_bitmask_set( bitmask, frag_off, frag_len );
6164 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6165 msg_len ) == 0 );
6166 }
6167 else
6168 {
6169 hs_buf->is_complete = 1;
6170 }
6171
6172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6173 hs_buf->is_complete ? "" : "not yet " ) );
6174 }
6175
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006176 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006177 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006178
6179 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006180 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006181 break;
6182 }
6183
6184exit:
6185
6186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6187 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006188}
6189#endif /* MBEDTLS_SSL_PROTO_DTLS */
6190
Hanno Becker1097b342018-08-15 14:09:41 +01006191static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006192{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006193 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006194 * Consume last content-layer message and potentially
6195 * update in_msglen which keeps track of the contents'
6196 * consumption state.
6197 *
6198 * (1) Handshake messages:
6199 * Remove last handshake message, move content
6200 * and adapt in_msglen.
6201 *
6202 * (2) Alert messages:
6203 * Consume whole record content, in_msglen = 0.
6204 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006205 * (3) Change cipher spec:
6206 * Consume whole record content, in_msglen = 0.
6207 *
6208 * (4) Application data:
6209 * Don't do anything - the record layer provides
6210 * the application data as a stream transport
6211 * and consumes through mbedtls_ssl_read only.
6212 *
6213 */
6214
6215 /* Case (1): Handshake messages */
6216 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006217 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006218 /* Hard assertion to be sure that no application data
6219 * is in flight, as corrupting ssl->in_msglen during
6220 * ssl->in_offt != NULL is fatal. */
6221 if( ssl->in_offt != NULL )
6222 {
6223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6224 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6225 }
6226
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006227 /*
6228 * Get next Handshake message in the current record
6229 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006230
Hanno Becker4a810fb2017-05-24 16:27:30 +01006231 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006232 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006233 * current handshake content: If DTLS handshake
6234 * fragmentation is used, that's the fragment
6235 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006236 * size here is faulty and should be changed at
6237 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006238 * (2) While it doesn't seem to cause problems, one
6239 * has to be very careful not to assume that in_hslen
6240 * is always <= in_msglen in a sensible communication.
6241 * Again, it's wrong for DTLS handshake fragmentation.
6242 * The following check is therefore mandatory, and
6243 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006244 * Additionally, ssl->in_hslen might be arbitrarily out of
6245 * bounds after handling a DTLS message with an unexpected
6246 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006247 */
6248 if( ssl->in_hslen < ssl->in_msglen )
6249 {
6250 ssl->in_msglen -= ssl->in_hslen;
6251 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6252 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006253
Hanno Becker4a810fb2017-05-24 16:27:30 +01006254 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6255 ssl->in_msg, ssl->in_msglen );
6256 }
6257 else
6258 {
6259 ssl->in_msglen = 0;
6260 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006261
Hanno Becker4a810fb2017-05-24 16:27:30 +01006262 ssl->in_hslen = 0;
6263 }
6264 /* Case (4): Application data */
6265 else if( ssl->in_offt != NULL )
6266 {
6267 return( 0 );
6268 }
6269 /* Everything else (CCS & Alerts) */
6270 else
6271 {
6272 ssl->in_msglen = 0;
6273 }
6274
Hanno Becker1097b342018-08-15 14:09:41 +01006275 return( 0 );
6276}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006277
Hanno Beckere74d5562018-08-15 14:26:08 +01006278static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6279{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006280 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006281 return( 1 );
6282
6283 return( 0 );
6284}
6285
Hanno Becker5f066e72018-08-16 14:56:31 +01006286#if defined(MBEDTLS_SSL_PROTO_DTLS)
6287
6288static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6289{
6290 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6291 if( hs == NULL )
6292 return;
6293
Hanno Becker01315ea2018-08-21 17:22:17 +01006294 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006295 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006296 hs->buffering.total_bytes_buffered -=
6297 hs->buffering.future_record.len;
6298
6299 mbedtls_free( hs->buffering.future_record.data );
6300 hs->buffering.future_record.data = NULL;
6301 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006302}
6303
6304static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6305{
6306 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6307 unsigned char * rec;
6308 size_t rec_len;
6309 unsigned rec_epoch;
6310
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006311 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006312 return( 0 );
6313
6314 if( hs == NULL )
6315 return( 0 );
6316
Hanno Becker5f066e72018-08-16 14:56:31 +01006317 rec = hs->buffering.future_record.data;
6318 rec_len = hs->buffering.future_record.len;
6319 rec_epoch = hs->buffering.future_record.epoch;
6320
6321 if( rec == NULL )
6322 return( 0 );
6323
Hanno Becker4cb782d2018-08-20 11:19:05 +01006324 /* Only consider loading future records if the
6325 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006326 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006327 return( 0 );
6328
Hanno Becker5f066e72018-08-16 14:56:31 +01006329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6330
6331 if( rec_epoch != ssl->in_epoch )
6332 {
6333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6334 goto exit;
6335 }
6336
6337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6338
6339 /* Double-check that the record is not too large */
6340 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6341 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6342 {
6343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6344 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6345 }
6346
Teppo Järvelin91d79382019-10-02 09:09:31 +03006347 mbedtls_platform_memcpy( ssl->in_hdr, rec, rec_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006348 ssl->in_left = rec_len;
6349 ssl->next_record_offset = 0;
6350
6351 ssl_free_buffered_record( ssl );
6352
6353exit:
6354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6355 return( 0 );
6356}
6357
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006358static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6359 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006360{
6361 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006362
6363 /* Don't buffer future records outside handshakes. */
6364 if( hs == NULL )
6365 return( 0 );
6366
6367 /* Only buffer handshake records (we are only interested
6368 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006369 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006370 return( 0 );
6371
6372 /* Don't buffer more than one future epoch record. */
6373 if( hs->buffering.future_record.data != NULL )
6374 return( 0 );
6375
Hanno Becker01315ea2018-08-21 17:22:17 +01006376 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006377 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006378 hs->buffering.total_bytes_buffered ) )
6379 {
6380 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 +01006381 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006382 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006383 return( 0 );
6384 }
6385
Hanno Becker5f066e72018-08-16 14:56:31 +01006386 /* Buffer record */
6387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6388 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006389 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006390
6391 /* ssl_parse_record_header() only considers records
6392 * of the next epoch as candidates for buffering. */
6393 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006394 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006395
6396 hs->buffering.future_record.data =
6397 mbedtls_calloc( 1, hs->buffering.future_record.len );
6398 if( hs->buffering.future_record.data == NULL )
6399 {
6400 /* If we run out of RAM trying to buffer a
6401 * record from the next epoch, just ignore. */
6402 return( 0 );
6403 }
6404
Teppo Järvelin91d79382019-10-02 09:09:31 +03006405 mbedtls_platform_memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006406
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006407 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006408 return( 0 );
6409}
6410
6411#endif /* MBEDTLS_SSL_PROTO_DTLS */
6412
Hanno Beckere74d5562018-08-15 14:26:08 +01006413static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006414{
6415 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006416 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006417
Hanno Becker5f066e72018-08-16 14:56:31 +01006418#if defined(MBEDTLS_SSL_PROTO_DTLS)
6419 /* We might have buffered a future record; if so,
6420 * and if the epoch matches now, load it.
6421 * On success, this call will set ssl->in_left to
6422 * the length of the buffered record, so that
6423 * the calls to ssl_fetch_input() below will
6424 * essentially be no-ops. */
6425 ret = ssl_load_buffered_record( ssl );
6426 if( ret != 0 )
6427 return( ret );
6428#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006429
Hanno Becker8b09b732019-05-08 12:03:28 +01006430 /* Ensure that we have enough space available for the default form
6431 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6432 * with no space for CIDs counted in). */
6433 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6434 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006437 return( ret );
6438 }
6439
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006440 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6441 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006443#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006444 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006445 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006446 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6447 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006448 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006449 if( ret != 0 )
6450 return( ret );
6451
6452 /* Fall through to handling of unexpected records */
6453 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6454 }
6455
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006456 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6457 {
Hanno Becker87b56262019-07-10 14:37:41 +01006458#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006459 /* Reset in pointers to default state for TLS/DTLS records,
6460 * assuming no CID and no offset between record content and
6461 * record plaintext. */
6462 ssl_update_in_pointers( ssl );
6463
Hanno Becker69412452019-07-12 08:33:49 +01006464 /* Setup internal message pointers from record structure. */
6465 ssl->in_msgtype = rec.type;
6466#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6467 ssl->in_len = ssl->in_cid + rec.cid_len;
6468#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006469 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006470 ssl->in_msglen = rec.data_len;
6471
Hanno Becker87b56262019-07-10 14:37:41 +01006472 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02006473 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker87b56262019-07-10 14:37:41 +01006474 if( ret != 0 )
6475 return( ret );
6476#endif
6477
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006478 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006479 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006480
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6482 "(header)" ) );
6483 }
6484 else
6485 {
6486 /* Skip invalid record and the rest of the datagram */
6487 ssl->next_record_offset = 0;
6488 ssl->in_left = 0;
6489
6490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6491 "(header)" ) );
6492 }
6493
6494 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006495 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006496 }
Hanno Becker87b56262019-07-10 14:37:41 +01006497 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006498#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006499 {
6500 return( ret );
6501 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006502 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006504#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006505 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006506 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006507 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006508 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006509 if( ssl->next_record_offset < ssl->in_left )
6510 {
6511 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6512 }
6513 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006514 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006515#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006516#if defined(MBEDTLS_SSL_PROTO_TLS)
6517 {
Hanno Beckere0452772019-07-10 17:12:07 +01006518 /*
6519 * Fetch record contents from underlying transport.
6520 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006521 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006522 if( ret != 0 )
6523 {
6524 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6525 return( ret );
6526 }
6527
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006528 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006529 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006530#endif /* MBEDTLS_SSL_PROTO_TLS */
6531
6532 /*
6533 * Decrypt record contents.
6534 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006535
Hanno Beckera89610a2019-07-11 13:07:45 +01006536 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006538#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006539 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006540 {
6541 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006542 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006543 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006544 /* Except when waiting for Finished as a bad mac here
6545 * probably means something went wrong in the handshake
6546 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6547 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6548 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6549 {
6550#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6551 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6552 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006553 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006554 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6555 }
6556#endif
6557 return( ret );
6558 }
6559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006560#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006561 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6562 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6565 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006566 }
6567#endif
6568
Hanno Becker4a810fb2017-05-24 16:27:30 +01006569 /* As above, invalid records cause
6570 * dismissal of the whole datagram. */
6571
6572 ssl->next_record_offset = 0;
6573 ssl->in_left = 0;
6574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006576 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006577 }
6578
6579 return( ret );
6580 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006581 MBEDTLS_SSL_TRANSPORT_ELSE
6582#endif /* MBEDTLS_SSL_PROTO_DTLS */
6583#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006584 {
6585 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6587 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006588 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006589 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006590 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006591 }
6592#endif
6593 return( ret );
6594 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006595#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006596 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006597
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006598
6599 /* Reset in pointers to default state for TLS/DTLS records,
6600 * assuming no CID and no offset between record content and
6601 * record plaintext. */
6602 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006603#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6604 ssl->in_len = ssl->in_cid + rec.cid_len;
6605#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006606 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006607
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006608 /* The record content type may change during decryption,
6609 * so re-read it. */
6610 ssl->in_msgtype = rec.type;
6611 /* Also update the input buffer, because unfortunately
6612 * the server-side ssl_parse_client_hello() reparses the
6613 * record header when receiving a ClientHello initiating
6614 * a renegotiation. */
6615 ssl->in_hdr[0] = rec.type;
6616 ssl->in_msg = rec.buf + rec.data_offset;
6617 ssl->in_msglen = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006618 (void)mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len );
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006619
Manuel Pégourié-Gonnardae48d862020-01-03 12:18:49 +01006620#if defined(MBEDTLS_ZLIB_SUPPORT)
6621 if( ssl->transform_in != NULL &&
6622 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
6623 {
6624 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
6625 {
6626 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
6627 return( ret );
6628 }
6629
6630 /* Check actual (decompress) record content length against
6631 * configured maximum. */
6632 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
6633 {
6634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
6635 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6636 }
6637 }
6638#endif /* MBEDTLS_ZLIB_SUPPORT */
6639
Simon Butcher99000142016-10-13 17:21:01 +01006640 return( 0 );
6641}
6642
6643int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6644{
6645 int ret;
6646
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006647 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006648 * Handle particular types of records
6649 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006650 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006651 {
Simon Butcher99000142016-10-13 17:21:01 +01006652 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6653 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006654 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006655 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006656 }
6657
Hanno Beckere678eaa2018-08-21 14:57:46 +01006658 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006659 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006660 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006661 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6663 ssl->in_msglen ) );
6664 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006665 }
6666
Hanno Beckere678eaa2018-08-21 14:57:46 +01006667 if( ssl->in_msg[0] != 1 )
6668 {
6669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6670 ssl->in_msg[0] ) );
6671 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6672 }
6673
6674#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006675 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006676 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6677 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6678 {
6679 if( ssl->handshake == NULL )
6680 {
6681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6682 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6683 }
6684
6685 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6686 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6687 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006688#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006689 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006692 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006693 if( ssl->in_msglen != 2 )
6694 {
6695 /* Note: Standard allows for more than one 2 byte alert
6696 to be packed in a single message, but Mbed TLS doesn't
6697 currently support this. */
6698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6699 ssl->in_msglen ) );
6700 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6701 }
6702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006704 ssl->in_msg[0], ssl->in_msg[1] ) );
6705
6706 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006707 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006709 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006712 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006714 }
6715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006716 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6717 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6720 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006721 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006722
6723#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6724 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6725 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6726 {
Hanno Becker90333da2017-10-10 11:27:13 +01006727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006728 /* Will be handled when trying to parse ServerHello */
6729 return( 0 );
6730 }
6731#endif
6732
6733#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006734 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006735 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6736 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006737 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6738 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6739 {
6740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6741 /* Will be handled in mbedtls_ssl_parse_certificate() */
6742 return( 0 );
6743 }
6744#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6745
6746 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006747 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006748 }
6749
Hanno Beckerc76c6192017-06-06 10:03:17 +01006750#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006751 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006752 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006753 /* Drop unexpected ApplicationData records,
6754 * except at the beginning of renegotiations */
6755 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6756 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6757#if defined(MBEDTLS_SSL_RENEGOTIATION)
6758 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6759 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006760#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006761 )
6762 {
6763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6764 return( MBEDTLS_ERR_SSL_NON_FATAL );
6765 }
6766
6767 if( ssl->handshake != NULL &&
6768 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6769 {
6770 ssl_handshake_wrapup_free_hs_transform( ssl );
6771 }
6772 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006773#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006774
Paul Bakker5121ce52009-01-03 21:22:43 +00006775 return( 0 );
6776}
6777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006779{
6780 int ret;
6781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6783 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6784 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006785 {
6786 return( ret );
6787 }
6788
6789 return( 0 );
6790}
6791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006792int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006793 unsigned char level,
6794 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006795{
6796 int ret;
6797
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006798 if( ssl == NULL || ssl->conf == NULL )
6799 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006802 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006804 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006805 ssl->out_msglen = 2;
6806 ssl->out_msg[0] = level;
6807 ssl->out_msg[1] = message;
6808
Hanno Becker67bc7c32018-08-06 11:33:50 +01006809 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006811 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006812 return( ret );
6813 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006815
6816 return( 0 );
6817}
6818
Hanno Becker17572472019-02-08 07:19:04 +00006819#if defined(MBEDTLS_X509_CRT_PARSE_C)
6820static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6821{
6822#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6823 if( session->peer_cert != NULL )
6824 {
6825 mbedtls_x509_crt_free( session->peer_cert );
6826 mbedtls_free( session->peer_cert );
6827 session->peer_cert = NULL;
6828 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006829#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006830 if( session->peer_cert_digest != NULL )
6831 {
6832 /* Zeroization is not necessary. */
6833 mbedtls_free( session->peer_cert_digest );
6834 session->peer_cert_digest = NULL;
6835 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6836 session->peer_cert_digest_len = 0;
6837 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006838#else
6839 ((void) session);
6840#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006841}
6842#endif /* MBEDTLS_X509_CRT_PARSE_C */
6843
Paul Bakker5121ce52009-01-03 21:22:43 +00006844/*
6845 * Handshake functions
6846 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006847#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006848/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006849int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006850{
Hanno Beckerdf645962019-06-26 13:02:22 +01006851 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6852 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006855
Hanno Becker5097cba2019-02-05 13:36:46 +00006856 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006859 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6860 {
6861 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6862 }
6863 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6864 {
6865 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6866 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006867 else
6868 {
6869 ssl->state = MBEDTLS_SSL_INVALID;
6870 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006871 return( 0 );
6872 }
6873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6875 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006876}
6877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006878int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006879{
Hanno Beckerdf645962019-06-26 13:02:22 +01006880 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6881 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006884
Hanno Becker5097cba2019-02-05 13:36:46 +00006885 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006888 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6889 {
6890 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6891 }
6892 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6893 {
6894 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6895 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006896 else
6897 {
6898 ssl->state = MBEDTLS_SSL_INVALID;
6899 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006900 return( 0 );
6901 }
6902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6904 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006905}
Gilles Peskinef9828522017-05-03 12:28:43 +02006906
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006907#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006908/* Some certificate support -> implement write and parse */
6909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006911{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006913 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006914 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006915 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6916 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006919
Hanno Becker5097cba2019-02-05 13:36:46 +00006920 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006923 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6924 {
6925 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6926 }
6927 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6928 {
6929 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6930 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006931 else
6932 {
6933 ssl->state = MBEDTLS_SSL_INVALID;
6934 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006935 return( 0 );
6936 }
6937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006938#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006939 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6940 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006941 {
6942 if( ssl->client_auth == 0 )
6943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006945 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6946 {
6947 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6948 }
6949 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6950 {
6951 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6952 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006953 else
6954 {
6955 ssl->state = MBEDTLS_SSL_INVALID;
6956 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006957 return( 0 );
6958 }
6959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006960#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006961 /*
6962 * If using SSLv3 and got no cert, send an Alert message
6963 * (otherwise an empty Certificate message will be sent).
6964 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006966 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006967 {
6968 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006969 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6970 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6971 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006974 goto write_msg;
6975 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006976#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006977 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978#endif /* MBEDTLS_SSL_CLI_C */
6979#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006980 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6985 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006986 }
6987 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006988#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006990 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006991
6992 /*
6993 * 0 . 0 handshake type
6994 * 1 . 3 handshake length
6995 * 4 . 6 length of all certs
6996 * 7 . 9 length of cert. 1
6997 * 10 . n-1 peer certificate
6998 * n . n+2 length of cert. 2
6999 * n+3 . ... upper level cert, etc.
7000 */
7001 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007003
Paul Bakker29087132010-03-21 21:03:34 +00007004 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007005 {
7006 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10007007 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00007008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10007010 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007012 }
7013
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03007014 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007015
Teppo Järvelin91d79382019-10-02 09:09:31 +03007016 i += 3; mbedtls_platform_memcpy( ssl->out_msg + i, crt->raw.p, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007017 i += n; crt = crt->next;
7018 }
7019
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03007020 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007021
7022 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7024 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007025
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007026#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007027write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007028#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007029
Jarno Lamsa2b205162019-11-12 15:36:21 +02007030 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7031 {
7032 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7033 }
7034 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7035 {
7036 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7037 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007038 else
7039 {
7040 ssl->state = MBEDTLS_SSL_INVALID;
7041 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007042
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007043 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007044 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007045 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007046 return( ret );
7047 }
7048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007050
Paul Bakkered27a042013-04-18 22:46:23 +02007051 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007052}
7053
Hanno Becker285ff0c2019-01-31 07:44:03 +00007054#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00007055
7056#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00007057static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7058 unsigned char *crt_buf,
7059 size_t crt_buf_len )
7060{
7061 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7062
7063 if( peer_crt == NULL )
7064 return( -1 );
7065
7066 if( peer_crt->raw.len != crt_buf_len )
7067 return( -1 );
7068
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007069 return( mbedtls_platform_memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007070}
Hanno Becker5882dd02019-06-06 16:25:57 +01007071#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00007072static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7073 unsigned char *crt_buf,
7074 size_t crt_buf_len )
7075{
7076 int ret;
7077 unsigned char const * const peer_cert_digest =
7078 ssl->session->peer_cert_digest;
7079 mbedtls_md_type_t const peer_cert_digest_type =
7080 ssl->session->peer_cert_digest_type;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007081 mbedtls_md_handle_t digest_info =
Hanno Beckerdf759382019-02-05 17:02:46 +00007082 mbedtls_md_info_from_type( peer_cert_digest_type );
7083 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7084 size_t digest_len;
7085
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007086 if( peer_cert_digest == NULL ||
7087 digest_info == MBEDTLS_MD_INVALID_HANDLE )
7088 {
Hanno Beckerdf759382019-02-05 17:02:46 +00007089 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007090 }
Hanno Beckerdf759382019-02-05 17:02:46 +00007091
7092 digest_len = mbedtls_md_get_size( digest_info );
7093 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
7094 return( -1 );
7095
7096 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
7097 if( ret != 0 )
7098 return( -1 );
7099
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007100 return( mbedtls_platform_memcmp( tmp_digest, peer_cert_digest, digest_len ) );
Hanno Beckerdf759382019-02-05 17:02:46 +00007101}
Hanno Becker5882dd02019-06-06 16:25:57 +01007102#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00007103#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00007104
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007105/*
7106 * Once the certificate message is read, parse it into a cert chain and
7107 * perform basic checks, but leave actual verification to the caller
7108 */
Hanno Becker35e41772019-02-05 15:37:23 +00007109static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
7110 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00007111{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007112 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00007113#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7114 int crt_cnt=0;
7115#endif
Paul Bakker23986e52011-04-24 08:57:21 +00007116 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02007117 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00007118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007122 mbedtls_ssl_pend_fatal_alert( ssl,
7123 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007125 }
7126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
7128 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007131 mbedtls_ssl_pend_fatal_alert( ssl,
7132 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007134 }
7135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007136 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007137
Paul Bakker5121ce52009-01-03 21:22:43 +00007138 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00007140 */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007141 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007142
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007143 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007147 mbedtls_ssl_pend_fatal_alert( ssl,
7148 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007150 }
7151
Hanno Becker33c3dc82019-01-30 14:46:46 +00007152 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7153 i += 3;
7154
Hanno Becker33c3dc82019-01-30 14:46:46 +00007155 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007156 while( i < ssl->in_hslen )
7157 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007158 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02007159 if ( i + 3 > ssl->in_hslen ) {
7160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007161 mbedtls_ssl_pend_fatal_alert( ssl,
7162 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02007163 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
7164 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007165 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7166 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007167 if( ssl->in_msg[i] != 0 )
7168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007170 mbedtls_ssl_pend_fatal_alert( ssl,
7171 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007172 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007173 }
7174
Hanno Becker33c3dc82019-01-30 14:46:46 +00007175 /* Read length of the next CRT in the chain. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007176 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007177 i += 3;
7178
7179 if( n < 128 || i + n > ssl->in_hslen )
7180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007182 mbedtls_ssl_pend_fatal_alert( ssl,
7183 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007184 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007185 }
7186
Hanno Becker33c3dc82019-01-30 14:46:46 +00007187 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00007188#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7189 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01007190 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7191 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00007192 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007193 {
Hanno Becker68b856d2019-02-08 14:00:04 +00007194 /* During client-side renegotiation, check that the server's
7195 * end-CRTs hasn't changed compared to the initial handshake,
7196 * mitigating the triple handshake attack. On success, reuse
7197 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00007198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7199 if( ssl_check_peer_crt_unchanged( ssl,
7200 &ssl->in_msg[i],
7201 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007202 {
Hanno Becker35e41772019-02-05 15:37:23 +00007203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007204 mbedtls_ssl_pend_fatal_alert( ssl,
7205 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007206 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007207 }
Hanno Becker35e41772019-02-05 15:37:23 +00007208
7209 /* Now we can safely free the original chain. */
7210 ssl_clear_peer_cert( ssl->session );
7211 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007212#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7213
Hanno Becker33c3dc82019-01-30 14:46:46 +00007214 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007215#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007216 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007217#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007218 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007219 * it in-place from the input buffer instead of making a copy. */
7220 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7221#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007222 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007223 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007224 case 0: /*ok*/
7225 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7226 /* Ignore certificate with an unknown algorithm: maybe a
7227 prior certificate was already trusted. */
7228 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007229
Hanno Becker33c3dc82019-01-30 14:46:46 +00007230 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7231 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7232 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007233
Hanno Becker33c3dc82019-01-30 14:46:46 +00007234 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7235 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7236 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007237
Hanno Becker33c3dc82019-01-30 14:46:46 +00007238 default:
7239 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7240 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007241 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007242 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7243 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007244 }
7245
7246 i += n;
7247 }
7248
Hanno Becker35e41772019-02-05 15:37:23 +00007249 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007250 return( 0 );
7251}
7252
Hanno Beckerb8a08572019-02-05 12:49:06 +00007253#if defined(MBEDTLS_SSL_SRV_C)
7254static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7255{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007256 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007257 return( -1 );
7258
7259#if defined(MBEDTLS_SSL_PROTO_SSL3)
7260 /*
7261 * Check if the client sent an empty certificate
7262 */
Hanno Becker2881d802019-05-22 14:44:53 +01007263 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007264 {
7265 if( ssl->in_msglen == 2 &&
7266 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7267 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7268 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7269 {
7270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7271 return( 0 );
7272 }
7273
7274 return( -1 );
7275 }
7276#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7277
7278#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7279 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7280 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7281 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7282 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Teppo Järvelin0efac532019-10-04 13:21:08 +03007283 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 +00007284 {
7285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7286 return( 0 );
7287 }
7288
Hanno Beckerb8a08572019-02-05 12:49:06 +00007289#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7290 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007291
7292 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007293}
7294#endif /* MBEDTLS_SSL_SRV_C */
7295
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007296/* Check if a certificate message is expected.
7297 * Return either
7298 * - SSL_CERTIFICATE_EXPECTED, or
7299 * - SSL_CERTIFICATE_SKIP
7300 * indicating whether a Certificate message is expected or not.
7301 */
7302#define SSL_CERTIFICATE_EXPECTED 0
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007303#define SSL_CERTIFICATE_SKIP 0xff
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007304static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7305 int authmode )
7306{
Hanno Becker473f98f2019-06-26 10:27:32 +01007307 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007308 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007309
7310 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7311 return( SSL_CERTIFICATE_SKIP );
7312
7313#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007314 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007315 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007316 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7317 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7318 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007319 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007320 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007321
7322 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7323 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007324 ssl->session_negotiate->verify_result =
7325 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7326 return( SSL_CERTIFICATE_SKIP );
7327 }
7328 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007329#else
7330 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007331#endif /* MBEDTLS_SSL_SRV_C */
7332
7333 return( SSL_CERTIFICATE_EXPECTED );
7334}
7335
Hanno Becker3cf50612019-02-05 14:36:34 +00007336static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007337 volatile int authmode,
Hanno Becker3cf50612019-02-05 14:36:34 +00007338 mbedtls_x509_crt *chain,
7339 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007340{
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007341 volatile int verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Andrzej Kurekfd56f402020-05-25 11:52:05 -04007342 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007343 volatile int flow_counter = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01007344 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007345 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007346 mbedtls_x509_crt *ca_chain;
7347 mbedtls_x509_crl *ca_crl;
7348
7349 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
Jarno Lamsae1621d42019-12-19 08:58:56 +02007350 {
Hanno Becker3cf50612019-02-05 14:36:34 +00007351 return( 0 );
Jarno Lamsae1621d42019-12-19 08:58:56 +02007352 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007353
Jarno Lamsae1621d42019-12-19 08:58:56 +02007354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Hanno Becker3cf50612019-02-05 14:36:34 +00007355#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7356 if( ssl->handshake->sni_ca_chain != NULL )
7357 {
7358 ca_chain = ssl->handshake->sni_ca_chain;
7359 ca_crl = ssl->handshake->sni_ca_crl;
7360 }
7361 else
7362#endif
7363 {
7364 ca_chain = ssl->conf->ca_chain;
7365 ca_crl = ssl->conf->ca_crl;
7366 }
7367
7368 /*
7369 * Main check: verify certificate
7370 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007371 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007372 chain,
7373 ca_chain, ca_crl,
7374 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007375#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007376 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007377#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007378 &ssl->session_negotiate->verify_result,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01007379#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
7380 ssl->conf->f_vrfy, ssl->conf->p_vrfy,
7381#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
7382 rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007383
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007384 if( verify_ret == 0 )
7385 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007386 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007387 if( verify_ret == 0 )
7388 {
7389 flow_counter++;
7390 }
7391 else
7392 {
7393 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7394 }
7395 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007396 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007397 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007398 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007399 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007400 }
7401
7402#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007403 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007404 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7405#endif
7406
7407 /*
7408 * Secondary checks: always done, but change 'ret' only if it was 0
7409 */
7410
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007411#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007412 {
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007413#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007414 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007415#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007416 mbedtls_pk_context *pk;
7417 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7418 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007419 {
7420 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007421 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007422 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007423
7424 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007425 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007426 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007427 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007428 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007429
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007430 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007431#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007432
7433 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007434 {
7435 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7436
7437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007438 if( verify_ret == 0 )
7439 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007440 flow_counter++;
7441 }
7442 if( ret == 0 )
7443 {
7444 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007445 }
7446 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007447#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007448
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007449 ret = mbedtls_ssl_check_cert_usage( chain,
Hanno Becker3cf50612019-02-05 14:36:34 +00007450 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007451 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7452 MBEDTLS_SSL_IS_CLIENT ),
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007453 &ssl->session_negotiate->verify_result );
7454 if( ret == 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007455 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007456 flow_counter++;
7457 }
7458 else
7459 {
7460 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007462 if( verify_ret == 0 )
7463 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007464 }
7465
7466 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7467 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7468 * with details encoded in the verification flags. All other kinds
7469 * of error codes, including those from the user provided f_vrfy
7470 * functions, are treated as fatal and lead to a failure of
7471 * ssl_parse_certificate even if verification was optional. */
7472 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007473 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7474 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007475 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007476 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007477 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7478 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7479 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7480 {
7481 verify_ret = 0;
7482 flow_counter++;
7483 }
7484 else
7485 {
7486 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7487 }
7488 } else {
7489 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007490 }
7491
7492 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7493 {
7494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007495 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007496 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007497 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007498 else
7499 {
7500 flow_counter++;
7501 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007502
Hanno Becker8c13ee62019-02-26 16:48:17 +00007503 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007504 {
7505 uint8_t alert;
7506
7507 /* The certificate may have been rejected for several reasons.
7508 Pick one and send the corresponding alert. Which alert to send
7509 may be a subject of debate in some cases. */
7510 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7511 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7512 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7513 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7514 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7515 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7516 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7517 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7518 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7519 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7520 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7521 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7522 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7523 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7524 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7525 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7526 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7527 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7528 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7529 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7530 else
7531 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007532 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007533 }
7534
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007535 if( verify_ret == 0 &&
7536#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7537 flow_counter == 5 )
7538#else
7539 flow_counter == 4 )
7540#endif
7541 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007542 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007543 if( verify_ret == 0 &&
7544#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7545 flow_counter == 5 )
7546#else
7547 flow_counter == 4 )
7548#endif
7549 {
Jarno Lamsae1621d42019-12-19 08:58:56 +02007550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER AUTHENTICATED" ) );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007551 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
7552 }
7553 else
7554 {
7555 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7556 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007557 } else {
7558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER NOT AUTHENTICATED, %d", flow_counter));
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007559 }
7560
Hanno Becker3cf50612019-02-05 14:36:34 +00007561#if defined(MBEDTLS_DEBUG_C)
7562 if( ssl->session_negotiate->verify_result != 0 )
7563 {
7564 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7565 ssl->session_negotiate->verify_result ) );
7566 }
7567 else
7568 {
7569 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7570 }
7571#endif /* MBEDTLS_DEBUG_C */
7572
Hanno Becker8c13ee62019-02-26 16:48:17 +00007573 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007574}
7575
Hanno Becker34106f62019-02-08 14:59:05 +00007576#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007577
7578#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007579static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7580 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007581{
7582 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007583 /* Remember digest of the peer's end-CRT. */
7584 ssl->session_negotiate->peer_cert_digest =
7585 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7586 if( ssl->session_negotiate->peer_cert_digest == NULL )
7587 {
7588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7589 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007590 mbedtls_ssl_pend_fatal_alert( ssl,
7591 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007592
7593 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7594 }
7595
7596 ret = mbedtls_md( mbedtls_md_info_from_type(
7597 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7598 start, len,
7599 ssl->session_negotiate->peer_cert_digest );
7600
7601 ssl->session_negotiate->peer_cert_digest_type =
7602 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7603 ssl->session_negotiate->peer_cert_digest_len =
7604 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7605
7606 return( ret );
7607}
Hanno Becker5882dd02019-06-06 16:25:57 +01007608#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007609
7610static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7611 unsigned char *start, size_t len )
7612{
7613 unsigned char *end = start + len;
7614 int ret;
7615
7616 /* Make a copy of the peer's raw public key. */
7617 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7618 ret = mbedtls_pk_parse_subpubkey( &start, end,
7619 &ssl->handshake->peer_pubkey );
7620 if( ret != 0 )
7621 {
7622 /* We should have parsed the public key before. */
7623 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7624 }
7625
Manuel Pégourié-Gonnard2829bbf2019-09-19 10:45:14 +02007626 ssl->handshake->got_peer_pubkey = 1;
Hanno Becker34106f62019-02-08 14:59:05 +00007627 return( 0 );
7628}
7629#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7630
Hanno Becker3cf50612019-02-05 14:36:34 +00007631int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7632{
7633 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007634 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007635#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7636 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7637 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007638 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007639#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007640 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007641#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007642 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007643 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007644
7645 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7646
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007647 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7648 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007649 {
7650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007651 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007652 }
7653
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007654#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7655 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007656 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007657 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007658 chain = ssl->handshake->ecrs_peer_cert;
7659 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007660 goto crt_verify;
7661 }
7662#endif
7663
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007664 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007665 {
7666 /* mbedtls_ssl_read_record may have sent an alert already. We
7667 let it decide whether to alert. */
7668 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007669 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007670 }
7671
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007672#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007673 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7674 {
7675 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007676
Hanno Beckerb8a08572019-02-05 12:49:06 +00007677 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007678 ret = 0;
7679 else
7680 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007681
Hanno Becker613d4902019-02-05 13:11:17 +00007682 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007683 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007684#endif /* MBEDTLS_SSL_SRV_C */
7685
Hanno Becker35e41772019-02-05 15:37:23 +00007686 /* Clear existing peer CRT structure in case we tried to
7687 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007688 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007689
7690 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7691 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007692 {
7693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7694 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007695 mbedtls_ssl_pend_fatal_alert( ssl,
7696 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007697
Hanno Beckere4aeb762019-02-05 17:19:52 +00007698 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7699 goto exit;
7700 }
7701 mbedtls_x509_crt_init( chain );
7702
7703 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007704 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007705 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007706
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007707#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7708 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007709 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007710
7711crt_verify:
7712 if( ssl->handshake->ecrs_enabled)
7713 rs_ctx = &ssl->handshake->ecrs_ctx;
7714#endif
7715
Hanno Becker3cf50612019-02-05 14:36:34 +00007716 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007717 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007718 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007719 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007720
Hanno Becker3008d282019-02-05 17:02:28 +00007721#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007722 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007723 size_t pk_len;
7724 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007725
Hanno Becker34106f62019-02-08 14:59:05 +00007726 /* We parse the CRT chain without copying, so
7727 * these pointers point into the input buffer,
7728 * and are hence still valid after freeing the
7729 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007730
Hanno Becker5882dd02019-06-06 16:25:57 +01007731#if defined(MBEDTLS_SSL_RENEGOTIATION)
7732 unsigned char *crt_start;
7733 size_t crt_len;
7734
Hanno Becker34106f62019-02-08 14:59:05 +00007735 crt_start = chain->raw.p;
7736 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007737#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007738
Hanno Becker8c13ee62019-02-26 16:48:17 +00007739 pk_start = chain->cache->pk_raw.p;
7740 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007741
7742 /* Free the CRT structures before computing
7743 * digest and copying the peer's public key. */
7744 mbedtls_x509_crt_free( chain );
7745 mbedtls_free( chain );
7746 chain = NULL;
7747
Hanno Becker5882dd02019-06-06 16:25:57 +01007748#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007749 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007750 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007751 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007752#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007753
Hanno Becker34106f62019-02-08 14:59:05 +00007754 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007755 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007756 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007757 }
Hanno Becker34106f62019-02-08 14:59:05 +00007758#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7759 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007760 ssl->session_negotiate->peer_cert = chain;
7761 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007762#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007765
Hanno Becker613d4902019-02-05 13:11:17 +00007766exit:
7767
Hanno Beckere4aeb762019-02-05 17:19:52 +00007768 if( ret == 0 )
Jarno Lamsa2b205162019-11-12 15:36:21 +02007769 {
7770 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7771 {
7772 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7773 }
7774 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7775 {
7776 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7777 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007778 else
7779 {
7780 ssl->state = MBEDTLS_SSL_INVALID;
7781 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02007782 }
Hanno Beckere4aeb762019-02-05 17:19:52 +00007783
7784#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7785 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7786 {
7787 ssl->handshake->ecrs_peer_cert = chain;
7788 chain = NULL;
7789 }
7790#endif
7791
7792 if( chain != NULL )
7793 {
7794 mbedtls_x509_crt_free( chain );
7795 mbedtls_free( chain );
7796 }
7797
Paul Bakker5121ce52009-01-03 21:22:43 +00007798 return( ret );
7799}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007800#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007803{
7804 int ret;
7805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007808 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007809 ssl->out_msglen = 1;
7810 ssl->out_msg[0] = 1;
7811
Jarno Lamsa2b205162019-11-12 15:36:21 +02007812 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7813 {
7814 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7815 }
7816 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7817 {
7818 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7819 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007820 else
7821 {
7822 ssl->state = MBEDTLS_SSL_INVALID;
7823 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007824
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007825 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007826 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007828 return( ret );
7829 }
7830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007832
7833 return( 0 );
7834}
7835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007836int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007837{
7838 int ret;
7839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007841
Hanno Becker327c93b2018-08-15 13:56:18 +01007842 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007845 return( ret );
7846 }
7847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007848 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007851 mbedtls_ssl_pend_fatal_alert( ssl,
7852 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007854 }
7855
Hanno Beckere678eaa2018-08-21 14:57:46 +01007856 /* CCS records are only accepted if they have length 1 and content '1',
7857 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007858
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007859 /*
7860 * Switch to our negotiated transform and session parameters for inbound
7861 * data.
7862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007864 ssl->transform_in = ssl->transform_negotiate;
7865 ssl->session_in = ssl->session_negotiate;
7866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007867#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007868 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007870#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007871 ssl_dtls_replay_reset( ssl );
7872#endif
7873
7874 /* Increment epoch */
7875 if( ++ssl->in_epoch == 0 )
7876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007878 /* This is highly unlikely to happen for legitimate reasons, so
7879 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007880 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007881 }
7882 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007883 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007885#if defined(MBEDTLS_SSL_PROTO_TLS)
7886 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02007887 mbedtls_platform_memset( ssl->in_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007888 }
7889#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007890
Hanno Beckerf5970a02019-05-08 09:38:41 +01007891 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7894 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007899 mbedtls_ssl_pend_fatal_alert( ssl,
7900 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007902 }
7903 }
7904#endif
7905
Jarno Lamsa2b205162019-11-12 15:36:21 +02007906 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7907 {
7908 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7909 }
7910 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7911 {
7912 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7913 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007914 else
7915 {
7916 ssl->state = MBEDTLS_SSL_INVALID;
7917 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007920
7921 return( 0 );
7922}
7923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007924void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007925{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7927 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007928 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007929 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007930#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007931#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7932#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007933 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007934#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007936 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007937#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007939}
7940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007942{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007944
7945 /*
7946 * Free our handshake params
7947 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007948 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007950 ssl->handshake = NULL;
7951
7952 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007953 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007954 */
7955 if( ssl->transform )
7956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007957 mbedtls_ssl_transform_free( ssl->transform );
7958 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007959 }
7960 ssl->transform = ssl->transform_negotiate;
7961 ssl->transform_negotiate = NULL;
7962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007964}
7965
Jarno Lamsae1621d42019-12-19 08:58:56 +02007966int mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007967{
Andrzej Kurekfd56f402020-05-25 11:52:05 -04007968 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007969
7970#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Jarno Lamsa015aa442019-12-20 12:09:37 +02007971 volatile const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007972 ? ssl->handshake->sni_authmode
7973 : mbedtls_ssl_conf_get_authmode( ssl->conf );
7974#else
Jarno Lamsa015aa442019-12-20 12:09:37 +02007975 volatile const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007976#endif
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007977#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7978 volatile int crt_expected = SSL_CERTIFICATE_EXPECTED;
7979 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7980#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007983#if defined(MBEDTLS_SSL_RENEGOTIATION)
7984 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007987 ssl->renego_records_seen = 0;
7988 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007989#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007990
7991 /*
7992 * Free the previous session and switch in the current one
7993 */
Paul Bakker0a597072012-09-25 21:55:46 +00007994 if( ssl->session )
7995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007997 /* RFC 7366 3.1: keep the EtM state */
7998 ssl->session_negotiate->encrypt_then_mac =
7999 ssl->session->encrypt_then_mac;
8000#endif
8001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008002 mbedtls_ssl_session_free( ssl->session );
8003 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00008004 }
8005 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00008006 ssl->session_negotiate = NULL;
8007
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02008008#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00008009 /*
8010 * Add cache entry
8011 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008012 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02008013 ssl->session->id_len != 0 &&
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008014 ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_UNSET )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02008015 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008016 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02008018 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02008019#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00008020
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008021 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8022 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8023#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8024 crt_expected == SSL_CERTIFICATE_SKIP )
8025#else
8026 1 )
8027#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008028 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008029 mbedtls_platform_random_delay();
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008030 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8031 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8032#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8033 crt_expected == SSL_CERTIFICATE_SKIP )
8034#else
8035 1 )
8036#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008037 {
8038 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
8039 }
8040 else
8041 {
8042 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8043 goto cleanup;
8044 }
8045 }
8046
Jarno Lamsae1621d42019-12-19 08:58:56 +02008047#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008048 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008049 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008050 mbedtls_platform_random_delay();
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008051 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008052 {
Jarno Lamsa06164052019-12-19 14:40:36 +02008053 /* When doing session resume, no premaster or peer authentication */
Jarno Lamsae1621d42019-12-19 08:58:56 +02008054 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsa06164052019-12-19 14:40:36 +02008055 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008056 }
8057 else
8058 {
8059 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008060 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008061 }
8062 }
8063#endif
8064
8065 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8066 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008067 mbedtls_platform_random_delay();
Jarno Lamsae1621d42019-12-19 08:58:56 +02008068 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8069 {
8070 ret = 0;
8071 }
8072 else
8073 {
8074 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008075 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008076 }
8077 }
8078 else
8079 {
8080 ret = MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008081 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008082 }
8083
Jarno Lamsa06164052019-12-19 14:40:36 +02008084 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8085 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8086 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8087 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008088 mbedtls_platform_random_delay();
Jarno Lamsa06164052019-12-19 14:40:36 +02008089 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8090 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8091 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8092 {
8093 ret = 0;
8094 }
8095 else
8096 {
8097 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8098 goto cleanup;
8099 }
8100 }
8101 else
8102 {
8103 MBEDTLS_SSL_DEBUG_MSG( 3, ( "hello random %d", ssl->handshake->hello_random_set ) );
8104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "key_derivation_done %d", ssl->handshake->key_derivation_done ) );
8105 MBEDTLS_SSL_DEBUG_MSG( 3, ( "premaster_generated %d", ssl->handshake->premaster_generated ) );
8106 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8107 }
8108
8109cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008110#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008111 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008112 ssl->handshake->flight != NULL )
8113 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008114 /* Cancel handshake timer */
8115 ssl_set_timer( ssl, 0 );
8116
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008117 /* Keep last flight around in case we need to resend it:
8118 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008119 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008120 }
8121 else
8122#endif
8123 ssl_handshake_wrapup_free_hs_transform( ssl );
8124
Jarno Lamsa2b205162019-11-12 15:36:21 +02008125 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Paul Bakker48916f92012-09-16 19:57:18 +00008126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008127 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Jarno Lamsae1621d42019-12-19 08:58:56 +02008128 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00008129}
8130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008131int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00008132{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008133 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00008134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008136
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008137 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01008138
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008139 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8140 mbedtls_ssl_suite_get_mac(
8141 mbedtls_ssl_ciphersuite_from_id(
8142 mbedtls_ssl_session_get_ciphersuite(
8143 ssl->session_negotiate ) ) ),
8144 ssl, ssl->out_msg + 4,
8145 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008146
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01008147 /*
8148 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8149 * may define some other value. Currently (early 2016), no defined
8150 * ciphersuite does this (and this is unlikely to change as activity has
8151 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8152 */
Hanno Becker2881d802019-05-22 14:44:53 +01008153 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008155#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008156 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008157 mbedtls_platform_memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008158#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008159
Paul Bakker5121ce52009-01-03 21:22:43 +00008160 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8162 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008163
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008164#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00008165 /*
8166 * In case of session resuming, invert the client and server
8167 * ChangeCipherSpec messages order.
8168 */
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008169 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008171#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008172 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8173 MBEDTLS_SSL_IS_CLIENT )
8174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008175 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008176 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008177#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008178#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008179 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8180 MBEDTLS_SSL_IS_SERVER )
8181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008182 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008183 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008184#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008185 }
8186 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008187#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008188 {
8189 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8190 {
8191 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8192 }
8193 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8194 {
8195 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8196 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008197 else
8198 {
8199 ssl->state = MBEDTLS_SSL_INVALID;
8200 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008201 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008202
Paul Bakker48916f92012-09-16 19:57:18 +00008203 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008204 * Switch to our negotiated transform and session parameters for outbound
8205 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008207 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008210 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008211 {
8212 unsigned char i;
8213
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008214 /* Remember current epoch settings for resending */
8215 ssl->handshake->alt_transform_out = ssl->transform_out;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008216 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008217
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008218 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008219 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008220
8221 /* Increment epoch */
8222 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008223 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008224 break;
8225
8226 /* The loop goes to its end iff the counter is wrapping */
8227 if( i == 0 )
8228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8230 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008231 }
8232 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008233 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008234#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008235#if defined(MBEDTLS_SSL_PROTO_TLS)
8236 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008237 mbedtls_platform_memset( ssl->cur_out_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008238 }
8239#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008240
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008241 ssl->transform_out = ssl->transform_negotiate;
8242 ssl->session_out = ssl->session_negotiate;
8243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008244#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8245 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008247 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8250 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008251 }
8252 }
8253#endif
8254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008255#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008256 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008257 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008258#endif
8259
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008260 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008261 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008262 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008263 return( ret );
8264 }
8265
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008266#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008267 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008268 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8269 {
8270 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8271 return( ret );
8272 }
8273#endif
8274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008276
8277 return( 0 );
8278}
8279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008280#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008281#define SSL_MAX_HASH_LEN 36
8282#else
8283#define SSL_MAX_HASH_LEN 12
8284#endif
8285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008286int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008287{
Paul Bakker23986e52011-04-24 08:57:21 +00008288 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008289 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008290 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008293
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008294 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8295 mbedtls_ssl_suite_get_mac(
8296 mbedtls_ssl_ciphersuite_from_id(
8297 mbedtls_ssl_session_get_ciphersuite(
8298 ssl->session_negotiate ) ) ),
8299 ssl, buf,
8300 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008301
Hanno Becker327c93b2018-08-15 13:56:18 +01008302 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008305 return( ret );
8306 }
8307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008308 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008311 mbedtls_ssl_pend_fatal_alert( ssl,
8312 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008313 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008314 }
8315
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008316 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008317#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01008318 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008319 hash_len = 36;
8320 else
8321#endif
8322 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008324 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8325 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008328 mbedtls_ssl_pend_fatal_alert( ssl,
8329 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008330 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008331 }
8332
Teppo Järvelin707ceb82019-10-04 07:49:39 +03008333 if( mbedtls_platform_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008334 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008337 mbedtls_ssl_pend_fatal_alert( ssl,
8338 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008339 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008340 }
8341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008342#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008343 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008344 mbedtls_platform_memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008345#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008346
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008347#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008348 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008350#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008351 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008352 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008353#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008355 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008356 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008357#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008358 }
8359 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008360#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008361 {
8362 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8363 {
8364 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8365 }
8366 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8367 {
8368 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8369 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008370 else
8371 {
8372 ssl->state = MBEDTLS_SSL_INVALID;
8373 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008374 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008376#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008377 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008378 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008379#endif
8380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008382
8383 return( 0 );
8384}
8385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008386static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008390#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8391 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8392 mbedtls_md5_init( &handshake->fin_md5 );
8393 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008394 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8395 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008396#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008397#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8398#if defined(MBEDTLS_SHA256_C)
8399 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008400 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008401#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008402#if defined(MBEDTLS_SHA512_C)
8403 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008404 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008405#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008406#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008407
Hanno Becker7e5437a2017-04-28 17:15:26 +01008408#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8409 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8410 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8411#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008413#if defined(MBEDTLS_DHM_C)
8414 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008415#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008416#if defined(MBEDTLS_ECDH_C)
8417 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008418#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008419#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008420 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008421#if defined(MBEDTLS_SSL_CLI_C)
8422 handshake->ecjpake_cache = NULL;
8423 handshake->ecjpake_cache_len = 0;
8424#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008425#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008426
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008427#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008428 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008429#endif
8430
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008431#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8432 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8433#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008434
8435#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8436 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8437 mbedtls_pk_init( &handshake->peer_pubkey );
8438#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008439}
8440
Hanno Becker611a83b2018-01-03 14:27:32 +00008441void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008442{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008445 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8446 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008447
Hanno Becker92231322018-01-03 15:32:51 +00008448#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008449 mbedtls_md_init( &transform->md_ctx_enc );
8450 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008451#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008452}
8453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008454void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008455{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008456 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008457}
8458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008459static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008460{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008461 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008462 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008463 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008464 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008465 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008466 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008467 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008468
8469 /*
8470 * Either the pointers are now NULL or cleared properly and can be freed.
8471 * Now allocate missing structures.
8472 */
8473 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008474 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008475 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008476 }
Paul Bakker48916f92012-09-16 19:57:18 +00008477
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008478 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008479 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008480 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008481 }
Paul Bakker48916f92012-09-16 19:57:18 +00008482
Paul Bakker82788fb2014-10-20 13:59:19 +02008483 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008484 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008485 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008486 }
Paul Bakker48916f92012-09-16 19:57:18 +00008487
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008488 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008489 if( ssl->handshake == NULL ||
8490 ssl->transform_negotiate == NULL ||
8491 ssl->session_negotiate == NULL )
8492 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008495 mbedtls_free( ssl->handshake );
8496 mbedtls_free( ssl->transform_negotiate );
8497 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008498
8499 ssl->handshake = NULL;
8500 ssl->transform_negotiate = NULL;
8501 ssl->session_negotiate = NULL;
8502
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008503 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008504 }
8505
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008506 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008507 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008508 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008509 ssl_handshake_params_init( ssl->handshake );
8510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008511#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008512 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008513 {
8514 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008515
Hanno Becker2d9623f2019-06-13 12:07:05 +01008516 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008517 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8518 else
8519 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8520 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008521#endif
8522
Paul Bakker48916f92012-09-16 19:57:18 +00008523 return( 0 );
8524}
8525
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008526#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008527/* Dummy cookie callbacks for defaults */
8528static int ssl_cookie_write_dummy( void *ctx,
8529 unsigned char **p, unsigned char *end,
8530 const unsigned char *cli_id, size_t cli_id_len )
8531{
8532 ((void) ctx);
8533 ((void) p);
8534 ((void) end);
8535 ((void) cli_id);
8536 ((void) cli_id_len);
8537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008538 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008539}
8540
8541static int ssl_cookie_check_dummy( void *ctx,
8542 const unsigned char *cookie, size_t cookie_len,
8543 const unsigned char *cli_id, size_t cli_id_len )
8544{
8545 ((void) ctx);
8546 ((void) cookie);
8547 ((void) cookie_len);
8548 ((void) cli_id);
8549 ((void) cli_id_len);
8550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008551 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008552}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008553#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008554
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008555/* Once ssl->out_hdr as the address of the beginning of the
8556 * next outgoing record is set, deduce the other pointers.
8557 *
8558 * Note: For TLS, we save the implicit record sequence number
8559 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8560 * and the caller has to make sure there's space for this.
8561 */
8562
8563static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8564 mbedtls_ssl_transform *transform )
8565{
8566#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008567 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008568 {
8569 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008570#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008571 ssl->out_cid = ssl->out_ctr + 8;
8572 ssl->out_len = ssl->out_cid;
8573 if( transform != NULL )
8574 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008575#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008576 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008577#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008578 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008579 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008580 MBEDTLS_SSL_TRANSPORT_ELSE
8581#endif /* MBEDTLS_SSL_PROTO_DTLS */
8582#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008583 {
8584 ssl->out_ctr = ssl->out_hdr - 8;
8585 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008586#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008587 ssl->out_cid = ssl->out_len;
8588#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008589 ssl->out_iv = ssl->out_hdr + 5;
8590 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008591#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008592
8593 /* Adjust out_msg to make space for explicit IV, if used. */
8594 if( transform != NULL &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008595 mbedtls_ssl_ver_geq(
8596 mbedtls_ssl_get_minor_ver( ssl ),
8597 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008598 {
8599 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8600 }
8601 else
8602 ssl->out_msg = ssl->out_iv;
8603}
8604
8605/* Once ssl->in_hdr as the address of the beginning of the
8606 * next incoming record is set, deduce the other pointers.
8607 *
8608 * Note: For TLS, we save the implicit record sequence number
8609 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8610 * and the caller has to make sure there's space for this.
8611 */
8612
Hanno Beckerf5970a02019-05-08 09:38:41 +01008613static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008614{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008615 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008616 * of unprotected TLS/DTLS records, with ssl->in_msg
8617 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008618 *
8619 * When decrypting a protected record, ssl->in_msg
8620 * will be shifted to point to the beginning of the
8621 * record plaintext.
8622 */
8623
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008624#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008625 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008626 {
Hanno Becker70e79282019-05-03 14:34:53 +01008627 /* This sets the header pointers to match records
8628 * without CID. When we receive a record containing
8629 * a CID, the fields are shifted accordingly in
8630 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008631 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008632#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008633 ssl->in_cid = ssl->in_ctr + 8;
8634 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008635#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008636 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008637#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008638 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008639 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008640 MBEDTLS_SSL_TRANSPORT_ELSE
8641#endif /* MBEDTLS_SSL_PROTO_DTLS */
8642#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008643 {
8644 ssl->in_ctr = ssl->in_hdr - 8;
8645 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008646#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008647 ssl->in_cid = ssl->in_len;
8648#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008649 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008650 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008651#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008652}
8653
Paul Bakker5121ce52009-01-03 21:22:43 +00008654/*
8655 * Initialize an SSL context
8656 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008657void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8658{
8659 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8660}
8661
8662/*
8663 * Setup an SSL context
8664 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008665
8666static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8667{
8668 /* Set the incoming and outgoing record pointers. */
8669#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008670 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008671 {
8672 ssl->out_hdr = ssl->out_buf;
8673 ssl->in_hdr = ssl->in_buf;
8674 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008675 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008676#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008677#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008678 {
8679 ssl->out_hdr = ssl->out_buf + 8;
8680 ssl->in_hdr = ssl->in_buf + 8;
8681 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008682#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008683
8684 /* Derive other internal pointers. */
8685 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008686 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008687}
8688
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008689int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008690 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008691{
Paul Bakker48916f92012-09-16 19:57:18 +00008692 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008693
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008694 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008695
Hanno Beckeref982d52019-07-23 15:56:18 +01008696#if defined(MBEDTLS_USE_TINYCRYPT)
8697 uECC_set_rng( &uecc_rng_wrapper );
8698#endif
8699
Paul Bakker62f2dee2012-09-28 07:31:51 +00008700 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008701 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008702 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008703
8704 /* Set to NULL in case of an error condition */
8705 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008706
Angus Grattond8213d02016-05-25 20:56:48 +10008707 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8708 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008709 {
Angus Grattond8213d02016-05-25 20:56:48 +10008710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008711 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008712 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008713 }
8714
8715 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8716 if( ssl->out_buf == NULL )
8717 {
8718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008719 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008720 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008721 }
8722
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008723 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008724
Paul Bakker48916f92012-09-16 19:57:18 +00008725 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008726 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008727
Hanno Beckerc8f52992019-07-25 11:15:08 +01008728 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008729
Paul Bakker5121ce52009-01-03 21:22:43 +00008730 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008731
8732error:
8733 mbedtls_free( ssl->in_buf );
8734 mbedtls_free( ssl->out_buf );
8735
8736 ssl->conf = NULL;
8737
8738 ssl->in_buf = NULL;
8739 ssl->out_buf = NULL;
8740
8741 ssl->in_hdr = NULL;
8742 ssl->in_ctr = NULL;
8743 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008744 ssl->in_msg = NULL;
8745
8746 ssl->out_hdr = NULL;
8747 ssl->out_ctr = NULL;
8748 ssl->out_len = NULL;
8749 ssl->out_iv = NULL;
8750 ssl->out_msg = NULL;
8751
k-stachowiak9f7798e2018-07-31 16:52:32 +02008752 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008753}
8754
8755/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008756 * Reset an initialized and used SSL context for re-use while retaining
8757 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008758 *
8759 * If partial is non-zero, keep data in the input buffer and client ID.
8760 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008761 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008762static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008763{
Paul Bakker48916f92012-09-16 19:57:18 +00008764 int ret;
8765
Hanno Becker7e772132018-08-10 12:38:21 +01008766#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8767 !defined(MBEDTLS_SSL_SRV_C)
8768 ((void) partial);
8769#endif
8770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008771 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008772
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008773 /* Cancel any possibly running timer */
8774 ssl_set_timer( ssl, 0 );
8775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008776#if defined(MBEDTLS_SSL_RENEGOTIATION)
8777 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008778 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008779
8780 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008781 mbedtls_platform_memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8782 mbedtls_platform_memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008783#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008784 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008785
Paul Bakker7eb013f2011-10-06 12:37:39 +00008786 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008787 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008788
8789 ssl->in_msgtype = 0;
8790 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008791#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008792 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008793 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008794#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008795#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008796 ssl_dtls_replay_reset( ssl );
8797#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008798
8799 ssl->in_hslen = 0;
8800 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008801
8802 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008803
8804 ssl->out_msgtype = 0;
8805 ssl->out_msglen = 0;
8806 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008807#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8808 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008809 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008810#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008811
Hanno Becker19859472018-08-06 09:40:20 +01008812 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8813
Paul Bakker48916f92012-09-16 19:57:18 +00008814 ssl->transform_in = NULL;
8815 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008816
Hanno Becker78640902018-08-13 16:35:15 +01008817 ssl->session_in = NULL;
8818 ssl->session_out = NULL;
8819
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008820 mbedtls_platform_memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008821
8822#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008823 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008824#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8825 {
8826 ssl->in_left = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008827 mbedtls_platform_memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008828 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008830#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8831 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8834 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008836 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8837 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008838 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008839 }
8840#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008841
Paul Bakker48916f92012-09-16 19:57:18 +00008842 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008844 mbedtls_ssl_transform_free( ssl->transform );
8845 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008846 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008847 }
Paul Bakker48916f92012-09-16 19:57:18 +00008848
Paul Bakkerc0463502013-02-14 11:19:38 +01008849 if( ssl->session )
8850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008851 mbedtls_ssl_session_free( ssl->session );
8852 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008853 ssl->session = NULL;
8854 }
8855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008856#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008857 ssl->alpn_chosen = NULL;
8858#endif
8859
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008860#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008861#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008862 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008863#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008864 {
8865 mbedtls_free( ssl->cli_id );
8866 ssl->cli_id = NULL;
8867 ssl->cli_id_len = 0;
8868 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008869#endif
8870
Paul Bakker48916f92012-09-16 19:57:18 +00008871 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8872 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008873
8874 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008875}
8876
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008877/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008878 * Reset an initialized and used SSL context for re-use while retaining
8879 * all application-set variables, function pointers and data.
8880 */
8881int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8882{
8883 return( ssl_session_reset_int( ssl, 0 ) );
8884}
8885
8886/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008887 * SSL set accessors
8888 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008889#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008890void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008891{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008892 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008893}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008894#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008895
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008896void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008897{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008898 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008899}
8900
Hanno Becker7f376f42019-06-12 16:20:48 +01008901#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8902 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008903void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008904{
Hanno Becker7f376f42019-06-12 16:20:48 +01008905 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008906}
Hanno Becker7f376f42019-06-12 16:20:48 +01008907#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008908
Hanno Beckerde671542019-06-12 16:30:46 +01008909#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8910 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8911void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8912 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008913{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008914 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008915}
Hanno Beckerde671542019-06-12 16:30:46 +01008916#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008918#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008919
Hanno Becker1841b0a2018-08-24 11:13:57 +01008920void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8921 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008922{
8923 ssl->disable_datagram_packing = !allow_packing;
8924}
8925
Hanno Becker1f835fa2019-06-13 10:14:59 +01008926#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8927 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008928void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8929 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008930{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008931 conf->hs_timeout_min = min;
8932 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008933}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008934#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8935 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8936void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8937 uint32_t min, uint32_t max )
8938{
8939 ((void) conf);
8940 ((void) min);
8941 ((void) max);
8942}
8943#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8944 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8945
8946#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008947
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008948void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008949{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008950#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8951 conf->authmode = authmode;
8952#else
8953 ((void) conf);
8954 ((void) authmode);
8955#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008956}
8957
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008958#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8959 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008960void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008961 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008962 void *p_vrfy )
8963{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008964 conf->f_vrfy = f_vrfy;
8965 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008966}
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008967#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008968
Hanno Beckerece325c2019-06-13 15:39:27 +01008969#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008970void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008971 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008972 void *p_rng )
8973{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008974 conf->f_rng = f_rng;
8975 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008976}
Hanno Beckerece325c2019-06-13 15:39:27 +01008977#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008978
Hanno Becker14a4a442019-07-02 17:00:34 +01008979#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008980void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008981 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008982 void *p_dbg )
8983{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008984 conf->f_dbg = f_dbg;
8985 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008986}
Hanno Becker14a4a442019-07-02 17:00:34 +01008987#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008988
Hanno Beckera58a8962019-06-13 16:11:15 +01008989#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8990 !defined(MBEDTLS_SSL_CONF_SEND) && \
8991 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008992void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008993 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008994 mbedtls_ssl_send_t *f_send,
8995 mbedtls_ssl_recv_t *f_recv,
8996 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008997{
Hanno Beckera58a8962019-06-13 16:11:15 +01008998 ssl->p_bio = p_bio;
8999 ssl->f_send = f_send;
9000 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009001 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009002}
Hanno Beckera58a8962019-06-13 16:11:15 +01009003#else
9004void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
9005 void *p_bio )
9006{
9007 ssl->p_bio = p_bio;
9008}
9009#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009010
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02009011#if defined(MBEDTLS_SSL_PROTO_DTLS)
9012void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
9013{
9014 ssl->mtu = mtu;
9015}
9016#endif
9017
Hanno Becker1f835fa2019-06-13 10:14:59 +01009018#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009019void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009020{
9021 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009022}
Hanno Becker1f835fa2019-06-13 10:14:59 +01009023#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009024
Hanno Becker0ae6b242019-06-13 16:45:36 +01009025#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
9026 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009027void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
9028 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00009029 mbedtls_ssl_set_timer_t *f_set_timer,
9030 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009031{
9032 ssl->p_timer = p_timer;
9033 ssl->f_set_timer = f_set_timer;
9034 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009035 /* Make sure we start with no timer running */
9036 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009037}
Hanno Becker0ae6b242019-06-13 16:45:36 +01009038#else
9039void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
9040 void *p_timer )
9041{
9042 ssl->p_timer = p_timer;
9043 /* Make sure we start with no timer running */
9044 ssl_set_timer( ssl, 0 );
9045}
9046#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009047
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009048#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009049void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009050 void *p_cache,
9051 int (*f_get_cache)(void *, mbedtls_ssl_session *),
9052 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00009053{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009054 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009055 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009056 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00009057}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009058#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009059
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009060#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009061int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00009062{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009063 int ret;
9064
9065 if( ssl == NULL ||
9066 session == NULL ||
9067 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009068 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009070 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009071 }
9072
Hanno Becker58fccf22019-02-06 14:30:46 +00009073 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
9074 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009075 return( ret );
9076
Jarno Lamsa8d09e572019-12-19 15:20:19 +02009077 ssl->handshake->resume = MBEDTLS_SSL_FI_FLAG_SET;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009078
9079 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009080}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009081#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009082
Hanno Becker73f4cb12019-06-27 13:51:07 +01009083#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009084void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009085 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00009086{
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009087 conf->ciphersuite_list[0] = ciphersuites;
9088 conf->ciphersuite_list[1] = ciphersuites;
9089 conf->ciphersuite_list[2] = ciphersuites;
9090 conf->ciphersuite_list[3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009091}
9092
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009093void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02009094 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009095 int major, int minor )
9096{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009097 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009098 return;
9099
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009100 if( mbedtls_ssl_ver_lt( minor, MBEDTLS_SSL_MINOR_VERSION_0 ) ||
9101 mbedtls_ssl_ver_gt( minor, MBEDTLS_SSL_MINOR_VERSION_3 ) )
9102 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009103 return;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009104 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009105
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009106 conf->ciphersuite_list[mbedtls_ssl_minor_ver_index( minor )] =
9107 ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00009108}
Hanno Becker73f4cb12019-06-27 13:51:07 +01009109#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009111#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009112void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01009113 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009114{
9115 conf->cert_profile = profile;
9116}
9117
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009118/* Append a new keycert entry to a (possibly empty) list */
9119static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
9120 mbedtls_x509_crt *cert,
9121 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009122{
niisato8ee24222018-06-25 19:05:48 +09009123 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009124
niisato8ee24222018-06-25 19:05:48 +09009125 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
9126 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009127 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009128
niisato8ee24222018-06-25 19:05:48 +09009129 new_cert->cert = cert;
9130 new_cert->key = key;
9131 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009132
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009133 /* Update head is the list was null, else add to the end */
9134 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01009135 {
niisato8ee24222018-06-25 19:05:48 +09009136 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01009137 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009138 else
9139 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009140 mbedtls_ssl_key_cert *cur = *head;
9141 while( cur->next != NULL )
9142 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09009143 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009144 }
9145
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009146 return( 0 );
9147}
9148
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009149int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009150 mbedtls_x509_crt *own_cert,
9151 mbedtls_pk_context *pk_key )
9152{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02009153 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009154}
9155
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009156void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009157 mbedtls_x509_crt *ca_chain,
9158 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009159{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009160 conf->ca_chain = ca_chain;
9161 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00009162}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009163#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00009164
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009165#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9166int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
9167 mbedtls_x509_crt *own_cert,
9168 mbedtls_pk_context *pk_key )
9169{
9170 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
9171 own_cert, pk_key ) );
9172}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02009173
9174void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
9175 mbedtls_x509_crt *ca_chain,
9176 mbedtls_x509_crl *ca_crl )
9177{
9178 ssl->handshake->sni_ca_chain = ca_chain;
9179 ssl->handshake->sni_ca_crl = ca_crl;
9180}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02009181
9182void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
9183 int authmode )
9184{
9185 ssl->handshake->sni_authmode = authmode;
9186}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009187#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9188
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009189#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009190/*
9191 * Set EC J-PAKE password for current handshake
9192 */
9193int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
9194 const unsigned char *pw,
9195 size_t pw_len )
9196{
9197 mbedtls_ecjpake_role role;
9198
Janos Follath8eb64132016-06-03 15:40:57 +01009199 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009200 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9201
Hanno Becker2d9623f2019-06-13 12:07:05 +01009202 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009203 role = MBEDTLS_ECJPAKE_SERVER;
9204 else
9205 role = MBEDTLS_ECJPAKE_CLIENT;
9206
9207 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
9208 role,
9209 MBEDTLS_MD_SHA256,
9210 MBEDTLS_ECP_DP_SECP256R1,
9211 pw, pw_len ) );
9212}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009213#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009215#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009216int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009217 const unsigned char *psk, size_t psk_len,
9218 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009219{
Paul Bakker6db455e2013-09-18 17:29:31 +02009220 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009221 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02009222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009223 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01009225
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009226 /* Identity len will be encoded on two bytes */
9227 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009228 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009229 {
9230 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9231 }
9232
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009233 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02009234 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009235 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009236
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009237 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009238 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009239 conf->psk_len = 0;
9240 }
9241 if( conf->psk_identity != NULL )
9242 {
9243 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009244 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009245 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02009246 }
9247
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009248 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
9249 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05009250 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009251 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009252 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009253 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009254 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009255 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05009256 }
Paul Bakker6db455e2013-09-18 17:29:31 +02009257
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009258 conf->psk_len = psk_len;
9259 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02009260
Teppo Järvelin91d79382019-10-02 09:09:31 +03009261 mbedtls_platform_memcpy( conf->psk, psk, conf->psk_len );
9262 mbedtls_platform_memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02009263
9264 return( 0 );
9265}
9266
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009267int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9268 const unsigned char *psk, size_t psk_len )
9269{
9270 if( psk == NULL || ssl->handshake == NULL )
9271 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9272
9273 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9274 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9275
9276 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009277 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009278 mbedtls_platform_zeroize( ssl->handshake->psk,
9279 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01009280 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009281 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009282 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009283
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009284 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009285 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009286
9287 ssl->handshake->psk_len = psk_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03009288 mbedtls_platform_memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009289
9290 return( 0 );
9291}
9292
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009293void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009294 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009295 size_t),
9296 void *p_psk )
9297{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009298 conf->f_psk = f_psk;
9299 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009301#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009302
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009303#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009304
9305#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009306int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009307{
9308 int ret;
9309
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009310 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9311 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9312 {
9313 mbedtls_mpi_free( &conf->dhm_P );
9314 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009315 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009316 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009317
9318 return( 0 );
9319}
Hanno Becker470a8c42017-10-04 15:28:46 +01009320#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009321
Hanno Beckera90658f2017-10-04 15:29:08 +01009322int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9323 const unsigned char *dhm_P, size_t P_len,
9324 const unsigned char *dhm_G, size_t G_len )
9325{
9326 int ret;
9327
9328 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9329 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9330 {
9331 mbedtls_mpi_free( &conf->dhm_P );
9332 mbedtls_mpi_free( &conf->dhm_G );
9333 return( ret );
9334 }
9335
9336 return( 0 );
9337}
Paul Bakker5121ce52009-01-03 21:22:43 +00009338
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009339int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009340{
9341 int ret;
9342
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009343 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9344 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9345 {
9346 mbedtls_mpi_free( &conf->dhm_P );
9347 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009348 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009349 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009350
9351 return( 0 );
9352}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009353#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009354
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009355#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9356/*
9357 * Set the minimum length for Diffie-Hellman parameters
9358 */
9359void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9360 unsigned int bitlen )
9361{
9362 conf->dhm_min_bitlen = bitlen;
9363}
9364#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9365
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009366#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009367/*
9368 * Set allowed/preferred hashes for handshake signatures
9369 */
9370void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9371 const int *hashes )
9372{
Hanno Becker56595f42019-06-19 16:31:38 +01009373#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009374 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01009375#else
9376 ((void) conf);
9377 ((void) hashes);
9378#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009379}
Hanno Becker947194e2017-04-07 13:25:49 +01009380#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009381
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009382#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01009383#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009384/*
9385 * Set the allowed elliptic curves
9386 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009387void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009388 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009389{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009390 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009391}
Hanno Beckerc1096e72019-06-19 12:30:41 +01009392#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01009393#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009394
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009395#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009396int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009397{
Hanno Becker947194e2017-04-07 13:25:49 +01009398 /* Initialize to suppress unnecessary compiler warning */
9399 size_t hostname_len = 0;
9400
9401 /* Check if new hostname is valid before
9402 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009403 if( hostname != NULL )
9404 {
9405 hostname_len = strlen( hostname );
9406
9407 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9408 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9409 }
9410
9411 /* Now it's clear that we will overwrite the old hostname,
9412 * so we can free it safely */
9413
9414 if( ssl->hostname != NULL )
9415 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009416 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009417 mbedtls_free( ssl->hostname );
9418 }
9419
9420 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009421
Paul Bakker5121ce52009-01-03 21:22:43 +00009422 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009423 {
9424 ssl->hostname = NULL;
9425 }
9426 else
9427 {
9428 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009429 if( ssl->hostname == NULL )
9430 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009431
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03009432 /* Not using more secure mbedtls_platform_memcpy as hostname is public in initial handshake */
9433 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009434
Hanno Becker947194e2017-04-07 13:25:49 +01009435 ssl->hostname[hostname_len] = '\0';
9436 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009437
9438 return( 0 );
9439}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009440#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009441
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009442#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009443void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009444 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009445 const unsigned char *, size_t),
9446 void *p_sni )
9447{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009448 conf->f_sni = f_sni;
9449 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009450}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009451#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009453#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009454int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009455{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009456 size_t cur_len, tot_len;
9457 const char **p;
9458
9459 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009460 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9461 * MUST NOT be truncated."
9462 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009463 */
9464 tot_len = 0;
9465 for( p = protos; *p != NULL; p++ )
9466 {
9467 cur_len = strlen( *p );
9468 tot_len += cur_len;
9469
9470 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009471 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009472 }
9473
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009474 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009475
9476 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009477}
9478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009479const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009480{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009481 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009482}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009483#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009484
Hanno Becker33b9b252019-07-05 11:23:25 +01009485#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9486 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9487void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9488 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009489{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009490 conf->max_major_ver = major;
9491 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009492}
Hanno Becker33b9b252019-07-05 11:23:25 +01009493#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9494 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009495
Hanno Becker33b9b252019-07-05 11:23:25 +01009496#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9497 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9498void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9499 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009500{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009501 conf->min_major_ver = major;
9502 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009503}
Hanno Becker33b9b252019-07-05 11:23:25 +01009504#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9505 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009508void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009509{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009510 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009511}
9512#endif
9513
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009514#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009515void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9516 char cert_req_ca_list )
9517{
9518 conf->cert_req_ca_list = cert_req_ca_list;
9519}
9520#endif
9521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009523void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009524{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009525 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009526}
9527#endif
9528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009529#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009530#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009531void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009532{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009533 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009534}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009535#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9536#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009537void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009538 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009539{
9540 conf->enforce_extended_master_secret = ems_enf;
9541}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009542#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009543#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009544
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009545#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009546void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009547{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009548 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009549}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009550#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009552#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009553int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009554{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009555 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009556 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009559 }
9560
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009561 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009562
9563 return( 0 );
9564}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009567#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009568void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009569{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009570 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009571}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009572#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009575void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009576{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009577 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009578}
9579#endif
9580
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009581#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009582void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009583{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009584 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009585}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009586#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009588#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009589void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009590{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009591 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009592}
9593
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009594void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009595{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009596 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009597}
9598
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009599void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009600 const unsigned char period[8] )
9601{
Teppo Järvelin91d79382019-10-02 09:09:31 +03009602 mbedtls_platform_memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009603}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009606#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009607#if defined(MBEDTLS_SSL_CLI_C)
9608void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009609{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009610 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009611}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009612#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009613
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009614#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009615void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9616 mbedtls_ssl_ticket_write_t *f_ticket_write,
9617 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9618 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009619{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009620 conf->f_ticket_write = f_ticket_write;
9621 conf->f_ticket_parse = f_ticket_parse;
9622 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009623}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009624#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009625#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009626
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009627#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9628void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9629 mbedtls_ssl_export_keys_t *f_export_keys,
9630 void *p_export_keys )
9631{
9632 conf->f_export_keys = f_export_keys;
9633 conf->p_export_keys = p_export_keys;
9634}
9635#endif
9636
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009637#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009638void mbedtls_ssl_conf_async_private_cb(
9639 mbedtls_ssl_config *conf,
9640 mbedtls_ssl_async_sign_t *f_async_sign,
9641 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9642 mbedtls_ssl_async_resume_t *f_async_resume,
9643 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009644 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009645{
9646 conf->f_async_sign_start = f_async_sign;
9647 conf->f_async_decrypt_start = f_async_decrypt;
9648 conf->f_async_resume = f_async_resume;
9649 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009650 conf->p_async_config_data = async_config_data;
9651}
9652
Gilles Peskine8f97af72018-04-26 11:46:10 +02009653void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9654{
9655 return( conf->p_async_config_data );
9656}
9657
Gilles Peskine1febfef2018-04-30 11:54:39 +02009658void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009659{
9660 if( ssl->handshake == NULL )
9661 return( NULL );
9662 else
9663 return( ssl->handshake->user_async_ctx );
9664}
9665
Gilles Peskine1febfef2018-04-30 11:54:39 +02009666void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009667 void *ctx )
9668{
9669 if( ssl->handshake != NULL )
9670 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009671}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009672#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009673
Paul Bakker5121ce52009-01-03 21:22:43 +00009674/*
9675 * SSL get accessors
9676 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009677size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009678{
9679 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9680}
9681
Hanno Becker8b170a02017-10-10 11:51:19 +01009682int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9683{
9684 /*
9685 * Case A: We're currently holding back
9686 * a message for further processing.
9687 */
9688
9689 if( ssl->keep_current_message == 1 )
9690 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009691 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009692 return( 1 );
9693 }
9694
9695 /*
9696 * Case B: Further records are pending in the current datagram.
9697 */
9698
9699#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009700 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009701 ssl->in_left > ssl->next_record_offset )
9702 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009703 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009704 return( 1 );
9705 }
9706#endif /* MBEDTLS_SSL_PROTO_DTLS */
9707
9708 /*
9709 * Case C: A handshake message is being processed.
9710 */
9711
Hanno Becker8b170a02017-10-10 11:51:19 +01009712 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9713 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009714 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009715 return( 1 );
9716 }
9717
9718 /*
9719 * Case D: An application data message is being processed
9720 */
9721 if( ssl->in_offt != NULL )
9722 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009723 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009724 return( 1 );
9725 }
9726
9727 /*
9728 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009729 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009730 * we implement support for multiple alerts in single records.
9731 */
9732
9733 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9734 return( 0 );
9735}
9736
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009737uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009738{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009739 if( ssl->session != NULL )
9740 return( ssl->session->verify_result );
9741
9742 if( ssl->session_negotiate != NULL )
9743 return( ssl->session_negotiate->verify_result );
9744
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009745 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009746}
9747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009748const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009749{
Hanno Beckere02758c2019-06-26 15:31:31 +01009750 int suite;
9751
Paul Bakker926c8e42013-03-06 10:23:34 +01009752 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009753 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009754
Hanno Beckere02758c2019-06-26 15:31:31 +01009755 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9756 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009757}
9758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009759const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009760{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009761#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009762 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009763 {
Hanno Becker2881d802019-05-22 14:44:53 +01009764 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009766 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009767 return( "DTLSv1.0" );
9768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009770 return( "DTLSv1.2" );
9771
9772 default:
9773 return( "unknown (DTLS)" );
9774 }
9775 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009776 MBEDTLS_SSL_TRANSPORT_ELSE
9777#endif /* MBEDTLS_SSL_PROTO_DTLS */
9778#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009779 {
Hanno Becker2881d802019-05-22 14:44:53 +01009780 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009781 {
9782 case MBEDTLS_SSL_MINOR_VERSION_0:
9783 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009784
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009785 case MBEDTLS_SSL_MINOR_VERSION_1:
9786 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009787
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009788 case MBEDTLS_SSL_MINOR_VERSION_2:
9789 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009790
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009791 case MBEDTLS_SSL_MINOR_VERSION_3:
9792 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009793
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009794 default:
9795 return( "unknown" );
9796 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009797 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009798#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009799}
9800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009802{
Hanno Becker3136ede2018-08-17 15:28:19 +01009803 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009804 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009805
Hanno Becker43395762019-05-03 14:46:38 +01009806 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9807
Hanno Becker78640902018-08-13 16:35:15 +01009808 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009809 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009811#if defined(MBEDTLS_ZLIB_SUPPORT)
9812 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9813 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009814#endif
9815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009816 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009817 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009818#if defined(MBEDTLS_GCM_C) || \
9819 defined(MBEDTLS_CCM_C) || \
9820 defined(MBEDTLS_CHACHAPOLY_C)
9821#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009823#endif
9824#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009826#endif
9827#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009828 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009829#endif
9830 transform_expansion =
9831 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009832 break;
9833
Hanno Beckera9d5c452019-07-25 16:47:12 +01009834#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9835 MBEDTLS_CHACHAPOLY_C */
9836
9837#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9838 case MBEDTLS_MODE_STREAM:
9839 transform_expansion = transform->maclen;
9840 break;
9841#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9842
9843#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009845 {
9846 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009847
9848 block_size = mbedtls_cipher_get_block_size(
9849 &transform->cipher_ctx_enc );
9850
Hanno Becker3136ede2018-08-17 15:28:19 +01009851 /* Expansion due to the addition of the MAC. */
9852 transform_expansion += transform->maclen;
9853
9854 /* Expansion due to the addition of CBC padding;
9855 * Theoretically up to 256 bytes, but we never use
9856 * more than the block size of the underlying cipher. */
9857 transform_expansion += block_size;
9858
9859 /* For TLS 1.1 or higher, an explicit IV is added
9860 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009861#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009862 if( mbedtls_ssl_ver_geq(
9863 mbedtls_ssl_get_minor_ver( ssl ),
9864 MBEDTLS_SSL_MINOR_VERSION_2 ) )
9865 {
Hanno Becker3136ede2018-08-17 15:28:19 +01009866 transform_expansion += block_size;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009867 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01009868#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009869
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009870 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009871 }
9872#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009873
9874 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009876 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009877 }
9878
Hanno Beckera5a2b082019-05-15 14:03:01 +01009879#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009880 if( transform->out_cid_len != 0 )
9881 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009882#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009883
Hanno Becker43395762019-05-03 14:46:38 +01009884 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009885}
9886
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009887#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9888size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9889{
9890 size_t max_len;
9891
9892 /*
9893 * Assume mfl_code is correct since it was checked when set
9894 */
Angus Grattond8213d02016-05-25 20:56:48 +10009895 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009896
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009897 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009898 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009899 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009900 {
Angus Grattond8213d02016-05-25 20:56:48 +10009901 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009902 }
9903
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009904 /* During a handshake, use the value being negotiated */
9905 if( ssl->session_negotiate != NULL &&
9906 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9907 {
9908 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9909 }
9910
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009911 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009912}
9913#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9914
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009915#if defined(MBEDTLS_SSL_PROTO_DTLS)
9916static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9917{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009918 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009919 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009920 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9921 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009922 return( 0 );
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009923
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009924 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9925 return( ssl->mtu );
9926
9927 if( ssl->mtu == 0 )
9928 return( ssl->handshake->mtu );
9929
9930 return( ssl->mtu < ssl->handshake->mtu ?
9931 ssl->mtu : ssl->handshake->mtu );
9932}
9933#endif /* MBEDTLS_SSL_PROTO_DTLS */
9934
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009935int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9936{
9937 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9938
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009939#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9940 !defined(MBEDTLS_SSL_PROTO_DTLS)
9941 (void) ssl;
9942#endif
9943
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009944#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9945 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9946
9947 if( max_len > mfl )
9948 max_len = mfl;
9949#endif
9950
9951#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009952 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009953 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009954 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009955 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9956 const size_t overhead = (size_t) ret;
9957
9958 if( ret < 0 )
9959 return( ret );
9960
9961 if( mtu <= overhead )
9962 {
9963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9964 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9965 }
9966
9967 if( max_len > mtu - overhead )
9968 max_len = mtu - overhead;
9969 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009970#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009971
Hanno Becker0defedb2018-08-10 12:35:02 +01009972#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9973 !defined(MBEDTLS_SSL_PROTO_DTLS)
9974 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009975#endif
9976
9977 return( (int) max_len );
9978}
9979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009980#if defined(MBEDTLS_X509_CRT_PARSE_C)
9981const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009982{
9983 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009984 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009985
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009986#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009987 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009988#else
9989 return( NULL );
9990#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009991}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009992#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009994#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009995int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9996 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009997{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009998 if( ssl == NULL ||
9999 dst == NULL ||
10000 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +010010001 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010003 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010004 }
10005
Hanno Becker58fccf22019-02-06 14:30:46 +000010006 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010007}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010008#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010009
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +020010010const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
10011{
10012 if( ssl == NULL )
10013 return( NULL );
10014
10015 return( ssl->session );
10016}
10017
Paul Bakker5121ce52009-01-03 21:22:43 +000010018/*
Hanno Beckerb5352f02019-05-16 12:39:07 +010010019 * Define ticket header determining Mbed TLS version
10020 * and structure of the ticket.
10021 */
10022
Hanno Becker41527622019-05-16 12:50:45 +010010023/*
Hanno Becker26829e92019-05-28 14:30:45 +010010024 * Define bitflag determining compile-time settings influencing
10025 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +010010026 */
10027
Hanno Becker26829e92019-05-28 14:30:45 +010010028#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010029#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +010010030#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010031#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +010010032#endif /* MBEDTLS_HAVE_TIME */
10033
10034#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010035#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +010010036#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010037#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +010010038#endif /* MBEDTLS_X509_CRT_PARSE_C */
10039
10040#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010041#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +010010042#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010043#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +010010044#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
10045
10046#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010047#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +010010048#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010049#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +010010050#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
10051
10052#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010053#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +010010054#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010055#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +010010056#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
10057
10058#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010059#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +010010060#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010061#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +010010062#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
10063
Hanno Becker41527622019-05-16 12:50:45 +010010064#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10065#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
10066#else
10067#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
10068#endif /* MBEDTLS_SSL_SESSION_TICKETS */
10069
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010070#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10071#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
10072#else
10073#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
10074#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10075
Hanno Becker88440552019-07-03 14:16:13 +010010076#if defined(MBEDTLS_ZLIB_SUPPORT)
10077#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
10078#else
10079#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
10080#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10081
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010082#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
10083#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
10084#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
10085#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
10086#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
10087#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
10088#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010089#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +010010090#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010091
Hanno Becker26829e92019-05-28 14:30:45 +010010092#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010093 ( (uint16_t) ( \
10094 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
10095 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
10096 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
10097 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
10098 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
10099 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010100 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +010010101 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010102 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +010010103
Hanno Becker557fe9f2019-05-16 12:41:07 +010010104static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +010010105 MBEDTLS_VERSION_MAJOR,
10106 MBEDTLS_VERSION_MINOR,
10107 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +010010108 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
10109 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +010010110};
Hanno Beckerb5352f02019-05-16 12:39:07 +010010111
10112/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010113 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010114 * (in the presentation language of TLS, RFC 8446 section 3)
10115 *
Hanno Becker26829e92019-05-28 14:30:45 +010010116 * opaque mbedtls_version[3]; // major, minor, patch
10117 * opaque session_format[2]; // version-specific 16-bit field determining
10118 * // the format of the remaining
10119 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +010010120 *
10121 * Note: When updating the format, remember to keep
10122 * these version+format bytes.
10123 *
Hanno Becker7bf77102019-06-04 09:43:16 +010010124 * // In this version, `session_format` determines
10125 * // the setting of those compile-time
10126 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +010010127 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010128 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +010010129 * uint8 ciphersuite[2]; // defined by the standard
10130 * uint8 compression; // 0 or 1
10131 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010132 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +010010133 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010134 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +010010135 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
10136 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
10137 * case disabled: uint8_t peer_cert_digest_type;
10138 * opaque peer_cert_digest<0..2^8-1>;
10139 * }
Hanno Becker26829e92019-05-28 14:30:45 +010010140 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010141 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +010010142 * uint8 mfl_code; // up to 255 according to standard
10143 * uint8 trunc_hmac; // 0 or 1
10144 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010145 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010146 * The order is the same as in the definition of the structure, except
10147 * verify_result is put before peer_cert so that all mandatory fields come
10148 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010149 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010150static int ssl_session_save( const mbedtls_ssl_session *session,
10151 unsigned char omit_header,
10152 unsigned char *buf,
10153 size_t buf_len,
10154 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010155{
10156 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010157 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010158#if defined(MBEDTLS_HAVE_TIME)
10159 uint64_t start;
10160#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010161#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010162#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010163 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010164#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +000010165#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010166
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010167 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010168 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010169 /*
10170 * Add version identifier
10171 */
10172
10173 used += sizeof( ssl_serialized_session_header );
10174
10175 if( used <= buf_len )
10176 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010177 mbedtls_platform_memcpy( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010178 sizeof( ssl_serialized_session_header ) );
10179 p += sizeof( ssl_serialized_session_header );
10180 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010181 }
10182
10183 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010184 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010185 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010186#if defined(MBEDTLS_HAVE_TIME)
10187 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010188
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010189 if( used <= buf_len )
10190 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010191 start = (uint64_t) session->start;
10192
10193 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
10194 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
10195 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
10196 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
10197 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
10198 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
10199 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
10200 *p++ = (unsigned char)( ( start ) & 0xFF );
10201 }
10202#endif /* MBEDTLS_HAVE_TIME */
10203
10204 /*
10205 * Basic mandatory fields
10206 */
Hanno Becker88440552019-07-03 14:16:13 +010010207 {
10208 size_t const ciphersuite_len = 2;
10209#if defined(MBEDTLS_ZLIB_SUPPORT)
10210 size_t const compression_len = 1;
10211#else
10212 size_t const compression_len = 0;
10213#endif
10214 size_t const id_len_len = 1;
10215 size_t const id_len = 32;
10216 size_t const master_len = 48;
10217 size_t const verif_result_len = 4;
10218
10219 size_t const basic_len =
10220 ciphersuite_len +
10221 compression_len +
10222 id_len_len +
10223 id_len +
10224 master_len +
10225 verif_result_len;
10226
10227 used += basic_len;
10228 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010229
10230 if( used <= buf_len )
10231 {
Hanno Beckere02758c2019-06-26 15:31:31 +010010232 const int ciphersuite =
10233 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010234 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010235
Hanno Becker88440552019-07-03 14:16:13 +010010236#if defined(MBEDTLS_ZLIB_SUPPORT)
10237 *p++ = (unsigned char)(
10238 mbedtls_ssl_session_get_compression( session ) );
10239#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010240
10241 *p++ = (unsigned char)( session->id_len & 0xFF );
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010242 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10243 memcpy( p, session->id, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010244 p += 32;
10245
Teppo Järvelin91d79382019-10-02 09:09:31 +030010246 mbedtls_platform_memcpy( p, session->master, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010247 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010248 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010249 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010250
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010251 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010252 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010253 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010254#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010255#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010256 if( session->peer_cert == NULL )
10257 cert_len = 0;
10258 else
10259 cert_len = session->peer_cert->raw.len;
10260
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010261 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010262
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010263 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010264 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010265 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010266
10267 if( session->peer_cert != NULL )
10268 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010269 mbedtls_platform_memcpy( p, session->peer_cert->raw.p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010270 p += cert_len;
10271 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010272 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010273
Hanno Becker5882dd02019-06-06 16:25:57 +010010274#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010275 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010276 if( session->peer_cert_digest != NULL )
10277 {
10278 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
10279 if( used <= buf_len )
10280 {
10281 *p++ = (unsigned char) session->peer_cert_digest_type;
10282 *p++ = (unsigned char) session->peer_cert_digest_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +030010283 mbedtls_platform_memcpy( p, session->peer_cert_digest,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010284 session->peer_cert_digest_len );
10285 p += session->peer_cert_digest_len;
10286 }
10287 }
10288 else
10289 {
10290 used += 2;
10291 if( used <= buf_len )
10292 {
10293 *p++ = (unsigned char) MBEDTLS_MD_NONE;
10294 *p++ = 0;
10295 }
10296 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010297#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010298#endif /* MBEDTLS_X509_CRT_PARSE_C */
10299
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010300 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010301 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010302 */
10303#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010304 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010305
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010306 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010307 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010308 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010309
10310 if( session->ticket != NULL )
10311 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010312 mbedtls_platform_memcpy( p, session->ticket, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010313 p += session->ticket_len;
10314 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010315
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010316 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010317 }
10318#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10319
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010320 /*
10321 * Misc extension-related info
10322 */
10323#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10324 used += 1;
10325
10326 if( used <= buf_len )
10327 *p++ = session->mfl_code;
10328#endif
10329
10330#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10331 used += 1;
10332
10333 if( used <= buf_len )
10334 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
10335#endif
10336
10337#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10338 used += 1;
10339
10340 if( used <= buf_len )
10341 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
10342#endif
10343
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010344 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010345 *olen = used;
10346
10347 if( used > buf_len )
10348 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010349
10350 return( 0 );
10351}
10352
10353/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010354 * Public wrapper for ssl_session_save()
10355 */
10356int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
10357 unsigned char *buf,
10358 size_t buf_len,
10359 size_t *olen )
10360{
10361 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
10362}
10363
10364/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010365 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010366 *
10367 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010368 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010369 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010370static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010371 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010372 const unsigned char *buf,
10373 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010374{
10375 const unsigned char *p = buf;
10376 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +010010377 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010378#if defined(MBEDTLS_HAVE_TIME)
10379 uint64_t start;
10380#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010381#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010382#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010383 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +000010384#endif
10385#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010386
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010387 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010388 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010389 /*
10390 * Check version identifier
10391 */
10392
10393 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
10394 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10395
Teppo Järvelin0efac532019-10-04 13:21:08 +030010396 // use regular memcmp as session header is public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010397 if( memcmp( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010398 sizeof( ssl_serialized_session_header ) ) != 0 )
10399 {
10400 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10401 }
10402 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +010010403 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010404
10405 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010406 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010407 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010408#if defined(MBEDTLS_HAVE_TIME)
10409 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010410 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10411
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010412 start = ( (uint64_t) p[0] << 56 ) |
10413 ( (uint64_t) p[1] << 48 ) |
10414 ( (uint64_t) p[2] << 40 ) |
10415 ( (uint64_t) p[3] << 32 ) |
10416 ( (uint64_t) p[4] << 24 ) |
10417 ( (uint64_t) p[5] << 16 ) |
10418 ( (uint64_t) p[6] << 8 ) |
10419 ( (uint64_t) p[7] );
10420 p += 8;
10421
10422 session->start = (time_t) start;
10423#endif /* MBEDTLS_HAVE_TIME */
10424
10425 /*
10426 * Basic mandatory fields
10427 */
Hanno Becker88440552019-07-03 14:16:13 +010010428 {
10429 size_t const ciphersuite_len = 2;
10430#if defined(MBEDTLS_ZLIB_SUPPORT)
10431 size_t const compression_len = 1;
10432#else
10433 size_t const compression_len = 0;
10434#endif
10435 size_t const id_len_len = 1;
10436 size_t const id_len = 32;
10437 size_t const master_len = 48;
10438 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010439
Hanno Becker88440552019-07-03 14:16:13 +010010440 size_t const basic_len =
10441 ciphersuite_len +
10442 compression_len +
10443 id_len_len +
10444 id_len +
10445 master_len +
10446 verif_result_len;
10447
10448 if( basic_len > (size_t)( end - p ) )
10449 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10450 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010451
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010452 ciphersuite = (int)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010453 p += 2;
10454
Hanno Becker73f4cb12019-06-27 13:51:07 +010010455#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010456 session->ciphersuite = ciphersuite;
10457#else
10458 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010459 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010460 {
10461 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10462 }
10463#endif
10464
Hanno Becker88440552019-07-03 14:16:13 +010010465#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010466 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010467#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010468
10469 session->id_len = *p++;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010470 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10471 memcpy( session->id, p, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010472 p += 32;
10473
Teppo Järvelin91d79382019-10-02 09:09:31 +030010474 mbedtls_platform_memcpy( session->master, p, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010475 p += 48;
10476
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010477 session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010478 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010479
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010480 /* Immediately clear invalid pointer values that have been read, in case
10481 * we exit early before we replaced them with valid ones. */
10482#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010483#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010484 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010485#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010486 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010487#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010488#endif
10489#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10490 session->ticket = NULL;
10491#endif
10492
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010493 /*
10494 * Peer certificate
10495 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010496#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010497#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010498 if( 3 > (size_t)( end - p ) )
10499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10500
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010501 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10502
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010503 p += 3;
10504
10505 if( cert_len == 0 )
10506 {
10507 session->peer_cert = NULL;
10508 }
10509 else
10510 {
10511 int ret;
10512
10513 if( cert_len > (size_t)( end - p ) )
10514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10515
10516 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10517
10518 if( session->peer_cert == NULL )
10519 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10520
10521 mbedtls_x509_crt_init( session->peer_cert );
10522
10523 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10524 p, cert_len ) ) != 0 )
10525 {
10526 mbedtls_x509_crt_free( session->peer_cert );
10527 mbedtls_free( session->peer_cert );
10528 session->peer_cert = NULL;
10529 return( ret );
10530 }
10531
10532 p += cert_len;
10533 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010534#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010535 /* Deserialize CRT digest from the end of the ticket. */
10536 if( 2 > (size_t)( end - p ) )
10537 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10538
10539 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10540 session->peer_cert_digest_len = (size_t) *p++;
10541
10542 if( session->peer_cert_digest_len != 0 )
10543 {
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010544 mbedtls_md_handle_t md_info =
Hanno Becker2326d202019-06-06 14:54:55 +010010545 mbedtls_md_info_from_type( session->peer_cert_digest_type );
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010546 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Hanno Becker2326d202019-06-06 14:54:55 +010010547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10548 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10549 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10550
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010551 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10553
10554 session->peer_cert_digest =
10555 mbedtls_calloc( 1, session->peer_cert_digest_len );
10556 if( session->peer_cert_digest == NULL )
10557 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10558
Teppo Järvelin91d79382019-10-02 09:09:31 +030010559 mbedtls_platform_memcpy( session->peer_cert_digest, p,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010560 session->peer_cert_digest_len );
10561 p += session->peer_cert_digest_len;
10562 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010563#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010564#endif /* MBEDTLS_X509_CRT_PARSE_C */
10565
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010566 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010567 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010568 */
10569#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10570 if( 3 > (size_t)( end - p ) )
10571 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10572
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010573 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010574 p += 3;
10575
10576 if( session->ticket_len != 0 )
10577 {
10578 if( session->ticket_len > (size_t)( end - p ) )
10579 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10580
10581 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10582 if( session->ticket == NULL )
10583 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10584
Teppo Järvelin91d79382019-10-02 09:09:31 +030010585 mbedtls_platform_memcpy( session->ticket, p, session->ticket_len );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010586 p += session->ticket_len;
10587 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010588
10589 if( 4 > (size_t)( end - p ) )
10590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10591
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010592 session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010593 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010594#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10595
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010596 /*
10597 * Misc extension-related info
10598 */
10599#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10600 if( 1 > (size_t)( end - p ) )
10601 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10602
10603 session->mfl_code = *p++;
10604#endif
10605
10606#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10607 if( 1 > (size_t)( end - p ) )
10608 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10609
10610 session->trunc_hmac = *p++;
10611#endif
10612
10613#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10614 if( 1 > (size_t)( end - p ) )
10615 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10616
10617 session->encrypt_then_mac = *p++;
10618#endif
10619
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010620 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010621 if( p != end )
10622 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10623
10624 return( 0 );
10625}
10626
10627/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010628 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010629 */
10630int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10631 const unsigned char *buf,
10632 size_t len )
10633{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010634 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010635
10636 if( ret != 0 )
10637 mbedtls_ssl_session_free( session );
10638
10639 return( ret );
10640}
10641
10642/*
Paul Bakker1961b702013-01-25 14:49:24 +010010643 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010645int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010646{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010647 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010648
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010649 if( ssl == NULL || ssl->conf == NULL )
10650 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010652#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010653 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010654 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010655#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010656#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010657 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010658 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010659#endif
10660
Hanno Beckerb82350b2019-07-26 07:24:05 +010010661 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010662 return( ret );
10663}
10664
10665/*
10666 * Perform the SSL handshake
10667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010668int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010669{
10670 int ret = 0;
10671
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010672 if( ssl == NULL || ssl->conf == NULL )
10673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010677 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010679 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010680
10681 if( ret != 0 )
10682 break;
10683 }
10684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010686
10687 return( ret );
10688}
10689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010690#if defined(MBEDTLS_SSL_RENEGOTIATION)
10691#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010692/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010693 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010695static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010696{
10697 int ret;
10698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010700
10701 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010702 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10703 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010704
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010705 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010706 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010707 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010708 return( ret );
10709 }
10710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010712
10713 return( 0 );
10714}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010715#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010716
10717/*
10718 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010719 * - any side: calling mbedtls_ssl_renegotiate(),
10720 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10721 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010722 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010723 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010724 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010726static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010727{
10728 int ret;
10729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010731
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010732 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10733 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010734
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010735 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10736 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010737#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010738 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010739 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010740 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010741 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10742 MBEDTLS_SSL_IS_SERVER )
10743 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010744 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010745 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010746 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010747 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010748 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010749 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010750 }
10751#endif
10752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010753 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10754 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010756 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010758 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010759 return( ret );
10760 }
10761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010763
10764 return( 0 );
10765}
10766
10767/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010768 * Renegotiate current connection on client,
10769 * or request renegotiation on server
10770 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010771int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010772{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010773 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010774
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010775 if( ssl == NULL || ssl->conf == NULL )
10776 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010778#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010779 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010780 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010782 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10783 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010785 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010786
10787 /* Did we already try/start sending HelloRequest? */
10788 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010789 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010790
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010791 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010792 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010793#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010795#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010796 /*
10797 * On client, either start the renegotiation process or,
10798 * if already in progress, continue the handshake
10799 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010800 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010802 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10803 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010804
10805 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010807 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010808 return( ret );
10809 }
10810 }
10811 else
10812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010813 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010816 return( ret );
10817 }
10818 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010819#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010820
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010821 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010822}
10823
10824/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010825 * Check record counters and renegotiate if they're above the limit.
10826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010827static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010828{
Andres AG2196c7f2016-12-15 17:01:16 +000010829 size_t ep_len = ssl_ep_len( ssl );
10830 int in_ctr_cmp;
10831 int out_ctr_cmp;
10832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010833 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10834 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010835 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010836 {
10837 return( 0 );
10838 }
10839
Teppo Järvelin0efac532019-10-04 13:21:08 +030010840 // use regular memcmp as counters are public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010841 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010842 ssl->conf->renego_period + ep_len, 8 - ep_len );
Teppo Järvelin650343c2019-10-03 15:36:59 +030010843 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010844 ssl->conf->renego_period + ep_len, 8 - ep_len );
10845
10846 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010847 {
10848 return( 0 );
10849 }
10850
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010852 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010853}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010854#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010855
10856/*
10857 * Receive application data decrypted from the SSL layer
10858 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010859int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010860{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010861 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010862 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010863
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010864 if( ssl == NULL || ssl->conf == NULL )
10865 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010869#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010870 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010872 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010873 return( ret );
10874
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010875 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010876 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010877 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010878 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010879 return( ret );
10880 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010881 }
10882#endif
10883
Hanno Becker4a810fb2017-05-24 16:27:30 +010010884 /*
10885 * Check if renegotiation is necessary and/or handshake is
10886 * in process. If yes, perform/continue, and fall through
10887 * if an unexpected packet is received while the client
10888 * is waiting for the ServerHello.
10889 *
10890 * (There is no equivalent to the last condition on
10891 * the server-side as it is not treated as within
10892 * a handshake while waiting for the ClientHello
10893 * after a renegotiation request.)
10894 */
10895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010896#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010897 ret = ssl_check_ctr_renegotiate( ssl );
10898 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10899 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010901 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010902 return( ret );
10903 }
10904#endif
10905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010906 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010908 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010909 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10910 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010912 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010913 return( ret );
10914 }
10915 }
10916
Hanno Beckere41158b2017-10-23 13:30:32 +010010917 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010918 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010919 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010920 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010921 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10922 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010923 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010924 ssl_set_timer( ssl,
10925 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010926 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010927
Hanno Becker327c93b2018-08-15 13:56:18 +010010928 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010929 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010930 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10931 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010932
Hanno Becker4a810fb2017-05-24 16:27:30 +010010933 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10934 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010935 }
10936
10937 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010938 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010939 {
10940 /*
10941 * OpenSSL sends empty messages to randomize the IV
10942 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010943 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010945 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010946 return( 0 );
10947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010948 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010949 return( ret );
10950 }
10951 }
10952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010953 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010956
Hanno Becker4a810fb2017-05-24 16:27:30 +010010957 /*
10958 * - For client-side, expect SERVER_HELLO_REQUEST.
10959 * - For server-side, expect CLIENT_HELLO.
10960 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10961 */
10962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010963#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010964 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10965 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010966 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010967 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010970
10971 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010972#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010973 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010974 {
10975 continue;
10976 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010977 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010978#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010979#if defined(MBEDTLS_SSL_PROTO_TLS)
10980 {
10981 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10982 }
10983#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010984 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010985#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010986
Hanno Becker4a810fb2017-05-24 16:27:30 +010010987#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010988 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10989 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010990 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010993
10994 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010995#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010996 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010997 {
10998 continue;
10999 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020011000 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020011001#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020011002#if defined(MBEDTLS_SSL_PROTO_TLS)
11003 {
11004 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
11005 }
11006#endif
Paul Bakker48916f92012-09-16 19:57:18 +000011007 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010011008#endif /* MBEDTLS_SSL_SRV_C */
11009
Hanno Becker21df7f92017-10-17 11:03:26 +010011010#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010011011 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011012 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
11013 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010011014 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011015 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
11016 {
11017 /*
11018 * Accept renegotiation request
11019 */
Paul Bakker48916f92012-09-16 19:57:18 +000011020
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011021 /* DTLS clients need to know renego is server-initiated */
11022#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011023 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010011024 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11025 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011026 {
11027 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
11028 }
11029#endif
11030 ret = ssl_start_renegotiation( ssl );
11031 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
11032 ret != 0 )
11033 {
11034 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
11035 return( ret );
11036 }
11037 }
11038 else
Hanno Becker21df7f92017-10-17 11:03:26 +010011039#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000011040 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010011041 /*
11042 * Refuse renegotiation
11043 */
11044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011045 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011047#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010011048 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000011049 {
Gilles Peskine92e44262017-05-10 17:27:49 +020011050 /* SSLv3 does not have a "no_renegotiation" warning, so
11051 we send a fatal alert and abort the connection. */
11052 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11053 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
11054 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011055 }
11056 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011057#endif /* MBEDTLS_SSL_PROTO_SSL3 */
11058#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11059 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011060 if( mbedtls_ssl_ver_geq(
11061 mbedtls_ssl_get_minor_ver( ssl ),
11062 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011063 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010011064 ret = mbedtls_ssl_send_alert_message( ssl,
11065 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11066 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
11067 if( ret != 0 )
11068 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000011069 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020011070 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011071#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
11072 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020011073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
11075 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020011076 }
Paul Bakker48916f92012-09-16 19:57:18 +000011077 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011078
Hanno Becker90333da2017-10-10 11:27:13 +010011079 /* At this point, we don't know whether the renegotiation has been
11080 * completed or not. The cases to consider are the following:
11081 * 1) The renegotiation is complete. In this case, no new record
11082 * has been read yet.
11083 * 2) The renegotiation is incomplete because the client received
11084 * an application data record while awaiting the ServerHello.
11085 * 3) The renegotiation is incomplete because the client received
11086 * a non-handshake, non-application data message while awaiting
11087 * the ServerHello.
11088 * In each of these case, looping will be the proper action:
11089 * - For 1), the next iteration will read a new record and check
11090 * if it's application data.
11091 * - For 2), the loop condition isn't satisfied as application data
11092 * is present, hence continue is the same as break
11093 * - For 3), the loop condition is satisfied and read_record
11094 * will re-deliver the message that was held back by the client
11095 * when expecting the ServerHello.
11096 */
11097 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000011098 }
Hanno Becker21df7f92017-10-17 11:03:26 +010011099#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011100 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011101 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011102 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011103 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011104 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011107 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011108 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011109 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011110 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011111 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011112#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011114 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
11115 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010011118 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011119 }
11120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011121 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000011122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
11124 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000011125 }
11126
11127 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020011128
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011129 /* We're going to return something now, cancel timer,
11130 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011131 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011132 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011133
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020011134#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011135 /* If we requested renego but received AppData, resend HelloRequest.
11136 * Do it now, after setting in_offt, to avoid taking this branch
11137 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011138#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010011139 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11140 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011141 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011142 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011143 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011145 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011146 return( ret );
11147 }
11148 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011149#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010011150#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000011151 }
11152
11153 n = ( len < ssl->in_msglen )
11154 ? len : ssl->in_msglen;
11155
Teppo Järvelin91d79382019-10-02 09:09:31 +030011156 mbedtls_platform_memcpy( buf, ssl->in_offt, n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011157 ssl->in_msglen -= n;
11158
Teppo Järvelincafb6c92020-01-08 09:19:07 +020011159 // clear incoming data after it's copied to buffer
11160 mbedtls_platform_memset(ssl->in_offt, 0, n);
11161
Paul Bakker5121ce52009-01-03 21:22:43 +000011162 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010011163 {
11164 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000011165 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010011166 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011167 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011168 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010011169 {
Paul Bakker5121ce52009-01-03 21:22:43 +000011170 /* more data available */
11171 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011172 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011175
Paul Bakker23986e52011-04-24 08:57:21 +000011176 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011177}
11178
11179/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011180 * Send application data to be encrypted by the SSL layer, taking care of max
11181 * fragment length and buffer size.
11182 *
11183 * According to RFC 5246 Section 6.2.1:
11184 *
11185 * Zero-length fragments of Application data MAY be sent as they are
11186 * potentially useful as a traffic analysis countermeasure.
11187 *
11188 * Therefore, it is possible that the input message length is 0 and the
11189 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000011190 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011191static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011192 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000011193{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020011194 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
11195 const size_t max_len = (size_t) ret;
11196
11197 if( ret < 0 )
11198 {
11199 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
11200 return( ret );
11201 }
11202
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011203 if( len > max_len )
11204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011205#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011206 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011209 "maximum fragment length: %d > %d",
11210 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011211 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011212 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011213 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011214#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011215#if defined(MBEDTLS_SSL_PROTO_TLS)
11216 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011217 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011218 }
11219#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011220 }
Paul Bakker887bd502011-06-08 13:10:54 +000011221
Paul Bakker5121ce52009-01-03 21:22:43 +000011222 if( ssl->out_left != 0 )
11223 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011224 /*
11225 * The user has previously tried to send the data and
11226 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
11227 * written. In this case, we expect the high-level write function
11228 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
11229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011230 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011232 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011233 return( ret );
11234 }
11235 }
Paul Bakker887bd502011-06-08 13:10:54 +000011236 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000011237 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011238 /*
11239 * The user is trying to send a message the first time, so we need to
11240 * copy the data into the internal buffers and setup the data structure
11241 * to keep track of partial writes
11242 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011243 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011244 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Teppo Järvelin91d79382019-10-02 09:09:31 +030011245 mbedtls_platform_memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000011246
Hanno Becker67bc7c32018-08-06 11:33:50 +010011247 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000011248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000011250 return( ret );
11251 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011252 }
11253
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011254 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000011255}
11256
11257/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011258 * Write application data, doing 1/n-1 splitting if necessary.
11259 *
11260 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011261 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010011262 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011264#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011265static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011266 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011267{
11268 int ret;
11269
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011270 if( ssl->conf->cbc_record_splitting ==
11271 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011272 len <= 1 ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011273 mbedtls_ssl_ver_gt(
11274 mbedtls_ssl_get_minor_ver( ssl ),
11275 MBEDTLS_SSL_MINOR_VERSION_1 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011276 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
11277 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011278 {
11279 return( ssl_write_real( ssl, buf, len ) );
11280 }
11281
11282 if( ssl->split_done == 0 )
11283 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011284 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011285 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011286 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011287 }
11288
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011289 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
11290 return( ret );
11291 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011292
11293 return( ret + 1 );
11294}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011295#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011296
11297/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011298 * Write application data (public-facing wrapper)
11299 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011300int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011301{
11302 int ret;
11303
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011305
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011306 if( ssl == NULL || ssl->conf == NULL )
11307 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11308
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011309#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011310 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
11311 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011312 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011313 return( ret );
11314 }
11315#endif
11316
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011317 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011318 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011319 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011320 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020011321 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011322 return( ret );
11323 }
11324 }
11325
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011326#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011327 ret = ssl_write_split( ssl, buf, len );
11328#else
11329 ret = ssl_write_real( ssl, buf, len );
11330#endif
11331
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011333
11334 return( ret );
11335}
11336
11337/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011338 * Notify the peer that the connection is being closed
11339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011340int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011341{
11342 int ret;
11343
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011344 if( ssl == NULL || ssl->conf == NULL )
11345 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011348
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011349 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011350 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011352 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000011353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011354 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
11355 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11356 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011358 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011359 return( ret );
11360 }
11361 }
11362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011364
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011365 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000011366}
11367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011368void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000011369{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011370 if( transform == NULL )
11371 return;
11372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011373#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000011374 deflateEnd( &transform->ctx_deflate );
11375 inflateEnd( &transform->ctx_inflate );
11376#endif
11377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011378 mbedtls_cipher_free( &transform->cipher_ctx_enc );
11379 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020011380
Hanno Becker92231322018-01-03 15:32:51 +000011381#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011382 mbedtls_md_free( &transform->md_ctx_enc );
11383 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000011384#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011385
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011386 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011387}
11388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011389#if defined(MBEDTLS_X509_CRT_PARSE_C)
11390static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011391{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011392 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011393
11394 while( cur != NULL )
11395 {
11396 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011397 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011398 cur = next;
11399 }
11400}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011401#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011402
Hanno Becker0271f962018-08-16 13:23:47 +010011403#if defined(MBEDTLS_SSL_PROTO_DTLS)
11404
11405static void ssl_buffering_free( mbedtls_ssl_context *ssl )
11406{
11407 unsigned offset;
11408 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11409
11410 if( hs == NULL )
11411 return;
11412
Hanno Becker283f5ef2018-08-24 09:34:47 +010011413 ssl_free_buffered_record( ssl );
11414
Hanno Becker0271f962018-08-16 13:23:47 +010011415 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010011416 ssl_buffering_free_slot( ssl, offset );
11417}
11418
11419static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
11420 uint8_t slot )
11421{
11422 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11423 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010011424
11425 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
11426 return;
11427
Hanno Beckere605b192018-08-21 15:59:07 +010011428 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010011429 {
Hanno Beckere605b192018-08-21 15:59:07 +010011430 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010011431 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010011432 mbedtls_free( hs_buf->data );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020011433 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010011434 }
11435}
11436
11437#endif /* MBEDTLS_SSL_PROTO_DTLS */
11438
Gilles Peskine9b562d52018-04-25 20:32:43 +020011439void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011440{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011441 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11442
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011443 if( handshake == NULL )
11444 return;
11445
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011446#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11447 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11448 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011449 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011450 handshake->async_in_progress = 0;
11451 }
11452#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11453
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011454#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11455 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11456 mbedtls_md5_free( &handshake->fin_md5 );
11457 mbedtls_sha1_free( &handshake->fin_sha1 );
11458#endif
11459#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11460#if defined(MBEDTLS_SHA256_C)
11461 mbedtls_sha256_free( &handshake->fin_sha256 );
11462#endif
11463#if defined(MBEDTLS_SHA512_C)
11464 mbedtls_sha512_free( &handshake->fin_sha512 );
11465#endif
11466#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011468#if defined(MBEDTLS_DHM_C)
11469 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011470#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011471#if defined(MBEDTLS_ECDH_C)
11472 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011473#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011474#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011475 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011476#if defined(MBEDTLS_SSL_CLI_C)
11477 mbedtls_free( handshake->ecjpake_cache );
11478 handshake->ecjpake_cache = NULL;
11479 handshake->ecjpake_cache_len = 0;
11480#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011481#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011482
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011483#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11484 if( handshake->psk != NULL )
11485 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011486 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011487 mbedtls_free( handshake->psk );
11488 }
11489#endif
11490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011491#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11492 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011493 /*
11494 * Free only the linked list wrapper, not the keys themselves
11495 * since the belong to the SNI callback
11496 */
11497 if( handshake->sni_key_cert != NULL )
11498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011499 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011500
11501 while( cur != NULL )
11502 {
11503 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011504 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011505 cur = next;
11506 }
11507 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011508#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011509
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011510#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011511 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011512 if( handshake->ecrs_peer_cert != NULL )
11513 {
11514 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11515 mbedtls_free( handshake->ecrs_peer_cert );
11516 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011517#endif
11518
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011519#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11520 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11521 mbedtls_pk_free( &handshake->peer_pubkey );
11522#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011524#if defined(MBEDTLS_SSL_PROTO_DTLS)
11525 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011526 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011527 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011528#endif
11529
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011530 mbedtls_platform_zeroize( handshake,
11531 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011532}
11533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011534void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011535{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011536 if( session == NULL )
11537 return;
11538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011539#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011540 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011541#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011542
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011543#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011544 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011545#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011546
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011547 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011548}
11549
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011550#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011551
11552#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11553#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11554#else
11555#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11556#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11557
11558#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11559#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11560#else
11561#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11562#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11563
11564#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11565#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11566#else
11567#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11568#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11569
11570#if defined(MBEDTLS_SSL_ALPN)
11571#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11572#else
11573#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11574#endif /* MBEDTLS_SSL_ALPN */
11575
11576#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11577#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11578#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11579#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11580
11581#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11582 ( (uint32_t) ( \
11583 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11584 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11585 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11586 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11587 0u ) )
11588
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011589static unsigned char ssl_serialized_context_header[] = {
11590 MBEDTLS_VERSION_MAJOR,
11591 MBEDTLS_VERSION_MINOR,
11592 MBEDTLS_VERSION_PATCH,
11593 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11594 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011595 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11596 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11597 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011598};
11599
Paul Bakker5121ce52009-01-03 21:22:43 +000011600/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011601 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011602 *
11603 * The format of the serialized data is:
11604 * (in the presentation language of TLS, RFC 8446 section 3)
11605 *
11606 * // header
11607 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011608 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011609 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011610 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011611 * Note: When updating the format, remember to keep these
11612 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011613 *
11614 * // session sub-structure
11615 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11616 * // transform sub-structure
11617 * uint8 random[64]; // ServerHello.random+ClientHello.random
11618 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11619 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11620 * // fields from ssl_context
11621 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11622 * uint64 in_window_top; // DTLS: last validated record seq_num
11623 * uint64 in_window; // DTLS: bitmask for replay protection
11624 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11625 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11626 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11627 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11628 *
11629 * Note that many fields of the ssl_context or sub-structures are not
11630 * serialized, as they fall in one of the following categories:
11631 *
11632 * 1. forced value (eg in_left must be 0)
11633 * 2. pointer to dynamically-allocated memory (eg session, transform)
11634 * 3. value can be re-derived from other data (eg session keys from MS)
11635 * 4. value was temporary (eg content of input buffer)
11636 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011637 */
11638int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11639 unsigned char *buf,
11640 size_t buf_len,
11641 size_t *olen )
11642{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011643 unsigned char *p = buf;
11644 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011645 size_t session_len;
11646 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011647
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011648 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011649 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11650 * this function's documentation.
11651 *
11652 * These are due to assumptions/limitations in the implementation. Some of
11653 * them are likely to stay (no handshake in progress) some might go away
11654 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011655 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011656 /* The initial handshake must be over */
11657 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011659 if( ssl->handshake != NULL )
11660 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11661 /* Double-check that sub-structures are indeed ready */
11662 if( ssl->transform == NULL || ssl->session == NULL )
11663 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11664 /* There must be no pending incoming or outgoing data */
11665 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11666 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11667 if( ssl->out_left != 0 )
11668 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11669 /* Protocol must be DLTS, not TLS */
11670 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11671 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11672 /* Version must be 1.2 */
11673 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11674 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11675 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11676 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11677 /* We must be using an AEAD ciphersuite */
11678 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11679 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11680 /* Renegotiation must not be enabled */
11681 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11682 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011683
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011684 /*
11685 * Version and format identifier
11686 */
11687 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011688
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011689 if( used <= buf_len )
11690 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011691 mbedtls_platform_memcpy( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011692 sizeof( ssl_serialized_context_header ) );
11693 p += sizeof( ssl_serialized_context_header );
11694 }
11695
11696 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011697 * Session (length + data)
11698 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011699 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011700 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11701 return( ret );
11702
11703 used += 4 + session_len;
11704 if( used <= buf_len )
11705 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011706 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011707
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011708 ret = ssl_session_save( ssl->session, 1,
11709 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011710 if( ret != 0 )
11711 return( ret );
11712
11713 p += session_len;
11714 }
11715
11716 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011717 * Transform
11718 */
11719 used += sizeof( ssl->transform->randbytes );
11720 if( used <= buf_len )
11721 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011722 mbedtls_platform_memcpy( p, ssl->transform->randbytes,
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011723 sizeof( ssl->transform->randbytes ) );
11724 p += sizeof( ssl->transform->randbytes );
11725 }
11726
11727#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11728 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11729 if( used <= buf_len )
11730 {
11731 *p++ = ssl->transform->in_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011732 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11733 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011734 p += ssl->transform->in_cid_len;
11735
11736 *p++ = ssl->transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011737 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11738 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011739 p += ssl->transform->out_cid_len;
11740 }
11741#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11742
11743 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011744 * Saved fields from top-level ssl_context structure
11745 */
11746#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11747 used += 4;
11748 if( used <= buf_len )
11749 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011750 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011751 }
11752#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11753
11754#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11755 used += 16;
11756 if( used <= buf_len )
11757 {
11758 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11759 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11760 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11761 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11762 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11763 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11764 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11765 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11766
11767 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11768 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11769 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11770 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11771 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11772 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11773 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11774 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11775 }
11776#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11777
11778#if defined(MBEDTLS_SSL_PROTO_DTLS)
11779 used += 1;
11780 if( used <= buf_len )
11781 {
11782 *p++ = ssl->disable_datagram_packing;
11783 }
11784#endif /* MBEDTLS_SSL_PROTO_DTLS */
11785
11786 used += 8;
11787 if( used <= buf_len )
11788 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011789 mbedtls_platform_memcpy( p, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011790 p += 8;
11791 }
11792
11793#if defined(MBEDTLS_SSL_PROTO_DTLS)
11794 used += 2;
11795 if( used <= buf_len )
11796 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011797 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011798 }
11799#endif /* MBEDTLS_SSL_PROTO_DTLS */
11800
11801#if defined(MBEDTLS_SSL_ALPN)
11802 {
11803 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011804 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011805 : 0;
11806
11807 used += 1 + alpn_len;
11808 if( used <= buf_len )
11809 {
11810 *p++ = alpn_len;
11811
11812 if( ssl->alpn_chosen != NULL )
11813 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011814 mbedtls_platform_memcpy( p, ssl->alpn_chosen, alpn_len );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011815 p += alpn_len;
11816 }
11817 }
11818 }
11819#endif /* MBEDTLS_SSL_ALPN */
11820
11821 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011822 * Done
11823 */
11824 *olen = used;
11825
11826 if( used > buf_len )
11827 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011828
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011829 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11830
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011831 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011832}
11833
11834/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011835 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011836 *
11837 * This internal version is wrapped by a public function that cleans up in
11838 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011839 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011840static int ssl_context_load( mbedtls_ssl_context *ssl,
11841 const unsigned char *buf,
11842 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011843{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011844 const unsigned char *p = buf;
11845 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011846 size_t session_len;
11847 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011848
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011849 /*
11850 * The context should have been freshly setup or reset.
11851 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011852 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011853 * renegotiating, or if the user mistakenly loaded a session first.)
11854 */
11855 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11856 ssl->session != NULL )
11857 {
11858 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11859 }
11860
11861 /*
11862 * We can't check that the config matches the initial one, but we can at
11863 * least check it matches the requirements for serializing.
11864 */
11865 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011866 mbedtls_ssl_ver_lt(
11867 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ),
11868 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11869 mbedtls_ssl_ver_gt(
11870 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ),
11871 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11872 mbedtls_ssl_ver_lt(
11873 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ),
11874 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
11875 mbedtls_ssl_ver_gt(
11876 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ),
11877 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011878 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011879 {
11880 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11881 }
11882
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011883 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11884
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011885 /*
11886 * Check version identifier
11887 */
11888 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11889 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11890
Teppo Järvelin650343c2019-10-03 15:36:59 +030011891 // use regular memcmp as header is not that critical
11892 if( memcmp( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011893 sizeof( ssl_serialized_context_header ) ) != 0 )
11894 {
11895 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11896 }
11897 p += sizeof( ssl_serialized_context_header );
11898
11899 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011900 * Session
11901 */
11902 if( (size_t)( end - p ) < 4 )
11903 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11904
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011905 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011906 p += 4;
11907
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011908 /* This has been allocated by ssl_handshake_init(), called by
11909 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11910 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011911 ssl->session_in = ssl->session;
11912 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011913 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011914
11915 if( (size_t)( end - p ) < session_len )
11916 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11917
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011918 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011919 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011920 {
11921 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011922 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011923 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011924
11925 p += session_len;
11926
11927 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011928 * Transform
11929 */
11930
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011931 /* This has been allocated by ssl_handshake_init(), called by
11932 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11933 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011934 ssl->transform_in = ssl->transform;
11935 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011936 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011937
11938 /* Read random bytes and populate structure */
11939 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11940 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11941
11942 ret = ssl_populate_transform( ssl->transform,
11943 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11944 ssl->session->master,
11945#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11946#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11947 ssl->session->encrypt_then_mac,
11948#endif
11949#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11950 ssl->session->trunc_hmac,
11951#endif
11952#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11953#if defined(MBEDTLS_ZLIB_SUPPORT)
11954 ssl->session->compression,
11955#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011956 p, /* currently pointing to randbytes */
11957 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11958 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11959 ssl );
11960 if( ret != 0 )
11961 return( ret );
11962
11963 p += sizeof( ssl->transform->randbytes );
11964
11965#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11966 /* Read connection IDs and store them */
11967 if( (size_t)( end - p ) < 1 )
11968 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11969
11970 ssl->transform->in_cid_len = *p++;
11971
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011972 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011973 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11974
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011975 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11976 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011977 p += ssl->transform->in_cid_len;
11978
11979 ssl->transform->out_cid_len = *p++;
11980
11981 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11982 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11983
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011984 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11985 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011986 p += ssl->transform->out_cid_len;
11987#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11988
11989 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011990 * Saved fields from top-level ssl_context structure
11991 */
11992#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11993 if( (size_t)( end - p ) < 4 )
11994 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11995
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011996 ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011997 p += 4;
11998#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11999
12000#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
12001 if( (size_t)( end - p ) < 16 )
12002 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12003
12004 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
12005 ( (uint64_t) p[1] << 48 ) |
12006 ( (uint64_t) p[2] << 40 ) |
12007 ( (uint64_t) p[3] << 32 ) |
12008 ( (uint64_t) p[4] << 24 ) |
12009 ( (uint64_t) p[5] << 16 ) |
12010 ( (uint64_t) p[6] << 8 ) |
12011 ( (uint64_t) p[7] );
12012 p += 8;
12013
12014 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
12015 ( (uint64_t) p[1] << 48 ) |
12016 ( (uint64_t) p[2] << 40 ) |
12017 ( (uint64_t) p[3] << 32 ) |
12018 ( (uint64_t) p[4] << 24 ) |
12019 ( (uint64_t) p[5] << 16 ) |
12020 ( (uint64_t) p[6] << 8 ) |
12021 ( (uint64_t) p[7] );
12022 p += 8;
12023#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
12024
12025#if defined(MBEDTLS_SSL_PROTO_DTLS)
12026 if( (size_t)( end - p ) < 1 )
12027 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12028
12029 ssl->disable_datagram_packing = *p++;
12030#endif /* MBEDTLS_SSL_PROTO_DTLS */
12031
12032 if( (size_t)( end - p ) < 8 )
12033 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12034
Teppo Järvelin91d79382019-10-02 09:09:31 +030012035 mbedtls_platform_memcpy( ssl->cur_out_ctr, p, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012036 p += 8;
12037
12038#if defined(MBEDTLS_SSL_PROTO_DTLS)
12039 if( (size_t)( end - p ) < 2 )
12040 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030012041 ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012042 p += 2;
12043#endif /* MBEDTLS_SSL_PROTO_DTLS */
12044
12045#if defined(MBEDTLS_SSL_ALPN)
12046 {
12047 uint8_t alpn_len;
12048 const char **cur;
12049
12050 if( (size_t)( end - p ) < 1 )
12051 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12052
12053 alpn_len = *p++;
12054
12055 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
12056 {
12057 /* alpn_chosen should point to an item in the configured list */
12058 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
12059 {
12060 if( strlen( *cur ) == alpn_len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +030012061 mbedtls_platform_memcmp( p, cur, alpn_len ) == 0 )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012062 {
12063 ssl->alpn_chosen = *cur;
12064 break;
12065 }
12066 }
12067 }
12068
12069 /* can only happen on conf mismatch */
12070 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
12071 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12072
12073 p += alpn_len;
12074 }
12075#endif /* MBEDTLS_SSL_ALPN */
12076
12077 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012078 * Forced fields from top-level ssl_context structure
12079 *
12080 * Most of them already set to the correct value by mbedtls_ssl_init() and
12081 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
12082 */
12083 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
12084
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012085#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012086 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012087#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
12088#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012089 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012090#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012091
Hanno Becker83985822019-08-30 10:42:49 +010012092 /* Adjust pointers for header fields of outgoing records to
12093 * the given transform, accounting for explicit IV and CID. */
12094 ssl_update_out_pointers( ssl, ssl->transform );
12095
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012096#if defined(MBEDTLS_SSL_PROTO_DTLS)
12097 ssl->in_epoch = 1;
12098#endif
12099
12100 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
12101 * which we don't want - otherwise we'd end up freeing the wrong transform
12102 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
12103 if( ssl->handshake != NULL )
12104 {
12105 mbedtls_ssl_handshake_free( ssl );
12106 mbedtls_free( ssl->handshake );
12107 ssl->handshake = NULL;
12108 }
12109
12110 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020012111 * Done - should have consumed entire buffer
12112 */
12113 if( p != end )
12114 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012115
12116 return( 0 );
12117}
12118
12119/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020012120 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012121 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012122int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012123 const unsigned char *buf,
12124 size_t len )
12125{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012126 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012127
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012128 if( ret != 0 )
12129 mbedtls_ssl_free( context );
12130
12131 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012132}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020012133#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012134
12135/*
Paul Bakker5121ce52009-01-03 21:22:43 +000012136 * Free an SSL context
12137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012138void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000012139{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020012140 if( ssl == NULL )
12141 return;
12142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012144
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012145 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012146 {
Angus Grattond8213d02016-05-25 20:56:48 +100012147 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012148 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012149 }
12150
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012151 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012152 {
Angus Grattond8213d02016-05-25 20:56:48 +100012153 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012154 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012155 }
12156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012157#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020012158 if( ssl->compress_buf != NULL )
12159 {
Angus Grattond8213d02016-05-25 20:56:48 +100012160 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012161 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020012162 }
12163#endif
12164
Paul Bakker48916f92012-09-16 19:57:18 +000012165 if( ssl->transform )
12166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012167 mbedtls_ssl_transform_free( ssl->transform );
12168 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000012169 }
12170
12171 if( ssl->handshake )
12172 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020012173 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012174 mbedtls_ssl_transform_free( ssl->transform_negotiate );
12175 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012177 mbedtls_free( ssl->handshake );
12178 mbedtls_free( ssl->transform_negotiate );
12179 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012180 }
12181
Paul Bakkerc0463502013-02-14 11:19:38 +010012182 if( ssl->session )
12183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012184 mbedtls_ssl_session_free( ssl->session );
12185 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010012186 }
12187
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030012188#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020012189 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012190 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012191 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012192 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000012193 }
Paul Bakker0be444a2013-08-27 21:55:01 +020012194#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000012195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012196#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
12197 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000012198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
12200 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000012201 }
12202#endif
12203
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012204#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012205 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020012206#endif
12207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000012209
Paul Bakker86f04f42013-02-14 11:20:09 +010012210 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012211 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012212}
12213
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012214/*
12215 * Initialze mbedtls_ssl_config
12216 */
12217void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
12218{
12219 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010012220
12221#if !defined(MBEDTLS_SSL_PROTO_TLS)
12222 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
12223#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012224}
12225
Simon Butcherc97b6972015-12-27 23:48:17 +000012226#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012227#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012228static int ssl_preset_default_hashes[] = {
12229#if defined(MBEDTLS_SHA512_C)
12230 MBEDTLS_MD_SHA512,
12231 MBEDTLS_MD_SHA384,
12232#endif
12233#if defined(MBEDTLS_SHA256_C)
12234 MBEDTLS_MD_SHA256,
12235 MBEDTLS_MD_SHA224,
12236#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020012237#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012238 MBEDTLS_MD_SHA1,
12239#endif
12240 MBEDTLS_MD_NONE
12241};
Simon Butcherc97b6972015-12-27 23:48:17 +000012242#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012243#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012244
Hanno Becker73f4cb12019-06-27 13:51:07 +010012245#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012246static int ssl_preset_suiteb_ciphersuites[] = {
12247 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
12248 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
12249 0
12250};
Hanno Becker73f4cb12019-06-27 13:51:07 +010012251#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012252
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012253#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012254#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012255static int ssl_preset_suiteb_hashes[] = {
12256 MBEDTLS_MD_SHA256,
12257 MBEDTLS_MD_SHA384,
12258 MBEDTLS_MD_NONE
12259};
12260#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012261#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012262
Hanno Beckerc1096e72019-06-19 12:30:41 +010012263#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012264static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010012265#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012266 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012267#endif
12268#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012269 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012270#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012271 MBEDTLS_ECP_DP_NONE
12272};
12273#endif
12274
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012275/*
Tillmann Karras588ad502015-09-25 04:27:22 +020012276 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012277 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012278int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012279 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012280{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012281#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012282 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012283#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012284
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020012285 /* Use the functions here so that they are covered in tests,
12286 * but otherwise access member directly for efficiency */
12287 mbedtls_ssl_conf_endpoint( conf, endpoint );
12288 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012289
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012290 /*
12291 * Things that are common to all presets
12292 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012293#if defined(MBEDTLS_SSL_CLI_C)
12294 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
12295 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012296#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012297 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012298#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012299#if defined(MBEDTLS_SSL_SESSION_TICKETS)
12300 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
12301#endif
12302 }
12303#endif
12304
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012305#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012306 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012307#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012308
12309#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
12310 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
12311#endif
12312
12313#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010012314#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012315 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012316#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
12317#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030012318 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030012319 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012320#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012321#endif
12322
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010012323#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
12324 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
12325#endif
12326
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012327#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012328 conf->f_cookie_write = ssl_cookie_write_dummy;
12329 conf->f_cookie_check = ssl_cookie_check_dummy;
12330#endif
12331
Hanno Becker7f376f42019-06-12 16:20:48 +010012332#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
12333 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012334 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
12335#endif
12336
Janos Follath088ce432017-04-10 12:42:31 +010012337#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012338#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010012339 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012340#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
12341#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010012342
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012343#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010012344#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012345 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012346#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
12347#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012348 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012349#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
12350#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012351
12352#if defined(MBEDTLS_SSL_RENEGOTIATION)
12353 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020012354 mbedtls_platform_memset( conf->renego_period, 0x00, 2 );
12355 mbedtls_platform_memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012356#endif
12357
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012358#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
12359 if( endpoint == MBEDTLS_SSL_IS_SERVER )
12360 {
Hanno Becker00d0a682017-10-04 13:14:29 +010012361 const unsigned char dhm_p[] =
12362 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
12363 const unsigned char dhm_g[] =
12364 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
12365
Hanno Beckera90658f2017-10-04 15:29:08 +010012366 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
12367 dhm_p, sizeof( dhm_p ),
12368 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012369 {
12370 return( ret );
12371 }
12372 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020012373#endif
12374
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012375 /*
12376 * Preset-specific defaults
12377 */
12378 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012379 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012380 /*
12381 * NSA Suite B
12382 */
12383 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010012384#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012385 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010012386#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12387#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012388 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010012389#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12390#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012391 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012392#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12393#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012394 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012395#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012396
Hanno Becker73f4cb12019-06-27 13:51:07 +010012397#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012398 conf->ciphersuite_list[0] =
12399 conf->ciphersuite_list[1] =
12400 conf->ciphersuite_list[2] =
12401 conf->ciphersuite_list[3] =
12402 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010012403#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012404
12405#if defined(MBEDTLS_X509_CRT_PARSE_C)
12406 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012407#endif
12408
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012409#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012410#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012411 conf->sig_hashes = ssl_preset_suiteb_hashes;
12412#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012413#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012414
12415#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012416#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012417 conf->curve_list = ssl_preset_suiteb_curves;
12418#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012419#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020012420 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012421
12422 /*
12423 * Default
12424 */
12425 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010012426#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012427 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
12428 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
12429 MBEDTLS_SSL_MIN_MAJOR_VERSION :
12430 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012431#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12432#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012433 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
12434 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
12435 MBEDTLS_SSL_MIN_MINOR_VERSION :
12436 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012437#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020012438 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012439 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
12440#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010012441#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12442#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
12443 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12444#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12445#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12446 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12447#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012448
Hanno Becker73f4cb12019-06-27 13:51:07 +010012449#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012450 conf->ciphersuite_list[0] =
12451 conf->ciphersuite_list[1] =
12452 conf->ciphersuite_list[2] =
12453 conf->ciphersuite_list[3] =
12454 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012455#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012456
12457#if defined(MBEDTLS_X509_CRT_PARSE_C)
12458 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12459#endif
12460
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012461#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012462#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012463 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012464#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012465#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012466
12467#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012468#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012469 conf->curve_list = mbedtls_ecp_grp_id_list();
12470#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012471#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012472
12473#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12474 conf->dhm_min_bitlen = 1024;
12475#endif
12476 }
12477
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012478 return( 0 );
12479}
12480
12481/*
12482 * Free mbedtls_ssl_config
12483 */
12484void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12485{
12486#if defined(MBEDTLS_DHM_C)
12487 mbedtls_mpi_free( &conf->dhm_P );
12488 mbedtls_mpi_free( &conf->dhm_G );
12489#endif
12490
12491#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12492 if( conf->psk != NULL )
12493 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012494 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012495 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012496 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012497 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012498 }
12499
12500 if( conf->psk_identity != NULL )
12501 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012502 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012503 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012504 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012505 conf->psk_identity_len = 0;
12506 }
12507#endif
12508
12509#if defined(MBEDTLS_X509_CRT_PARSE_C)
12510 ssl_key_cert_free( conf->key_cert );
12511#endif
12512
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012513 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012514}
12515
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012516#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012517 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12518 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012519/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012520 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012522unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012523{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012524#if defined(MBEDTLS_RSA_C)
12525 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12526 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012527#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012528#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012529 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12530 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012531#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012532 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012533}
12534
Hanno Becker7e5437a2017-04-28 17:15:26 +010012535unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12536{
12537 switch( type ) {
12538 case MBEDTLS_PK_RSA:
12539 return( MBEDTLS_SSL_SIG_RSA );
12540 case MBEDTLS_PK_ECDSA:
12541 case MBEDTLS_PK_ECKEY:
12542 return( MBEDTLS_SSL_SIG_ECDSA );
12543 default:
12544 return( MBEDTLS_SSL_SIG_ANON );
12545 }
12546}
12547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012548mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012549{
12550 switch( sig )
12551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012552#if defined(MBEDTLS_RSA_C)
12553 case MBEDTLS_SSL_SIG_RSA:
12554 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012555#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012556#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012557 case MBEDTLS_SSL_SIG_ECDSA:
12558 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012559#endif
12560 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012561 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012562 }
12563}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012564#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012565
Hanno Becker7e5437a2017-04-28 17:15:26 +010012566#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12567 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12568
12569/* Find an entry in a signature-hash set matching a given hash algorithm. */
12570mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12571 mbedtls_pk_type_t sig_alg )
12572{
12573 switch( sig_alg )
12574 {
12575 case MBEDTLS_PK_RSA:
12576 return( set->rsa );
12577 case MBEDTLS_PK_ECDSA:
12578 return( set->ecdsa );
12579 default:
12580 return( MBEDTLS_MD_NONE );
12581 }
12582}
12583
12584/* Add a signature-hash-pair to a signature-hash set */
12585void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12586 mbedtls_pk_type_t sig_alg,
12587 mbedtls_md_type_t md_alg )
12588{
12589 switch( sig_alg )
12590 {
12591 case MBEDTLS_PK_RSA:
12592 if( set->rsa == MBEDTLS_MD_NONE )
12593 set->rsa = md_alg;
12594 break;
12595
12596 case MBEDTLS_PK_ECDSA:
12597 if( set->ecdsa == MBEDTLS_MD_NONE )
12598 set->ecdsa = md_alg;
12599 break;
12600
12601 default:
12602 break;
12603 }
12604}
12605
12606/* Allow exactly one hash algorithm for each signature. */
12607void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12608 mbedtls_md_type_t md_alg )
12609{
12610 set->rsa = md_alg;
12611 set->ecdsa = md_alg;
12612}
12613
12614#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12615 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12616
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012617/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012618 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012620mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012621{
12622 switch( hash )
12623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012624#if defined(MBEDTLS_MD5_C)
12625 case MBEDTLS_SSL_HASH_MD5:
12626 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012627#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012628#if defined(MBEDTLS_SHA1_C)
12629 case MBEDTLS_SSL_HASH_SHA1:
12630 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012631#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012632#if defined(MBEDTLS_SHA256_C)
12633 case MBEDTLS_SSL_HASH_SHA224:
12634 return( MBEDTLS_MD_SHA224 );
12635 case MBEDTLS_SSL_HASH_SHA256:
12636 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012637#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012638#if defined(MBEDTLS_SHA512_C)
12639 case MBEDTLS_SSL_HASH_SHA384:
12640 return( MBEDTLS_MD_SHA384 );
12641 case MBEDTLS_SSL_HASH_SHA512:
12642 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012643#endif
12644 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012645 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012646 }
12647}
12648
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012649/*
12650 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12651 */
12652unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12653{
12654 switch( md )
12655 {
12656#if defined(MBEDTLS_MD5_C)
12657 case MBEDTLS_MD_MD5:
12658 return( MBEDTLS_SSL_HASH_MD5 );
12659#endif
12660#if defined(MBEDTLS_SHA1_C)
12661 case MBEDTLS_MD_SHA1:
12662 return( MBEDTLS_SSL_HASH_SHA1 );
12663#endif
12664#if defined(MBEDTLS_SHA256_C)
12665 case MBEDTLS_MD_SHA224:
12666 return( MBEDTLS_SSL_HASH_SHA224 );
12667 case MBEDTLS_MD_SHA256:
12668 return( MBEDTLS_SSL_HASH_SHA256 );
12669#endif
12670#if defined(MBEDTLS_SHA512_C)
12671 case MBEDTLS_MD_SHA384:
12672 return( MBEDTLS_SSL_HASH_SHA384 );
12673 case MBEDTLS_MD_SHA512:
12674 return( MBEDTLS_SSL_HASH_SHA512 );
12675#endif
12676 default:
12677 return( MBEDTLS_SSL_HASH_NONE );
12678 }
12679}
12680
Hanno Beckeree902df2019-08-23 13:47:47 +010012681#if defined(MBEDTLS_USE_TINYCRYPT)
12682/*
12683 * Check if a curve proposed by the peer is in our list.
12684 * Return 0 if we're willing to use it, -1 otherwise.
12685 */
12686int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12687 mbedtls_uecc_group_id grp_id )
12688{
12689 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12690 if( own_ec_id == grp_id )
12691 return( 0 );
12692 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12693
12694 return( -1 );
12695}
12696#endif /* MBEDTLS_USE_TINYCRYPT */
12697
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012698#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012699/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012700 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012701 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012702 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012703int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12704 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012705{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012706 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12707 if( own_ec_id == grp_id )
12708 return( 0 );
12709 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012710
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012711 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012712}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012713#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012714
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012715#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012716/*
12717 * Check if a hash proposed by the peer is in our list.
12718 * Return 0 if we're willing to use it, -1 otherwise.
12719 */
12720int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12721 mbedtls_md_type_t md )
12722{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012723 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12724 if( md_alg == md )
12725 return( 0 );
12726 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012727
12728 return( -1 );
12729}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012730#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012732#if defined(MBEDTLS_X509_CRT_PARSE_C)
12733int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012734 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012735 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012736 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012737{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012738 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012739#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012740 int usage = 0;
12741#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012742#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012743 const char *ext_oid;
12744 size_t ext_len;
12745#endif
12746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012747#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12748 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012749 ((void) cert);
12750 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012751 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012752#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012754#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12755 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012756 {
12757 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012758 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012760 case MBEDTLS_KEY_EXCHANGE_RSA:
12761 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012762 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012763 break;
12764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012765 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12766 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12767 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12768 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012769 break;
12770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012771 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12772 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012773 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012774 break;
12775
12776 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012777 case MBEDTLS_KEY_EXCHANGE_NONE:
12778 case MBEDTLS_KEY_EXCHANGE_PSK:
12779 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12780 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012781 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012782 usage = 0;
12783 }
12784 }
12785 else
12786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012787 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12788 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012789 }
12790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012791 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012792 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012793 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012794 ret = -1;
12795 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012796#else
12797 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012798#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012800#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12801 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012803 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12804 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012805 }
12806 else
12807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012808 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12809 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012810 }
12811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012812 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012813 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012814 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012815 ret = -1;
12816 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012817#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012818
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012819 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012820}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012821#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012822
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012823#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12824 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12825int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12826 unsigned char *output,
12827 unsigned char *data, size_t data_len )
12828{
12829 int ret = 0;
12830 mbedtls_md5_context mbedtls_md5;
12831 mbedtls_sha1_context mbedtls_sha1;
12832
12833 mbedtls_md5_init( &mbedtls_md5 );
12834 mbedtls_sha1_init( &mbedtls_sha1 );
12835
12836 /*
12837 * digitally-signed struct {
12838 * opaque md5_hash[16];
12839 * opaque sha_hash[20];
12840 * };
12841 *
12842 * md5_hash
12843 * MD5(ClientHello.random + ServerHello.random
12844 * + ServerParams);
12845 * sha_hash
12846 * SHA(ClientHello.random + ServerHello.random
12847 * + ServerParams);
12848 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012849 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012850 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012851 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012852 goto exit;
12853 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012854 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012855 ssl->handshake->randbytes, 64 ) ) != 0 )
12856 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012857 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012858 goto exit;
12859 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012860 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012861 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012862 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012863 goto exit;
12864 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012865 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012866 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012867 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012868 goto exit;
12869 }
12870
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012871 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012872 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012873 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012874 goto exit;
12875 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012876 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012877 ssl->handshake->randbytes, 64 ) ) != 0 )
12878 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012880 goto exit;
12881 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012882 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012883 data_len ) ) != 0 )
12884 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012886 goto exit;
12887 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012888 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012889 output + 16 ) ) != 0 )
12890 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012891 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012892 goto exit;
12893 }
12894
12895exit:
12896 mbedtls_md5_free( &mbedtls_md5 );
12897 mbedtls_sha1_free( &mbedtls_sha1 );
12898
12899 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012900 mbedtls_ssl_pend_fatal_alert( ssl,
12901 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012902
12903 return( ret );
12904
12905}
12906#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12907 MBEDTLS_SSL_PROTO_TLS1_1 */
12908
12909#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12910 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12911int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012912 unsigned char *hash, size_t *hashlen,
12913 unsigned char *data, size_t data_len,
12914 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012915{
12916 int ret = 0;
12917 mbedtls_md_context_t ctx;
Hanno Beckera5cedbc2019-07-17 11:21:02 +010012918 mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012919 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012920
12921 mbedtls_md_init( &ctx );
12922
12923 /*
12924 * digitally-signed struct {
12925 * opaque client_random[32];
12926 * opaque server_random[32];
12927 * ServerDHParams params;
12928 * };
12929 */
12930 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12931 {
12932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12933 goto exit;
12934 }
12935 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12936 {
12937 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12938 goto exit;
12939 }
12940 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12941 {
12942 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12943 goto exit;
12944 }
12945 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12946 {
12947 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12948 goto exit;
12949 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012950 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012951 {
12952 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12953 goto exit;
12954 }
12955
12956exit:
12957 mbedtls_md_free( &ctx );
12958
12959 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012960 mbedtls_ssl_pend_fatal_alert( ssl,
12961 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012962
12963 return( ret );
12964}
12965#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12966 MBEDTLS_SSL_PROTO_TLS1_2 */
12967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012968#endif /* MBEDTLS_SSL_TLS_C */