blob: 98515609758b6961b8f5273ef1f1ca6c596782bf [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 {
Piotr Nowickie048b912020-06-05 17:59:28 +02002030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret",
2031 MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2032 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002033 }
2034 }
2035 else
Hanno Becker09d23642019-07-22 17:18:18 +01002036 {
2037 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2038 return( ret );
2039 }
2040
2041 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2042 }
2043 else
2044#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01002045#if defined(MBEDTLS_ECDH_C) && \
2046 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2047 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2048 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2049 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002050 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2051 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2052 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2053 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2054 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2055 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2056 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2057 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2058 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002059 ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002060 &ssl->handshake->pmslen,
2061 ssl->handshake->premaster,
2062 MBEDTLS_MPI_MAX_SIZE,
2063 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002064 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2065 if( ret == 0 )
2066 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002067 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002068 if( ret == 0 )
2069 {
2070 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2071 }
2072 else
2073 {
2074 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002075 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002076 }
2077 }
2078 else
Hanno Becker09d23642019-07-22 17:18:18 +01002079 {
2080 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2081 return( ret );
2082 }
2083
2084 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2085 }
2086 else
2087#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2088 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2089 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2090 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2091#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2092 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2093 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002094 ret = mbedtls_ssl_psk_derive_premaster( ssl,
2095 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) );
2096 if( ret == 0 )
2097 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002098 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002099 if( ret == 0 )
2100 {
2101 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2102 }
2103 else
2104 {
2105 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002106 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002107 }
2108 }
2109 else
Hanno Becker09d23642019-07-22 17:18:18 +01002110 {
2111 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2112 return( ret );
2113 }
2114 }
2115 else
2116#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2117#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2118 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2119 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2120 {
2121 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2122 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2123 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002124 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002125 if( ret == 0 )
2126 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002127 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002128 if( ret == 0 )
2129 {
2130 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2131 }
2132 else
2133 {
2134 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002135 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002136 }
2137 }
2138 else
Hanno Becker09d23642019-07-22 17:18:18 +01002139 {
2140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2141 return( ret );
2142 }
2143 }
2144 else
2145#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2146#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2147 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2148 == MBEDTLS_KEY_EXCHANGE_RSA )
2149 {
2150 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002151 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002152 * ssl_rsa_generate_partial_pms(). Only the
2153 * PMS length needs to be set. */
2154 ssl->handshake->pmslen = 48;
2155 }
2156 else
2157#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2158 {
2159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2160 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2161 }
2162
2163 return( 0 );
2164}
2165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2167int 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 +02002168{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002169 unsigned char *p = ssl->handshake->premaster;
2170 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002171 const unsigned char *psk = ssl->conf->psk;
2172 size_t psk_len = ssl->conf->psk_len;
2173
2174 /* If the psk callback was called, use its result */
2175 if( ssl->handshake->psk != NULL )
2176 {
2177 psk = ssl->handshake->psk;
2178 psk_len = ssl->handshake->psk_len;
2179 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002180
2181 /*
2182 * PMS = struct {
2183 * opaque other_secret<0..2^16-1>;
2184 * opaque psk<0..2^16-1>;
2185 * };
2186 * with "other_secret" depending on the particular key exchange
2187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2189 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002190 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002191 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002193
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002194 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002195
2196 if( end < p || (size_t)( end - p ) < psk_len )
2197 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2198
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002199 mbedtls_platform_memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002200 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002201 }
2202 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2204#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2205 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002206 {
2207 /*
2208 * other_secret already set by the ClientKeyExchange message,
2209 * and is 48 bytes long
2210 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002211 if( end - p < 2 )
2212 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2213
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002214 *p++ = 0;
2215 *p++ = 48;
2216 p += 48;
2217 }
2218 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2220#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2221 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002222 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002223 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002224 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002225
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002226 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002228 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002229 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002230 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002233 return( ret );
2234 }
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002235 p = mbedtls_platform_put_uint16_be( p, len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002236 p += len;
2237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002239 }
2240 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2242#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2243 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002244 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002245 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002246 size_t zlen;
2247
Hanno Becker982da7e2019-09-02 09:47:39 +01002248#if defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01002249 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2250 ssl->handshake->ecdh_privkey,
2251 p + 2 );
2252 if( ret == UECC_FAULT_DETECTED )
2253 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2254 if( ret != UECC_SUCCESS )
Hanno Becker982da7e2019-09-02 09:47:39 +01002255 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Hanno Becker982da7e2019-09-02 09:47:39 +01002256
2257 zlen = NUM_ECC_BYTES;
2258#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002260 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002261 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002262 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002265 return( ret );
2266 }
2267
Hanno Becker982da7e2019-09-02 09:47:39 +01002268 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2269 MBEDTLS_DEBUG_ECDH_Z );
2270#endif /* MBEDTLS_USE_TINYCRYPT */
2271
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002272 p = mbedtls_platform_put_uint16_be( p, zlen );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002273 p += zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002274 }
2275 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2279 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002280 }
2281
2282 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002283 if( end - p < 2 )
2284 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002285
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002286 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002287
2288 if( end < p || (size_t)( end - p ) < psk_len )
2289 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2290
Teppo Järvelin91d79382019-10-02 09:09:31 +03002291 mbedtls_platform_memcpy( p, psk, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002292 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002293
2294 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2295
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002296 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2297
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002298 return( 0 );
2299}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002303/*
2304 * SSLv3.0 MAC functions
2305 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002306#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002307static void ssl_mac( mbedtls_md_context_t *md_ctx,
2308 const unsigned char *secret,
2309 const unsigned char *buf, size_t len,
2310 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002311 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002312{
2313 unsigned char header[11];
2314 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002315 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2317 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002318
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002319 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002321 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002322 else
Paul Bakker68884e32013-01-07 18:20:04 +01002323 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002324
Teppo Järvelin91d79382019-10-02 09:09:31 +03002325 mbedtls_platform_memcpy( header, ctr, 8 );
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03002326 header[8] = (unsigned char) type;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002327 (void)mbedtls_platform_put_uint16_be( &header[9], len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002328
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002329 mbedtls_platform_memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 mbedtls_md_starts( md_ctx );
2331 mbedtls_md_update( md_ctx, secret, md_size );
2332 mbedtls_md_update( md_ctx, padding, padlen );
2333 mbedtls_md_update( md_ctx, header, 11 );
2334 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002335 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002336
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002337 mbedtls_platform_memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 mbedtls_md_starts( md_ctx );
2339 mbedtls_md_update( md_ctx, secret, md_size );
2340 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002341 mbedtls_md_update( md_ctx, out, md_size );
2342 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002343}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002345
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002346/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002347 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002348#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002349 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2350 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2351 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2352/* This function makes sure every byte in the memory region is accessed
2353 * (in ascending addresses order) */
2354static void ssl_read_memory( unsigned char *p, size_t len )
2355{
2356 unsigned char acc = 0;
2357 volatile unsigned char force;
2358
2359 for( ; len != 0; p++, len-- )
2360 acc ^= *p;
2361
2362 force = acc;
2363 (void) force;
2364}
2365#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2366
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002367/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002368 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002369 */
Hanno Becker3307b532017-12-27 21:37:21 +00002370
Hanno Beckera5a2b082019-05-15 14:03:01 +01002371#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002372/* This functions transforms a DTLS plaintext fragment and a record content
2373 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002374 *
2375 * struct {
2376 * opaque content[DTLSPlaintext.length];
2377 * ContentType real_type;
2378 * uint8 zeros[length_of_padding];
2379 * } DTLSInnerPlaintext;
2380 *
2381 * Input:
2382 * - `content`: The beginning of the buffer holding the
2383 * plaintext to be wrapped.
2384 * - `*content_size`: The length of the plaintext in Bytes.
2385 * - `max_len`: The number of Bytes available starting from
2386 * `content`. This must be `>= *content_size`.
2387 * - `rec_type`: The desired record content type.
2388 *
2389 * Output:
2390 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2391 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2392 *
2393 * Returns:
2394 * - `0` on success.
2395 * - A negative error code if `max_len` didn't offer enough space
2396 * for the expansion.
2397 */
2398static int ssl_cid_build_inner_plaintext( unsigned char *content,
2399 size_t *content_size,
2400 size_t remaining,
2401 uint8_t rec_type )
2402{
2403 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002404 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2405 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2406 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002407
2408 /* Write real content type */
2409 if( remaining == 0 )
2410 return( -1 );
2411 content[ len ] = rec_type;
2412 len++;
2413 remaining--;
2414
2415 if( remaining < pad )
2416 return( -1 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002417 mbedtls_platform_memset( content + len, 0, pad );
Hanno Becker92c930f2019-04-29 17:31:37 +01002418 len += pad;
2419 remaining -= pad;
2420
2421 *content_size = len;
2422 return( 0 );
2423}
2424
Hanno Becker7dc25772019-05-20 15:08:01 +01002425/* This function parses a DTLSInnerPlaintext structure.
2426 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002427static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2428 size_t *content_size,
2429 uint8_t *rec_type )
2430{
2431 size_t remaining = *content_size;
2432
2433 /* Determine length of padding by skipping zeroes from the back. */
2434 do
2435 {
2436 if( remaining == 0 )
2437 return( -1 );
2438 remaining--;
2439 } while( content[ remaining ] == 0 );
2440
2441 *content_size = remaining;
2442 *rec_type = content[ remaining ];
2443
2444 return( 0 );
2445}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002446#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002447
Hanno Becker99abf512019-05-20 14:50:53 +01002448/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002449 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002450static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002451 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002452 mbedtls_record *rec )
2453{
Hanno Becker99abf512019-05-20 14:50:53 +01002454 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002455 *
2456 * additional_data = seq_num + TLSCompressed.type +
2457 * TLSCompressed.version + TLSCompressed.length;
2458 *
Hanno Becker99abf512019-05-20 14:50:53 +01002459 * For the CID extension, this is extended as follows
2460 * (quoting draft-ietf-tls-dtls-connection-id-05,
2461 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002462 *
2463 * additional_data = seq_num + DTLSPlaintext.type +
2464 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002465 * cid +
2466 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002467 * length_of_DTLSInnerPlaintext;
2468 */
2469
Teppo Järvelin91d79382019-10-02 09:09:31 +03002470 mbedtls_platform_memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002471 add_data[8] = rec->type;
Teppo Järvelin91d79382019-10-02 09:09:31 +03002472 mbedtls_platform_memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002473
Hanno Beckera5a2b082019-05-15 14:03:01 +01002474#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002475 if( rec->cid_len != 0 )
2476 {
Teppo Järvelin91d79382019-10-02 09:09:31 +03002477 mbedtls_platform_memcpy( add_data + 11, rec->cid, rec->cid_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002478 add_data[11 + rec->cid_len + 0] = rec->cid_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002479 (void)mbedtls_platform_put_uint16_be( &add_data[11 + rec->cid_len + 1],
2480 rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002481 *add_data_len = 13 + 1 + rec->cid_len;
2482 }
2483 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002484#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002485 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002486 (void)mbedtls_platform_put_uint16_be( &add_data[11], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002487 *add_data_len = 13;
2488 }
Hanno Becker3307b532017-12-27 21:37:21 +00002489}
2490
Hanno Becker611a83b2018-01-03 14:27:32 +00002491int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2492 mbedtls_ssl_transform *transform,
2493 mbedtls_record *rec,
2494 int (*f_rng)(void *, unsigned char *, size_t),
2495 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002496{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002498 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002499 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002500 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002501 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002502 size_t post_avail;
2503
2504 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002505#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002506 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002507 ((void) ssl);
2508#endif
2509
2510 /* The PRNG is used for dynamic IV generation that's used
2511 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2512#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2513 ( defined(MBEDTLS_AES_C) || \
2514 defined(MBEDTLS_ARIA_C) || \
2515 defined(MBEDTLS_CAMELLIA_C) ) && \
2516 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2517 ((void) f_rng);
2518 ((void) p_rng);
2519#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
Hanno Becker3307b532017-12-27 21:37:21 +00002523 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002524 {
Hanno Becker3307b532017-12-27 21:37:21 +00002525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2526 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2527 }
Hanno Becker505089d2019-05-01 09:45:57 +01002528 if( rec == NULL
2529 || rec->buf == NULL
2530 || rec->buf_len < rec->data_offset
2531 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002532#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002533 || rec->cid_len != 0
2534#endif
2535 )
Hanno Becker3307b532017-12-27 21:37:21 +00002536 {
2537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002539 }
2540
Hanno Becker3307b532017-12-27 21:37:21 +00002541 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002542 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002544 data, rec->data_len );
2545
2546 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2547
2548 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2549 {
2550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2551 (unsigned) rec->data_len,
2552 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2554 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002555
Hanno Beckera5a2b082019-05-15 14:03:01 +01002556#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002557 /*
2558 * Add CID information
2559 */
2560 rec->cid_len = transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03002561 /* Not using more secure mbedtls_platform_memcpy as cid is public */
2562 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
Hanno Beckere83efe62019-04-29 13:52:53 +01002563 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002564
2565 if( rec->cid_len != 0 )
2566 {
2567 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002568 * Wrap plaintext into DTLSInnerPlaintext structure.
2569 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002570 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002571 * Note that this changes `rec->data_len`, and hence
2572 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002573 */
2574 if( ssl_cid_build_inner_plaintext( data,
2575 &rec->data_len,
2576 post_avail,
2577 rec->type ) != 0 )
2578 {
2579 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2580 }
2581
2582 rec->type = MBEDTLS_SSL_MSG_CID;
2583 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002584#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002585
Hanno Becker92c930f2019-04-29 17:31:37 +01002586 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2587
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002589 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002591#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592 if( mode == MBEDTLS_MODE_STREAM ||
2593 ( mode == MBEDTLS_MODE_CBC
2594#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002595 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002596#endif
2597 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 {
Hanno Becker3307b532017-12-27 21:37:21 +00002599 if( post_avail < transform->maclen )
2600 {
2601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2602 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2603 }
2604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002606 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2607 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002608 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002609 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002610 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2611 data, rec->data_len, rec->ctr, rec->type, mac );
Teppo Järvelin91d79382019-10-02 09:09:31 +03002612 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002613 }
2614 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002615#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2617 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002618 if( mbedtls_ssl_ver_geq(
2619 mbedtls_ssl_transform_get_minor_ver( transform ),
2620 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002621 {
Hanno Becker992b6872017-11-09 18:57:39 +00002622 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2623
Hanno Beckere83efe62019-04-29 13:52:53 +01002624 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002625
Hanno Becker3307b532017-12-27 21:37:21 +00002626 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002627 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002628 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2629 data, rec->data_len );
2630 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2631 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2632
Teppo Järvelin91d79382019-10-02 09:09:31 +03002633 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002634 }
2635 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002636#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2639 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002640 }
2641
Hanno Becker3307b532017-12-27 21:37:21 +00002642 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2643 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002644
Hanno Becker3307b532017-12-27 21:37:21 +00002645 rec->data_len += transform->maclen;
2646 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002647 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002648 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002649#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002650
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002651 /*
2652 * Encrypt
2653 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2655 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002656 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002657 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002658 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002660 "including %d bytes of padding",
2661 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002662
Hanno Becker3307b532017-12-27 21:37:21 +00002663 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2664 transform->iv_enc, transform->ivlen,
2665 data, rec->data_len,
2666 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002669 return( ret );
2670 }
2671
Hanno Becker3307b532017-12-27 21:37:21 +00002672 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2675 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002676 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002677 }
Paul Bakker68884e32013-01-07 18:20:04 +01002678 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002680
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002681#if defined(MBEDTLS_GCM_C) || \
2682 defined(MBEDTLS_CCM_C) || \
2683 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002685 mode == MBEDTLS_MODE_CCM ||
2686 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002687 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002688 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002689 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002690 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002691
Hanno Becker3307b532017-12-27 21:37:21 +00002692 /* Check that there's space for both the authentication tag
2693 * and the explicit IV before and after the record content. */
2694 if( post_avail < transform->taglen ||
2695 rec->data_offset < explicit_iv_len )
2696 {
2697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2698 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2699 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002700
Paul Bakker68884e32013-01-07 18:20:04 +01002701 /*
2702 * Generate IV
2703 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002704 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2705 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002706 /* GCM and CCM: fixed || explicit (=seqnum) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002707 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2708 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, rec->ctr,
Hanno Becker3307b532017-12-27 21:37:21 +00002709 explicit_iv_len );
2710 /* Prefix record content with explicit IV. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002711 mbedtls_platform_memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002712 }
2713 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2714 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002715 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002716 unsigned char i;
2717
Teppo Järvelin91d79382019-10-02 09:09:31 +03002718 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002719
2720 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002721 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002722 }
2723 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002724 {
2725 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2727 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002728 }
2729
Hanno Beckere83efe62019-04-29 13:52:53 +01002730 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002731
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002732 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2733 iv, transform->ivlen );
2734 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002735 data - explicit_iv_len, explicit_iv_len );
2736 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002737 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002739 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002740 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002741
Paul Bakker68884e32013-01-07 18:20:04 +01002742 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002743 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002744 */
Hanno Becker3307b532017-12-27 21:37:21 +00002745
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002746 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002747 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002748 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002749 data, rec->data_len, /* source */
2750 data, &rec->data_len, /* destination */
2751 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002754 return( ret );
2755 }
2756
Hanno Becker3307b532017-12-27 21:37:21 +00002757 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2758 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002759
Hanno Becker3307b532017-12-27 21:37:21 +00002760 rec->data_len += transform->taglen + explicit_iv_len;
2761 rec->data_offset -= explicit_iv_len;
2762 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002763 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002764 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002765 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2767#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002768 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002769 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002770 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002771 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002772 size_t padlen, i;
2773 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002774
Hanno Becker3307b532017-12-27 21:37:21 +00002775 /* Currently we're always using minimal padding
2776 * (up to 255 bytes would be allowed). */
2777 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2778 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002779 padlen = 0;
2780
Hanno Becker3307b532017-12-27 21:37:21 +00002781 /* Check there's enough space in the buffer for the padding. */
2782 if( post_avail < padlen + 1 )
2783 {
2784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2785 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2786 }
2787
Paul Bakker5121ce52009-01-03 21:22:43 +00002788 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002789 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002790
Hanno Becker3307b532017-12-27 21:37:21 +00002791 rec->data_len += padlen + 1;
2792 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002795 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002796 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2797 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002798 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002799 if( mbedtls_ssl_ver_geq(
2800 mbedtls_ssl_transform_get_minor_ver( transform ),
2801 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002802 {
Hanno Becker3307b532017-12-27 21:37:21 +00002803 if( f_rng == NULL )
2804 {
2805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2806 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2807 }
2808
2809 if( rec->data_offset < transform->ivlen )
2810 {
2811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2812 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2813 }
2814
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002815 /*
2816 * Generate IV
2817 */
Hanno Becker3307b532017-12-27 21:37:21 +00002818 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002819 if( ret != 0 )
2820 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002821
Teppo Järvelin91d79382019-10-02 09:09:31 +03002822 mbedtls_platform_memcpy( data - transform->ivlen, transform->iv_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002823 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002824
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002825 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002829 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002830 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002831 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002832
Hanno Becker3307b532017-12-27 21:37:21 +00002833 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2834 transform->iv_enc,
2835 transform->ivlen,
2836 data, rec->data_len,
2837 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002840 return( ret );
2841 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002842
Hanno Becker3307b532017-12-27 21:37:21 +00002843 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2846 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002847 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002849#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002850 if( mbedtls_ssl_ver_lt(
2851 mbedtls_ssl_transform_get_minor_ver( transform ),
2852 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02002853 {
2854 /*
2855 * Save IV in SSL3 and TLS1
2856 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002857 mbedtls_platform_memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
Hanno Becker3307b532017-12-27 21:37:21 +00002858 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002859 }
Hanno Becker3307b532017-12-27 21:37:21 +00002860 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002861#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002862 {
2863 data -= transform->ivlen;
2864 rec->data_offset -= transform->ivlen;
2865 rec->data_len += transform->ivlen;
2866 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002869 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002870 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002871 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2872
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002873 /*
2874 * MAC(MAC_write_key, seq_num +
2875 * TLSCipherText.type +
2876 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002877 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002878 * IV + // except for TLS 1.0
2879 * ENC(content + padding + padding_length));
2880 */
Hanno Becker3307b532017-12-27 21:37:21 +00002881
2882 if( post_avail < transform->maclen)
2883 {
2884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2885 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2886 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002887
Hanno Beckere83efe62019-04-29 13:52:53 +01002888 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002891 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002892 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002893
Hanno Becker3307b532017-12-27 21:37:21 +00002894 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002895 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002896 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2897 data, rec->data_len );
2898 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2899 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002900
Teppo Järvelin91d79382019-10-02 09:09:31 +03002901 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002902
Hanno Becker3307b532017-12-27 21:37:21 +00002903 rec->data_len += transform->maclen;
2904 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002905 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002906 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002908 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002909 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002911 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2914 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002915 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002916
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002917 /* Make extra sure authentication was performed, exactly once */
2918 if( auth_done != 1 )
2919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2921 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002922 }
2923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002925
2926 return( 0 );
2927}
2928
Hanno Becker40478be2019-07-12 08:23:59 +01002929int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002930 mbedtls_ssl_transform *transform,
2931 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002932{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002933 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002935 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002936#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002937 size_t padlen = 0, correct = 1;
2938#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002939 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002940 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002941 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002942
Hanno Becker611a83b2018-01-03 14:27:32 +00002943#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002944 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002945 ((void) ssl);
2946#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002949 if( rec == NULL ||
2950 rec->buf == NULL ||
2951 rec->buf_len < rec->data_offset ||
2952 rec->buf_len - rec->data_offset < rec->data_len )
2953 {
2954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002956 }
2957
Hanno Becker4c6876b2017-12-27 21:28:58 +00002958 data = rec->buf + rec->data_offset;
2959 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002960
Hanno Beckera5a2b082019-05-15 14:03:01 +01002961#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002962 /*
2963 * Match record's CID with incoming CID.
2964 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002965 if( rec->cid_len != transform->in_cid_len ||
Teppo Järvelin0efac532019-10-04 13:21:08 +03002966 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) // use regular memcmp as CID is public
Hanno Beckerabd7c892019-05-08 13:02:22 +01002967 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002968 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002969 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002970#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2973 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002974 {
2975 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002976 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2977 transform->iv_dec,
2978 transform->ivlen,
2979 data, rec->data_len,
2980 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002983 return( ret );
2984 }
2985
Hanno Becker4c6876b2017-12-27 21:28:58 +00002986 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2989 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002990 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002991 }
Paul Bakker68884e32013-01-07 18:20:04 +01002992 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002994#if defined(MBEDTLS_GCM_C) || \
2995 defined(MBEDTLS_CCM_C) || \
2996 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002998 mode == MBEDTLS_MODE_CCM ||
2999 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003000 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003001 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003002 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003003
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003004 /*
3005 * Prepare IV from explicit and implicit data.
3006 */
3007
3008 /* Check that there's enough space for the explicit IV
3009 * (at the beginning of the record) and the MAC (at the
3010 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003011 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003014 "+ taglen (%d)", rec->data_len,
3015 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003017 }
Paul Bakker68884e32013-01-07 18:20:04 +01003018
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003019#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003020 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
3021 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003022 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01003023
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003024 /* Fixed */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003025 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003026 /* Explicit */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003027 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003028 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003029 else
3030#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3031#if defined(MBEDTLS_CHACHAPOLY_C)
3032 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003033 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02003034 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003035 unsigned char i;
3036
Teppo Järvelin91d79382019-10-02 09:09:31 +03003037 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003038
3039 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003040 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003041 }
3042 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003043#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003044 {
3045 /* Reminder if we ever add an AEAD mode with a different size */
3046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3047 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3048 }
3049
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003050 /* Group changes to data, data_len, and add_data, because
3051 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003052 data += explicit_iv_len;
3053 rec->data_offset += explicit_iv_len;
3054 rec->data_len -= explicit_iv_len + transform->taglen;
3055
Hanno Beckere83efe62019-04-29 13:52:53 +01003056 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003057 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01003058 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003059
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003060 /* Because of the check above, we know that there are
3061 * explicit_iv_len Bytes preceeding data, and taglen
3062 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01003063 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003064 * mbedtls_cipher_auth_decrypt() below. */
3065
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003066 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003067 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00003068 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01003069
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003070 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003071 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003072 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003073 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
3074 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01003075 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003076 data, rec->data_len,
3077 data, &olen,
3078 data + rec->data_len,
3079 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
3084 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003085
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003086 return( ret );
3087 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003088 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003089
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003090 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003091 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3094 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003095 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003096 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003097 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3099#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003100 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003102 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003103 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003104
Paul Bakker5121ce52009-01-03 21:22:43 +00003105 /*
Paul Bakker45829992013-01-03 14:52:21 +01003106 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003108#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003109 if( mbedtls_ssl_ver_geq(
3110 mbedtls_ssl_transform_get_minor_ver( transform ),
3111 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003112 {
3113 /* The ciphertext is prefixed with the CBC IV. */
3114 minlen += transform->ivlen;
3115 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003116#endif
Paul Bakker45829992013-01-03 14:52:21 +01003117
Hanno Becker4c6876b2017-12-27 21:28:58 +00003118 /* Size considerations:
3119 *
3120 * - The CBC cipher text must not be empty and hence
3121 * at least of size transform->ivlen.
3122 *
3123 * Together with the potential IV-prefix, this explains
3124 * the first of the two checks below.
3125 *
3126 * - The record must contain a MAC, either in plain or
3127 * encrypted, depending on whether Encrypt-then-MAC
3128 * is used or not.
3129 * - If it is, the message contains the IV-prefix,
3130 * the CBC ciphertext, and the MAC.
3131 * - If it is not, the padded plaintext, and hence
3132 * the CBC ciphertext, has at least length maclen + 1
3133 * because there is at least the padding length byte.
3134 *
3135 * As the CBC ciphertext is not empty, both cases give the
3136 * lower bound minlen + maclen + 1 on the record size, which
3137 * we test for in the second check below.
3138 */
3139 if( rec->data_len < minlen + transform->ivlen ||
3140 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003143 "+ 1 ) ( + expl IV )", rec->data_len,
3144 transform->ivlen,
3145 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003147 }
3148
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003149 /*
3150 * Authenticate before decrypt if enabled
3151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003153 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003154 {
Hanno Becker992b6872017-11-09 18:57:39 +00003155 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003158
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003159 /* Update data_len in tandem with add_data.
3160 *
3161 * The subtraction is safe because of the previous check
3162 * data_len >= minlen + maclen + 1.
3163 *
3164 * Afterwards, we know that data + data_len is followed by at
3165 * least maclen Bytes, which justifies the call to
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003166 * mbedtls_platform_memcmp() below.
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003167 *
3168 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003169 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003170 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003171
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003172 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003173 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3174 add_data_len );
3175 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3176 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003177 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3178 data, rec->data_len );
3179 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3180 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003181
Hanno Becker4c6876b2017-12-27 21:28:58 +00003182 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3183 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003184 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003185 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003186
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003187 /* Compare expected MAC with MAC at the end of the record. */
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003188 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003189 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003193 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003194 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003195 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003197
3198 /*
3199 * Check length sanity
3200 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003201
3202 /* We know from above that data_len > minlen >= 0,
3203 * so the following check in particular implies that
3204 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003205 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003208 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003210 }
3211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003212#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003213 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003214 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003215 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003216 if( mbedtls_ssl_ver_geq(
3217 mbedtls_ssl_transform_get_minor_ver( transform ),
3218 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003219 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003220 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003221 mbedtls_platform_memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003222
Hanno Becker4c6876b2017-12-27 21:28:58 +00003223 data += transform->ivlen;
3224 rec->data_offset += transform->ivlen;
3225 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003226 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003228
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003229 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3230
Hanno Becker4c6876b2017-12-27 21:28:58 +00003231 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3232 transform->iv_dec, transform->ivlen,
3233 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003236 return( ret );
3237 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003238
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003239 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003240 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3243 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003244 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003247 if( mbedtls_ssl_ver_lt(
3248 mbedtls_ssl_transform_get_minor_ver( transform ),
3249 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02003250 {
3251 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003252 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3253 * records is equivalent to CBC decryption of the concatenation
3254 * of the records; in other words, IVs are maintained across
3255 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003256 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003257 mbedtls_platform_memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003258 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003259 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003260#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003261
Hanno Becker4c6876b2017-12-27 21:28:58 +00003262 /* Safe since data_len >= minlen + maclen + 1, so after having
3263 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003264 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3265 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003266 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003267
Hanno Becker4c6876b2017-12-27 21:28:58 +00003268 if( auth_done == 1 )
3269 {
3270 correct *= ( rec->data_len >= padlen + 1 );
3271 padlen *= ( rec->data_len >= padlen + 1 );
3272 }
3273 else
Paul Bakker45829992013-01-03 14:52:21 +01003274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003276 if( rec->data_len < transform->maclen + padlen + 1 )
3277 {
3278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3279 rec->data_len,
3280 transform->maclen,
3281 padlen + 1 ) );
3282 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003283#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003284
3285 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3286 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003287 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003288
Hanno Becker4c6876b2017-12-27 21:28:58 +00003289 padlen++;
3290
3291 /* Regardless of the validity of the padding,
3292 * we have data_len >= padlen here. */
3293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003295 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3296 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003297 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003298 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300#if defined(MBEDTLS_SSL_DEBUG_ALL)
3301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003302 "should be no more than %d",
3303 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003304#endif
Paul Bakker45829992013-01-03 14:52:21 +01003305 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003306 }
3307 }
3308 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3310#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3311 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003312 if( mbedtls_ssl_ver_gt(
3313 mbedtls_ssl_transform_get_minor_ver( transform ),
3314 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003315 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003316 /* The padding check involves a series of up to 256
3317 * consecutive memory reads at the end of the record
3318 * plaintext buffer. In order to hide the length and
3319 * validity of the padding, always perform exactly
3320 * `min(256,plaintext_len)` reads (but take into account
3321 * only the last `padlen` bytes for the padding check). */
3322 size_t pad_count = 0;
3323 size_t real_count = 0;
3324 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003325
Hanno Becker4c6876b2017-12-27 21:28:58 +00003326 /* Index of first padding byte; it has been ensured above
3327 * that the subtraction is safe. */
3328 size_t const padding_idx = rec->data_len - padlen;
3329 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3330 size_t const start_idx = rec->data_len - num_checks;
3331 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003332
Hanno Becker4c6876b2017-12-27 21:28:58 +00003333 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003334 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003335 real_count |= ( idx >= padding_idx );
3336 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003337 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003338 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003341 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003343#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003344 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003345 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003346 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3348 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3351 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003352 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003353
Hanno Becker4c6876b2017-12-27 21:28:58 +00003354 /* If the padding was found to be invalid, padlen == 0
3355 * and the subtraction is safe. If the padding was found valid,
3356 * padlen hasn't been changed and the previous assertion
3357 * data_len >= padlen still holds. */
3358 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003359 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003360 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003362 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3365 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003366 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003367
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003368#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003370 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003371#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003372
3373 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003374 * Authenticate if not done yet.
3375 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003376 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003377#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003378 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003379 {
Hanno Becker992b6872017-11-09 18:57:39 +00003380 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003381
Hanno Becker4c6876b2017-12-27 21:28:58 +00003382 /* If the initial value of padlen was such that
3383 * data_len < maclen + padlen + 1, then padlen
3384 * got reset to 1, and the initial check
3385 * data_len >= minlen + maclen + 1
3386 * guarantees that at this point we still
3387 * have at least data_len >= maclen.
3388 *
3389 * If the initial value of padlen was such that
3390 * data_len >= maclen + padlen + 1, then we have
3391 * subtracted either padlen + 1 (if the padding was correct)
3392 * or 0 (if the padding was incorrect) since then,
3393 * hence data_len >= maclen in any case.
3394 */
3395 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003396 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003399 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3400 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003401 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003402 ssl_mac( &transform->md_ctx_dec,
3403 transform->mac_dec,
3404 data, rec->data_len,
3405 rec->ctr, rec->type,
3406 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003407 }
3408 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3410#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3411 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003412 if( mbedtls_ssl_ver_gt(
3413 mbedtls_ssl_transform_get_minor_ver( transform ),
3414 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003415 {
3416 /*
3417 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003418 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003419 *
3420 * Known timing attacks:
3421 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3422 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003423 * To compensate for different timings for the MAC calculation
3424 * depending on how much padding was removed (which is determined
3425 * by padlen), process extra_run more blocks through the hash
3426 * function.
3427 *
3428 * The formula in the paper is
3429 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3430 * where L1 is the size of the header plus the decrypted message
3431 * plus CBC padding and L2 is the size of the header plus the
3432 * decrypted message. This is for an underlying hash function
3433 * with 64-byte blocks.
3434 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3435 * correctly. We round down instead of up, so -56 is the correct
3436 * value for our calculations instead of -55.
3437 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003438 * Repeat the formula rather than defining a block_size variable.
3439 * This avoids requiring division by a variable at runtime
3440 * (which would be marginally less efficient and would require
3441 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003442 */
3443 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003444 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003445
3446 /*
3447 * The next two sizes are the minimum and maximum values of
3448 * in_msglen over all padlen values.
3449 *
3450 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003451 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003452 *
3453 * Note that max_len + maclen is never more than the buffer
3454 * length, as we previously did in_msglen -= maclen too.
3455 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003456 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003457 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3458
Hanno Becker4c6876b2017-12-27 21:28:58 +00003459 memset( tmp, 0, sizeof( tmp ) );
3460
Hanno Beckera5cedbc2019-07-17 11:21:02 +01003461 switch( mbedtls_md_get_type(
3462 mbedtls_md_get_handle( &transform->md_ctx_dec ) ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003463 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003464#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3465 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003466 case MBEDTLS_MD_MD5:
3467 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003468 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003469 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003470 extra_run =
3471 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3472 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003473 break;
3474#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003475#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003476 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003477 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003478 extra_run =
3479 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3480 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003481 break;
3482#endif
3483 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003485 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3486 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003487
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003488 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003489
Hanno Beckere83efe62019-04-29 13:52:53 +01003490 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3491 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003492 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3493 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003494 /* Make sure we access everything even when padlen > 0. This
3495 * makes the synchronisation requirements for just-in-time
3496 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003497 ssl_read_memory( data + rec->data_len, padlen );
3498 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003499
3500 /* Call mbedtls_md_process at least once due to cache attacks
3501 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003502 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003503 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003504
Hanno Becker4c6876b2017-12-27 21:28:58 +00003505 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003506
3507 /* Make sure we access all the memory that could contain the MAC,
3508 * before we check it in the next code block. This makes the
3509 * synchronisation requirements for just-in-time Prime+Probe
3510 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003511 ssl_read_memory( data + min_len,
3512 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003513 }
3514 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3516 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3519 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003520 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003521
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003522#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003523 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3524 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003525#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003526
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003527 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003528 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530#if defined(MBEDTLS_SSL_DEBUG_ALL)
3531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003532#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003533 correct = 0;
3534 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003535 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003536 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003537
3538 /*
3539 * Finally check the correct flag
3540 */
3541 if( correct == 0 )
3542 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003543#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003544
3545 /* Make extra sure authentication was performed, exactly once */
3546 if( auth_done != 1 )
3547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3549 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003550 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003551
Hanno Beckera5a2b082019-05-15 14:03:01 +01003552#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003553 if( rec->cid_len != 0 )
3554 {
3555 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3556 &rec->type );
3557 if( ret != 0 )
3558 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3559 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003560#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003563
3564 return( 0 );
3565}
3566
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003567#undef MAC_NONE
3568#undef MAC_PLAINTEXT
3569#undef MAC_CIPHERTEXT
3570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003572/*
3573 * Compression/decompression functions
3574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003576{
3577 int ret;
3578 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003579 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003580 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003581 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003584
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003585 if( len_pre == 0 )
3586 return( 0 );
3587
Teppo Järvelin91d79382019-10-02 09:09:31 +03003588 mbedtls_platform_memcpy( msg_pre, ssl->out_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003591 ssl->out_msglen ) );
3592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003593 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003594 ssl->out_msg, ssl->out_msglen );
3595
Paul Bakker48916f92012-09-16 19:57:18 +00003596 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3597 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3598 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003599 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003600
Paul Bakker48916f92012-09-16 19:57:18 +00003601 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003602 if( ret != Z_OK )
3603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3605 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003606 }
3607
Angus Grattond8213d02016-05-25 20:56:48 +10003608 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003609 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003612 ssl->out_msglen ) );
3613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003615 ssl->out_msg, ssl->out_msglen );
3616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003618
3619 return( 0 );
3620}
3621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003623{
3624 int ret;
3625 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003626 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003627 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003628 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003631
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003632 if( len_pre == 0 )
3633 return( 0 );
3634
Teppo Järvelin91d79382019-10-02 09:09:31 +03003635 mbedtls_platform_memcpy( msg_pre, ssl->in_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003638 ssl->in_msglen ) );
3639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003641 ssl->in_msg, ssl->in_msglen );
3642
Paul Bakker48916f92012-09-16 19:57:18 +00003643 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3644 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3645 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003646 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003647 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003648
Paul Bakker48916f92012-09-16 19:57:18 +00003649 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003650 if( ret != Z_OK )
3651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3653 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003654 }
3655
Angus Grattond8213d02016-05-25 20:56:48 +10003656 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003657 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003660 ssl->in_msglen ) );
3661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003663 ssl->in_msg, ssl->in_msglen );
3664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003666
3667 return( 0 );
3668}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3672static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674#if defined(MBEDTLS_SSL_PROTO_DTLS)
3675static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003676{
3677 /* If renegotiation is not enforced, retransmit until we would reach max
3678 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003679 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003680 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003681 uint32_t ratio =
3682 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3683 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003684 unsigned char doublings = 1;
3685
3686 while( ratio != 0 )
3687 {
3688 ++doublings;
3689 ratio >>= 1;
3690 }
3691
3692 if( ++ssl->renego_records_seen > doublings )
3693 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003695 return( 0 );
3696 }
3697 }
3698
3699 return( ssl_write_hello_request( ssl ) );
3700}
3701#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003702#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003703
Paul Bakker5121ce52009-01-03 21:22:43 +00003704/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003705 * Fill the input message buffer by appending data to it.
3706 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003707 *
3708 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3709 * available (from this read and/or a previous one). Otherwise, an error code
3710 * is returned (possibly EOF or WANT_READ).
3711 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003712 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3713 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3714 * since we always read a whole datagram at once.
3715 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003716 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003717 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003720{
Paul Bakker23986e52011-04-24 08:57:21 +00003721 int ret;
3722 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003725
Hanno Beckera58a8962019-06-13 16:11:15 +01003726 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3727 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003730 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003732 }
3733
Angus Grattond8213d02016-05-25 20:56:48 +10003734 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3737 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003738 }
3739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003741 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003742 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003743 uint32_t timeout;
3744
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003745 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003746 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3747 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003748 {
3749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3750 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3751 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3752 }
3753
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003754 /*
3755 * The point is, we need to always read a full datagram at once, so we
3756 * sometimes read more then requested, and handle the additional data.
3757 * It could be the rest of the current record (while fetching the
3758 * header) and/or some other records in the same datagram.
3759 */
3760
3761 /*
3762 * Move to the next record in the already read datagram if applicable
3763 */
3764 if( ssl->next_record_offset != 0 )
3765 {
3766 if( ssl->in_left < ssl->next_record_offset )
3767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3769 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003770 }
3771
3772 ssl->in_left -= ssl->next_record_offset;
3773
3774 if( ssl->in_left != 0 )
3775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003777 ssl->next_record_offset ) );
3778 memmove( ssl->in_hdr,
3779 ssl->in_hdr + ssl->next_record_offset,
3780 ssl->in_left );
3781 }
3782
3783 ssl->next_record_offset = 0;
3784 }
3785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003787 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003788
3789 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003790 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003791 */
3792 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003795 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003796 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003797
3798 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003799 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003800 * are not at the beginning of a new record, the caller did something
3801 * wrong.
3802 */
3803 if( ssl->in_left != 0 )
3804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3806 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003807 }
3808
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003809 /*
3810 * Don't even try to read if time's out already.
3811 * This avoids by-passing the timer when repeatedly receiving messages
3812 * that will end up being dropped.
3813 */
3814 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003815 {
3816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003817 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003818 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003819 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003820 {
Angus Grattond8213d02016-05-25 20:56:48 +10003821 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003823 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003824 timeout = ssl->handshake->retransmit_timeout;
3825 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003826 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003829
Hanno Beckera58a8962019-06-13 16:11:15 +01003830 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3831 {
3832 ret = mbedtls_ssl_get_recv_timeout( ssl )
3833 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3834 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003835 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003836 {
3837 ret = mbedtls_ssl_get_recv( ssl )
3838 ( ssl->p_bio, ssl->in_hdr, len );
3839 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003842
3843 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003845 }
3846
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003847 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003850 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003853 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003854 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003857 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003858 }
3859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003863 return( ret );
3864 }
3865
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003866 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003867 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003868#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003869 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3870 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003871 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003872 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003873 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003875 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003876 return( ret );
3877 }
3878
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003879 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003880 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003882 }
3883
Paul Bakker5121ce52009-01-03 21:22:43 +00003884 if( ret < 0 )
3885 return( ret );
3886
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003887 ssl->in_left = ret;
3888 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003889 MBEDTLS_SSL_TRANSPORT_ELSE
3890#endif /* MBEDTLS_SSL_PROTO_DTLS */
3891#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003894 ssl->in_left, nb_want ) );
3895
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003896 while( ssl->in_left < nb_want )
3897 {
3898 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003899
3900 if( ssl_check_timer( ssl ) != 0 )
3901 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3902 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003903 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003904 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003905 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003906 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3907 ssl->in_hdr + ssl->in_left, len,
3908 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003909 }
3910 else
3911 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003912 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003913 ssl->in_hdr + ssl->in_left, len );
3914 }
3915 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003918 ssl->in_left, nb_want ) );
3919 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003920
3921 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003922 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003923
3924 if( ret < 0 )
3925 return( ret );
3926
mohammad160352aecb92018-03-28 23:41:40 -07003927 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003928 {
Darryl Green11999bb2018-03-13 15:22:58 +00003929 MBEDTLS_SSL_DEBUG_MSG( 1,
3930 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003931 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003932 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3933 }
3934
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003935 ssl->in_left += ret;
3936 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003937 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003938#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003941
3942 return( 0 );
3943}
3944
3945/*
3946 * Flush any data not yet written
3947 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003948int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003949{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003950 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003951 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003954
Hanno Beckera58a8962019-06-13 16:11:15 +01003955 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003958 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003960 }
3961
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003962 /* Avoid incrementing counter if data is flushed */
3963 if( ssl->out_left == 0 )
3964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003966 return( 0 );
3967 }
3968
Paul Bakker5121ce52009-01-03 21:22:43 +00003969 while( ssl->out_left > 0 )
3970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003972 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003973
Hanno Becker2b1e3542018-08-06 11:19:13 +01003974 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003975 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003977 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003978
3979 if( ret <= 0 )
3980 return( ret );
3981
mohammad160352aecb92018-03-28 23:41:40 -07003982 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003983 {
Darryl Green11999bb2018-03-13 15:22:58 +00003984 MBEDTLS_SSL_DEBUG_MSG( 1,
3985 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003986 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003987 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3988 }
3989
Paul Bakker5121ce52009-01-03 21:22:43 +00003990 ssl->out_left -= ret;
3991 }
3992
Hanno Becker2b1e3542018-08-06 11:19:13 +01003993#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003994 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003995 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003996 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003997 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003998 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003999#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004000#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01004001 {
4002 ssl->out_hdr = ssl->out_buf + 8;
4003 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004004#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01004005 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004008
4009 return( 0 );
4010}
4011
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004012/*
4013 * Functions to handle the DTLS retransmission state machine
4014 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004015#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004016/*
4017 * Append current handshake message to current outgoing flight
4018 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004019static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004020{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004021 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01004022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
4023 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
4024 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004025
4026 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004027 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004028 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004030 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004031 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004032 }
4033
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004034 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004035 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004037 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004038 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004039 }
4040
4041 /* Copy current handshake message with headers */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004042 mbedtls_platform_memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004043 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004044 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004045 msg->next = NULL;
4046
4047 /* Append to the current flight */
4048 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004049 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004050 else
4051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004052 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004053 while( cur->next != NULL )
4054 cur = cur->next;
4055 cur->next = msg;
4056 }
4057
Hanno Becker3b235902018-08-06 09:54:53 +01004058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004059 return( 0 );
4060}
4061
4062/*
4063 * Free the current flight of handshake messages
4064 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004065static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004066{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004067 mbedtls_ssl_flight_item *cur = flight;
4068 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004069
4070 while( cur != NULL )
4071 {
4072 next = cur->next;
4073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004074 mbedtls_free( cur->p );
4075 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004076
4077 cur = next;
4078 }
4079}
4080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004081#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4082static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004083#endif
4084
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004085/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004086 * Swap transform_out and out_ctr with the alternative ones
4087 */
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004088static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004089{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004090 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004091 unsigned char tmp_out_ctr[8];
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004092#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4093 int ret;
4094#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004095
4096 if( ssl->transform_out == ssl->handshake->alt_transform_out )
4097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004098 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004099 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004100 }
4101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004102 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004103
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004104 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004105 tmp_transform = ssl->transform_out;
4106 ssl->transform_out = ssl->handshake->alt_transform_out;
4107 ssl->handshake->alt_transform_out = tmp_transform;
4108
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004109 /* Swap epoch + sequence_number */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004110 mbedtls_platform_memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4111 mbedtls_platform_memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
4112 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004113
4114 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004115 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004117#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4118 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004120 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004122 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4123 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004124 }
4125 }
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004126#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
4127
4128 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004129}
4130
4131/*
4132 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004133 */
4134int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4135{
4136 int ret = 0;
4137
4138 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4139
4140 ret = mbedtls_ssl_flight_transmit( ssl );
4141
4142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4143
4144 return( ret );
4145}
4146
4147/*
4148 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004149 *
4150 * Need to remember the current message in case flush_output returns
4151 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004152 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004153 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004154int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004155{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004156 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004159 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004160 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004162
4163 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004164 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004165 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4166 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004168 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004169 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004170
4171 while( ssl->handshake->cur_msg != NULL )
4172 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004173 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004174 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004175
Hanno Beckere1dcb032018-08-17 16:47:58 +01004176 int const is_finished =
4177 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4178 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4179
Hanno Becker04da1892018-08-14 13:22:10 +01004180 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4181 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4182
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004183 /* Swap epochs before sending Finished: we can't do it after
4184 * sending ChangeCipherSpec, in case write returns WANT_READ.
4185 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004186 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004187 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004189 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4190 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004191 }
4192
Hanno Becker67bc7c32018-08-06 11:33:50 +01004193 ret = ssl_get_remaining_payload_in_datagram( ssl );
4194 if( ret < 0 )
4195 return( ret );
4196 max_frag_len = (size_t) ret;
4197
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004198 /* CCS is copied as is, while HS messages may need fragmentation */
4199 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4200 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004201 if( max_frag_len == 0 )
4202 {
4203 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4204 return( ret );
4205
4206 continue;
4207 }
4208
Teppo Järvelin91d79382019-10-02 09:09:31 +03004209 mbedtls_platform_memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004210 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004211 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004212
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004213 /* Update position inside current message */
4214 ssl->handshake->cur_msg_p += cur->len;
4215 }
4216 else
4217 {
4218 const unsigned char * const p = ssl->handshake->cur_msg_p;
4219 const size_t hs_len = cur->len - 12;
4220 const size_t frag_off = p - ( cur->p + 12 );
4221 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004222 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004223
Hanno Beckere1dcb032018-08-17 16:47:58 +01004224 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004225 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004226 if( is_finished )
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004227 {
4228 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4229 return( ret );
4230 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004231
Hanno Becker67bc7c32018-08-06 11:33:50 +01004232 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4233 return( ret );
4234
4235 continue;
4236 }
4237 max_hs_frag_len = max_frag_len - 12;
4238
4239 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4240 max_hs_frag_len : rem_len;
4241
4242 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004243 {
4244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004245 (unsigned) cur_hs_frag_len,
4246 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004247 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004248
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004249 /* Messages are stored with handshake headers as if not fragmented,
4250 * copy beginning of headers then fill fragmentation fields.
4251 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004252 mbedtls_platform_memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004253
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004254 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[6], frag_off );
4255 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[9],
4256 cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004257
4258 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4259
Hanno Becker3f7b9732018-08-28 09:53:25 +01004260 /* Copy the handshake message content and set records fields */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004261 mbedtls_platform_memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004262 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004263 ssl->out_msgtype = cur->type;
4264
4265 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004266 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004267 }
4268
4269 /* If done with the current message move to the next one if any */
4270 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4271 {
4272 if( cur->next != NULL )
4273 {
4274 ssl->handshake->cur_msg = cur->next;
4275 ssl->handshake->cur_msg_p = cur->next->p + 12;
4276 }
4277 else
4278 {
4279 ssl->handshake->cur_msg = NULL;
4280 ssl->handshake->cur_msg_p = NULL;
4281 }
4282 }
4283
4284 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004285 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004287 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004288 return( ret );
4289 }
4290 }
4291
Hanno Becker67bc7c32018-08-06 11:33:50 +01004292 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4293 return( ret );
4294
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004295 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004296 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4297 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004298 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004300 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004301 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4302 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004303
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004305
4306 return( 0 );
4307}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004308
4309/*
4310 * To be called when the last message of an incoming flight is received.
4311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004312void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004313{
4314 /* We won't need to resend that one any more */
4315 ssl_flight_free( ssl->handshake->flight );
4316 ssl->handshake->flight = NULL;
4317 ssl->handshake->cur_msg = NULL;
4318
4319 /* The next incoming flight will start with this msg_seq */
4320 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4321
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004322 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004323 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004324
Hanno Becker0271f962018-08-16 13:23:47 +01004325 /* Clear future message buffering structure. */
4326 ssl_buffering_free( ssl );
4327
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004328 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004329 ssl_set_timer( ssl, 0 );
4330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4332 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004335 }
4336 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004337 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004338}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004339
4340/*
4341 * To be called when the last message of an outgoing flight is send.
4342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004343void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004344{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004345 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004346 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004348 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4349 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004352 }
4353 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004354 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004355}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004357
Paul Bakker5121ce52009-01-03 21:22:43 +00004358/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004359 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004360 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004361
4362/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004363 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004364 *
4365 * - fill in handshake headers
4366 * - update handshake checksum
4367 * - DTLS: save message for resending
4368 * - then pass to the record layer
4369 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004370 * DTLS: except for HelloRequest, messages are only queued, and will only be
4371 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004372 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004373 * Inputs:
4374 * - ssl->out_msglen: 4 + actual handshake message len
4375 * (4 is the size of handshake headers for TLS)
4376 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4377 * - ssl->out_msg + 4: the handshake message body
4378 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004379 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004380 * - ssl->out_msglen: the length of the record contents
4381 * (including handshake headers but excluding record headers)
4382 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004383 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004384int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004385{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004386 int ret;
4387 const size_t hs_len = ssl->out_msglen - 4;
4388 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004389
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4391
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004392 /*
4393 * Sanity checks
4394 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004395 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004396 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4397 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004398 /* In SSLv3, the client might send a NoCertificate alert. */
4399#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004400 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004401 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004402 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4403 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004404#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4405 {
4406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4407 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4408 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004409 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004410
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004411 /* Whenever we send anything different from a
4412 * HelloRequest we should be in a handshake - double check. */
4413 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4414 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004415 ssl->handshake == NULL )
4416 {
4417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4418 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4419 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004421#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004422 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004423 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004424 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004425 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4427 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004428 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004429#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004430
Hanno Beckerb50a2532018-08-06 11:52:54 +01004431 /* Double-check that we did not exceed the bounds
4432 * of the outgoing record buffer.
4433 * This should never fail as the various message
4434 * writing functions must obey the bounds of the
4435 * outgoing record buffer, but better be safe.
4436 *
4437 * Note: We deliberately do not check for the MTU or MFL here.
4438 */
4439 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4440 {
4441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4442 "size %u, maximum %u",
4443 (unsigned) ssl->out_msglen,
4444 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4445 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4446 }
4447
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004448 /*
4449 * Fill handshake headers
4450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004451 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004452 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004453 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[1], hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004454
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004455 /*
4456 * DTLS has additional fields in the Handshake layer,
4457 * between the length field and the actual payload:
4458 * uint16 message_seq;
4459 * uint24 fragment_offset;
4460 * uint24 fragment_length;
4461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004462#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004463 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004464 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004465 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004466 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004467 {
4468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4469 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004470 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004471 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004472 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4473 }
4474
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004475 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004476 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004477
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004478 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004479 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004480 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004481 (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[4],
4482 ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004483 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004484 }
4485 else
4486 {
4487 ssl->out_msg[4] = 0;
4488 ssl->out_msg[5] = 0;
4489 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004490
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004491 /* Handshake hashes are computed without fragmentation,
4492 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004493 mbedtls_platform_memset( ssl->out_msg + 6, 0x00, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03004494 mbedtls_platform_memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004495 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004496#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004497
Hanno Becker0207e532018-08-28 10:28:28 +01004498 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004499 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004500 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004501 }
4502
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004503 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004504#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004505 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004506 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4507 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004508 {
4509 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004511 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004512 return( ret );
4513 }
4514 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004515 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004516#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004517 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004518 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004519 {
4520 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4521 return( ret );
4522 }
4523 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004524
4525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4526
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004527 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004528}
4529
4530/*
4531 * Record layer functions
4532 */
4533
4534/*
4535 * Write current record.
4536 *
4537 * Uses:
4538 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4539 * - ssl->out_msglen: length of the record content (excl headers)
4540 * - ssl->out_msg: record content
4541 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004542int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004543{
4544 int ret, done = 0;
4545 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004546 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004547
4548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004550#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004551 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004553 {
4554 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004556 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004557 return( ret );
4558 }
4559
4560 len = ssl->out_msglen;
4561 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004562#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004564#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4565 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004569 ret = mbedtls_ssl_hw_record_write( ssl );
4570 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004572 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4573 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004574 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004575
4576 if( ret == 0 )
4577 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004578 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004579#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004580 if( !done )
4581 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004582 unsigned i;
4583 size_t protected_record_size;
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004584 volatile int encrypted_fi = 0;
Hanno Becker2b1e3542018-08-06 11:19:13 +01004585
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004586 /* Skip writing the record content type to after the encryption,
4587 * as it may change when using the CID extension. */
4588
Hanno Becker2881d802019-05-22 14:44:53 +01004589 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4590 mbedtls_ssl_get_minor_ver( ssl ),
4591 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004592
Teppo Järvelin91d79382019-10-02 09:09:31 +03004593 mbedtls_platform_memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004594 (void)mbedtls_platform_put_uint16_be( ssl->out_len, len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004595
Paul Bakker48916f92012-09-16 19:57:18 +00004596 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004597 {
Hanno Becker3307b532017-12-27 21:37:21 +00004598 mbedtls_record rec;
4599
4600 rec.buf = ssl->out_iv;
4601 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4602 ( ssl->out_iv - ssl->out_buf );
4603 rec.data_len = ssl->out_msglen;
4604 rec.data_offset = ssl->out_msg - rec.buf;
4605
Teppo Järvelin91d79382019-10-02 09:09:31 +03004606 mbedtls_platform_memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004607 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4608 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004609 ssl->conf->transport, rec.ver );
4610 rec.type = ssl->out_msgtype;
4611
Hanno Beckera5a2b082019-05-15 14:03:01 +01004612#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004613 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004614 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004615#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004616
Hanno Becker611a83b2018-01-03 14:27:32 +00004617 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004618 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004619 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004621 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004622 return( ret );
4623 }
4624
Hanno Becker3307b532017-12-27 21:37:21 +00004625 if( rec.data_offset != 0 )
4626 {
4627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4628 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4629 }
4630
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004631 /* Update the record content type and CID. */
4632 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004633#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03004634 /* Not using more secure mbedtls_platform_memcpy as cid is public */
4635 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004636#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004637 ssl->out_msglen = len = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004638 (void)mbedtls_platform_put_uint16_be( ssl->out_len, rec.data_len );
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004639 encrypted_fi = 1;
4640 }
4641
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004642 /* Double check to ensure the encryption has been done */
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004643 if( ssl->transform_out != NULL && encrypted_fi == 0 )
4644 {
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004645 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004646 }
4647
Hanno Becker43395762019-05-03 14:46:38 +01004648 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004649
4650#if defined(MBEDTLS_SSL_PROTO_DTLS)
4651 /* In case of DTLS, double-check that we don't exceed
4652 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004653 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004654 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004655 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004656 if( ret < 0 )
4657 return( ret );
4658
4659 if( protected_record_size > (size_t) ret )
4660 {
4661 /* Should never happen */
4662 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4663 }
4664 }
4665#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004666
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004667 /* Now write the potentially updated record content type. */
4668 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004670 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004671 "version = [%d:%d], msglen = %d",
4672 ssl->out_hdr[0], ssl->out_hdr[1],
4673 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004675 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004676 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004677
4678 ssl->out_left += protected_record_size;
4679 ssl->out_hdr += protected_record_size;
4680 ssl_update_out_pointers( ssl, ssl->transform_out );
4681
Hanno Becker04484622018-08-06 09:49:38 +01004682 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4683 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4684 break;
4685
4686 /* The loop goes to its end iff the counter is wrapping */
4687 if( i == ssl_ep_len( ssl ) )
4688 {
4689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4690 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4691 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004692 }
4693
Hanno Becker67bc7c32018-08-06 11:33:50 +01004694#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004695 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004696 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004697 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004698 size_t remaining;
4699 ret = ssl_get_remaining_payload_in_datagram( ssl );
4700 if( ret < 0 )
4701 {
4702 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4703 ret );
4704 return( ret );
4705 }
4706
4707 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004708 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004709 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004710 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004711 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004712 else
4713 {
Hanno Becker513815a2018-08-20 11:56:09 +01004714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004715 }
4716 }
4717#endif /* MBEDTLS_SSL_PROTO_DTLS */
4718
4719 if( ( flush == SSL_FORCE_FLUSH ) &&
4720 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004722 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004723 return( ret );
4724 }
4725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004727
4728 return( 0 );
4729}
4730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004731#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004732
4733static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4734{
4735 if( ssl->in_msglen < ssl->in_hslen ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +03004736 mbedtls_platform_memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4737 mbedtls_platform_memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
Hanno Beckere25e3b72018-08-16 09:30:53 +01004738 {
4739 return( 1 );
4740 }
4741 return( 0 );
4742}
Hanno Becker44650b72018-08-16 12:51:11 +01004743
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004744static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004745{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004746 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[9] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004747}
4748
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004749static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004750{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004751 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[6] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004752}
4753
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004754static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004755{
4756 uint32_t msg_len, frag_off, frag_len;
4757
4758 msg_len = ssl_get_hs_total_len( ssl );
4759 frag_off = ssl_get_hs_frag_off( ssl );
4760 frag_len = ssl_get_hs_frag_len( ssl );
4761
4762 if( frag_off > msg_len )
4763 return( -1 );
4764
4765 if( frag_len > msg_len - frag_off )
4766 return( -1 );
4767
4768 if( frag_len + 12 > ssl->in_msglen )
4769 return( -1 );
4770
4771 return( 0 );
4772}
4773
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004774/*
4775 * Mark bits in bitmask (used for DTLS HS reassembly)
4776 */
4777static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4778{
4779 unsigned int start_bits, end_bits;
4780
4781 start_bits = 8 - ( offset % 8 );
4782 if( start_bits != 8 )
4783 {
4784 size_t first_byte_idx = offset / 8;
4785
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004786 /* Special case */
4787 if( len <= start_bits )
4788 {
4789 for( ; len != 0; len-- )
4790 mask[first_byte_idx] |= 1 << ( start_bits - len );
4791
4792 /* Avoid potential issues with offset or len becoming invalid */
4793 return;
4794 }
4795
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004796 offset += start_bits; /* Now offset % 8 == 0 */
4797 len -= start_bits;
4798
4799 for( ; start_bits != 0; start_bits-- )
4800 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4801 }
4802
4803 end_bits = len % 8;
4804 if( end_bits != 0 )
4805 {
4806 size_t last_byte_idx = ( offset + len ) / 8;
4807
4808 len -= end_bits; /* Now len % 8 == 0 */
4809
4810 for( ; end_bits != 0; end_bits-- )
4811 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4812 }
4813
4814 memset( mask + offset / 8, 0xFF, len / 8 );
4815}
4816
4817/*
4818 * Check that bitmask is full
4819 */
4820static int ssl_bitmask_check( unsigned char *mask, size_t len )
4821{
4822 size_t i;
4823
4824 for( i = 0; i < len / 8; i++ )
4825 if( mask[i] != 0xFF )
4826 return( -1 );
4827
4828 for( i = 0; i < len % 8; i++ )
4829 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4830 return( -1 );
4831
4832 return( 0 );
4833}
4834
Hanno Becker56e205e2018-08-16 09:06:12 +01004835/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004836static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004837 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004838{
Hanno Becker56e205e2018-08-16 09:06:12 +01004839 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004840
Hanno Becker56e205e2018-08-16 09:06:12 +01004841 alloc_len = 12; /* Handshake header */
4842 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004843
Hanno Beckerd07df862018-08-16 09:14:58 +01004844 if( add_bitmap )
4845 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004846
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004847 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004848}
Hanno Becker56e205e2018-08-16 09:06:12 +01004849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004851
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004852static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004853{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004854 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[1] ) );
Hanno Becker12555c62018-08-16 12:47:53 +01004855}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004856
Simon Butcher99000142016-10-13 17:21:01 +01004857int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004858{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004859 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004862 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004863 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004864 }
4865
Hanno Becker12555c62018-08-16 12:47:53 +01004866 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004868 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004869 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004870 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004872#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004873 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004874 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004875 int ret;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004876 unsigned int recv_msg_seq = (unsigned int)
4877 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004878
Hanno Becker44650b72018-08-16 12:51:11 +01004879 if( ssl_check_hs_header( ssl ) != 0 )
4880 {
4881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4882 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4883 }
4884
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004885 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004886 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4887 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4888 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4889 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004890 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004891 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4892 {
4893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4894 recv_msg_seq,
4895 ssl->handshake->in_msg_seq ) );
4896 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4897 }
4898
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004899 /* Retransmit only on last message from previous flight, to avoid
4900 * too many retransmissions.
4901 * Besides, No sane server ever retransmits HelloVerifyRequest */
4902 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004903 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004906 "message_seq = %d, start_of_flight = %d",
4907 recv_msg_seq,
4908 ssl->handshake->in_flight_start_seq ) );
4909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004910 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004912 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004913 return( ret );
4914 }
4915 }
4916 else
4917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004919 "message_seq = %d, expected = %d",
4920 recv_msg_seq,
4921 ssl->handshake->in_msg_seq ) );
4922 }
4923
Hanno Becker90333da2017-10-10 11:27:13 +01004924 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004925 }
4926 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004927
Hanno Becker6d97ef52018-08-16 13:09:04 +01004928 /* Message reassembly is handled alongside buffering of future
4929 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004930 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004931 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004932 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004935 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004936 }
4937 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004938 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004939#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004940#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004941 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004942 /* With TLS we don't handle fragmentation (for now) */
4943 if( ssl->in_msglen < ssl->in_hslen )
4944 {
4945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4946 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4947 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004948 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004949#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004950
Simon Butcher99000142016-10-13 17:21:01 +01004951 return( 0 );
4952}
4953
4954void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4955{
Hanno Becker0271f962018-08-16 13:23:47 +01004956 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004957
Hanno Becker0271f962018-08-16 13:23:47 +01004958 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004959 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004960
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004961 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004962#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004963 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004964 ssl->handshake != NULL )
4965 {
Hanno Becker0271f962018-08-16 13:23:47 +01004966 unsigned offset;
4967 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004968
Hanno Becker0271f962018-08-16 13:23:47 +01004969 /* Increment handshake sequence number */
4970 hs->in_msg_seq++;
4971
4972 /*
4973 * Clear up handshake buffering and reassembly structure.
4974 */
4975
4976 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004977 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004978
4979 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004980 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4981 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004982 offset++, hs_buf++ )
4983 {
4984 *hs_buf = *(hs_buf + 1);
4985 }
4986
4987 /* Create a fresh last entry */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004988 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004989 }
4990#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004991}
4992
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004993/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004994 * DTLS anti-replay: RFC 6347 4.1.2.6
4995 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004996 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4997 * Bit n is set iff record number in_window_top - n has been seen.
4998 *
4999 * Usually, in_window_top is the last record number seen and the lsb of
5000 * in_window is set. The only exception is the initial state (record number 0
5001 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005003#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5004static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005005{
5006 ssl->in_window_top = 0;
5007 ssl->in_window = 0;
5008}
5009
5010static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
5011{
5012 return( ( (uint64_t) buf[0] << 40 ) |
5013 ( (uint64_t) buf[1] << 32 ) |
5014 ( (uint64_t) buf[2] << 24 ) |
5015 ( (uint64_t) buf[3] << 16 ) |
5016 ( (uint64_t) buf[4] << 8 ) |
5017 ( (uint64_t) buf[5] ) );
5018}
5019
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005020static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
5021{
5022 int ret;
5023 unsigned char *original_in_ctr;
5024
5025 // save original in_ctr
5026 original_in_ctr = ssl->in_ctr;
5027
5028 // use counter from record
5029 ssl->in_ctr = record_in_ctr;
5030
5031 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
5032
5033 // restore the counter
5034 ssl->in_ctr = original_in_ctr;
5035
5036 return ret;
5037}
5038
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005039/*
5040 * Return 0 if sequence number is acceptable, -1 otherwise
5041 */
Hanno Beckerfc551722019-07-12 08:50:37 +01005042int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005043{
5044 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5045 uint64_t bit;
5046
Hanno Becker7f376f42019-06-12 16:20:48 +01005047 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5048 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5049 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005050 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01005051 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005052
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005053 if( rec_seqnum > ssl->in_window_top )
5054 return( 0 );
5055
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005056 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005057
5058 if( bit >= 64 )
5059 return( -1 );
5060
5061 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
5062 return( -1 );
5063
5064 return( 0 );
5065}
5066
5067/*
5068 * Update replay window on new validated record
5069 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005070void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005071{
5072 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5073
Hanno Becker7f376f42019-06-12 16:20:48 +01005074 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5075 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5076 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005077 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01005078 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005079
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005080 if( rec_seqnum > ssl->in_window_top )
5081 {
5082 /* Update window_top and the contents of the window */
5083 uint64_t shift = rec_seqnum - ssl->in_window_top;
5084
5085 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005086 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005087 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005088 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005089 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005090 ssl->in_window |= 1;
5091 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005092
5093 ssl->in_window_top = rec_seqnum;
5094 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005095 else
5096 {
5097 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005098 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005099
5100 if( bit < 64 ) /* Always true, but be extra sure */
5101 ssl->in_window |= (uint64_t) 1 << bit;
5102 }
5103}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005104#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005105
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005106#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005107/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005108static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
5109
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005110/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005111 * Without any SSL context, check if a datagram looks like a ClientHello with
5112 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01005113 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005114 *
5115 * - if cookie is valid, return 0
5116 * - if ClientHello looks superficially valid but cookie is not,
5117 * fill obuf and set olen, then
5118 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
5119 * - otherwise return a specific error code
5120 */
5121static int ssl_check_dtls_clihlo_cookie(
5122 mbedtls_ssl_cookie_write_t *f_cookie_write,
5123 mbedtls_ssl_cookie_check_t *f_cookie_check,
5124 void *p_cookie,
5125 const unsigned char *cli_id, size_t cli_id_len,
5126 const unsigned char *in, size_t in_len,
5127 unsigned char *obuf, size_t buf_len, size_t *olen )
5128{
5129 size_t sid_len, cookie_len;
5130 unsigned char *p;
5131
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005132 /*
5133 * Structure of ClientHello with record and handshake headers,
5134 * and expected values. We don't need to check a lot, more checks will be
5135 * done when actually parsing the ClientHello - skipping those checks
5136 * avoids code duplication and does not make cookie forging any easier.
5137 *
5138 * 0-0 ContentType type; copied, must be handshake
5139 * 1-2 ProtocolVersion version; copied
5140 * 3-4 uint16 epoch; copied, must be 0
5141 * 5-10 uint48 sequence_number; copied
5142 * 11-12 uint16 length; (ignored)
5143 *
5144 * 13-13 HandshakeType msg_type; (ignored)
5145 * 14-16 uint24 length; (ignored)
5146 * 17-18 uint16 message_seq; copied
5147 * 19-21 uint24 fragment_offset; copied, must be 0
5148 * 22-24 uint24 fragment_length; (ignored)
5149 *
5150 * 25-26 ProtocolVersion client_version; (ignored)
5151 * 27-58 Random random; (ignored)
5152 * 59-xx SessionID session_id; 1 byte len + sid_len content
5153 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5154 * ...
5155 *
5156 * Minimum length is 61 bytes.
5157 */
5158 if( in_len < 61 ||
5159 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5160 in[3] != 0 || in[4] != 0 ||
5161 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5162 {
5163 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5164 }
5165
5166 sid_len = in[59];
5167 if( sid_len > in_len - 61 )
5168 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5169
5170 cookie_len = in[60 + sid_len];
5171 if( cookie_len > in_len - 60 )
5172 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5173
5174 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5175 cli_id, cli_id_len ) == 0 )
5176 {
5177 /* Valid cookie */
5178 return( 0 );
5179 }
5180
5181 /*
5182 * If we get here, we've got an invalid cookie, let's prepare HVR.
5183 *
5184 * 0-0 ContentType type; copied
5185 * 1-2 ProtocolVersion version; copied
5186 * 3-4 uint16 epoch; copied
5187 * 5-10 uint48 sequence_number; copied
5188 * 11-12 uint16 length; olen - 13
5189 *
5190 * 13-13 HandshakeType msg_type; hello_verify_request
5191 * 14-16 uint24 length; olen - 25
5192 * 17-18 uint16 message_seq; copied
5193 * 19-21 uint24 fragment_offset; copied
5194 * 22-24 uint24 fragment_length; olen - 25
5195 *
5196 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5197 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5198 *
5199 * Minimum length is 28.
5200 */
5201 if( buf_len < 28 )
5202 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5203
5204 /* Copy most fields and adapt others */
Teppo Järvelin91d79382019-10-02 09:09:31 +03005205 mbedtls_platform_memcpy( obuf, in, 25 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005206 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5207 obuf[25] = 0xfe;
5208 obuf[26] = 0xff;
5209
5210 /* Generate and write actual cookie */
5211 p = obuf + 28;
5212 if( f_cookie_write( p_cookie,
5213 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5214 {
5215 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5216 }
5217
5218 *olen = p - obuf;
5219
5220 /* Go back and fill length fields */
5221 obuf[27] = (unsigned char)( *olen - 28 );
5222
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005223 (void)mbedtls_platform_put_uint24_be( &obuf[14], ( *olen - 25 ) );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005224 obuf[22] = obuf[14];
5225 obuf[23] = obuf[15];
5226 obuf[24] = obuf[16];
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005227
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005228 (void)mbedtls_platform_put_uint16_be( &obuf[11], ( *olen - 13 ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005229
5230 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5231}
5232
5233/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005234 * Handle possible client reconnect with the same UDP quadruplet
5235 * (RFC 6347 Section 4.2.8).
5236 *
5237 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5238 * that looks like a ClientHello.
5239 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005240 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005241 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005242 * - if the input looks like a ClientHello with a valid cookie,
5243 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005244 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005245 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005246 *
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005247 * This function is called (through ssl_check_client_reconnect()) when an
5248 * unexpected record is found in ssl_get_next_record(), which will discard the
5249 * record if we return 0, and bubble up the return value otherwise (this
5250 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
5251 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005252 */
5253static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5254{
5255 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005256 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005257
Hanno Becker87b56262019-07-10 14:37:41 +01005258 if( ssl->conf->f_cookie_write == NULL ||
5259 ssl->conf->f_cookie_check == NULL )
5260 {
5261 /* If we can't use cookies to verify reachability of the peer,
5262 * drop the record. */
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
5264 "can't check reconnect validity" ) );
Hanno Becker87b56262019-07-10 14:37:41 +01005265 return( 0 );
5266 }
5267
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005268 ret = ssl_check_dtls_clihlo_cookie(
5269 ssl->conf->f_cookie_write,
5270 ssl->conf->f_cookie_check,
5271 ssl->conf->p_cookie,
5272 ssl->cli_id, ssl->cli_id_len,
5273 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005274 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005275
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005276 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5277
5278 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005279 {
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005280 int send_ret;
5281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
5282 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
5283 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08005284 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005285 * If the error is permanent we'll catch it later,
5286 * if it's not, then hopefully it'll work next time. */
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005287 send_ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
5288 MBEDTLS_SSL_DEBUG_RET( 2, "mbedtls_ssl_get_send", send_ret );
5289 (void) send_ret;
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005290 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005291 }
5292
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005293 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005294 {
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005296 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5297 {
5298 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5299 return( ret );
5300 }
5301
5302 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005303 }
5304
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005305 return( ret );
5306}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005307#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005308
Hanno Becker46483f12019-05-03 13:25:54 +01005309static int ssl_check_record_type( uint8_t record_type )
5310{
5311 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5312 record_type != MBEDTLS_SSL_MSG_ALERT &&
5313 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5314 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5315 {
5316 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5317 }
5318
5319 return( 0 );
5320}
5321
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005322/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005323 * ContentType type;
5324 * ProtocolVersion version;
5325 * uint16 epoch; // DTLS only
5326 * uint48 sequence_number; // DTLS only
5327 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005328 *
5329 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005330 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005331 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5332 *
5333 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005334 * 1. proceed with the record if this function returns 0
5335 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5336 * 3. return CLIENT_RECONNECT if this function return that value
5337 * 4. drop the whole datagram if this function returns anything else.
5338 * Point 2 is needed when the peer is resending, and we have already received
5339 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005340 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005341static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005342 unsigned char *buf,
5343 size_t len,
5344 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005345{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005346 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005347
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005348 size_t const rec_hdr_type_offset = 0;
5349 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005350
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005351 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5352 rec_hdr_type_len;
5353 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005354
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005355 size_t const rec_hdr_ctr_len = 8;
5356#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005357 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005358 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5359 rec_hdr_version_len;
5360
Hanno Beckera5a2b082019-05-15 14:03:01 +01005361#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005362 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5363 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005364 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005365#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5366#endif /* MBEDTLS_SSL_PROTO_DTLS */
5367
5368 size_t rec_hdr_len_offset; /* To be determined */
5369 size_t const rec_hdr_len_len = 2;
5370
5371 /*
5372 * Check minimum lengths for record header.
5373 */
5374
5375#if defined(MBEDTLS_SSL_PROTO_DTLS)
5376 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5377 {
5378 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5379 }
5380 MBEDTLS_SSL_TRANSPORT_ELSE
5381#endif /* MBEDTLS_SSL_PROTO_DTLS */
5382#if defined(MBEDTLS_SSL_PROTO_TLS)
5383 {
5384 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5385 }
5386#endif /* MBEDTLS_SSL_PROTO_DTLS */
5387
5388 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5389 {
5390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5391 (unsigned) len,
5392 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5393 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5394 }
5395
5396 /*
5397 * Parse and validate record content type
5398 */
5399
5400 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005401
5402 /* Check record content type */
5403#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5404 rec->cid_len = 0;
5405
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005406 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005407 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5408 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005409 {
5410 /* Shift pointers to account for record header including CID
5411 * struct {
5412 * ContentType special_type = tls12_cid;
5413 * ProtocolVersion version;
5414 * uint16 epoch;
5415 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005416 * opaque cid[cid_length]; // Additional field compared to
5417 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005418 * uint16 length;
5419 * opaque enc_content[DTLSCiphertext.length];
5420 * } DTLSCiphertext;
5421 */
5422
5423 /* So far, we only support static CID lengths
5424 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005425 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5426 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005427
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005428 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005429 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5431 (unsigned) len,
5432 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005433 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005434 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005435
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005436 /* configured CID len is guaranteed at most 255, see
5437 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5438 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005439 /* Not using more secure mbedtls_platform_memcpy as cid is public */
5440 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005441 }
5442 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005443#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005444 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005445 if( ssl_check_record_type( rec->type ) )
5446 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5448 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005449 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5450 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005451 }
5452
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005453 /*
5454 * Parse and validate record version
5455 */
5456
Hanno Becker8061c6e2019-07-26 08:07:03 +01005457 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5458 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005459 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5460 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005461 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005462
Hanno Becker2881d802019-05-22 14:44:53 +01005463 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5466 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005467 }
5468
Hanno Becker7bcf2b52019-07-26 09:02:40 +01005469 if( mbedtls_ssl_ver_gt( minor_ver,
5470 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5473 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005474 }
5475
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005476 /*
5477 * Parse/Copy record sequence number.
5478 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005479
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005480#if defined(MBEDTLS_SSL_PROTO_DTLS)
5481 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5482 {
5483 /* Copy explicit record sequence number from input buffer. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005484 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5485 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005486 rec_hdr_ctr_len );
5487 }
5488 MBEDTLS_SSL_TRANSPORT_ELSE
5489#endif /* MBEDTLS_SSL_PROTO_DTLS */
5490#if defined(MBEDTLS_SSL_PROTO_TLS)
5491 {
5492 /* Copy implicit record sequence number from SSL context structure. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005493 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5494 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005495 }
5496#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005497
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005498 /*
5499 * Parse record length.
5500 */
5501
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005502 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005503 rec->data_len = mbedtls_platform_get_uint16_be( &buf[rec_hdr_len_offset] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005504 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5505
Hanno Becker8b09b732019-05-08 12:03:28 +01005506 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005507 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005508 rec->type,
5509 major_ver, minor_ver, rec->data_len ) );
5510
5511 rec->buf = buf;
5512 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005513
Hanno Beckerec014082019-07-26 08:20:27 +01005514 if( rec->data_len == 0 )
5515 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5516
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005517 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005518 * DTLS-related tests.
5519 * Check epoch before checking length constraint because
5520 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5521 * message gets duplicated before the corresponding Finished message,
5522 * the second ChangeCipherSpec should be discarded because it belongs
5523 * to an old epoch, but not because its length is shorter than
5524 * the minimum record length for packets using the new record transform.
5525 * Note that these two kinds of failures are handled differently,
5526 * as an unexpected record is silently skipped but an invalid
5527 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005528 */
5529#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005530 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005531 {
Arto Kinnunena3fa06e2019-09-09 12:22:51 +03005532 rec_epoch = (uint32_t)mbedtls_platform_get_uint16_be( rec->ctr );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005533
Hanno Beckere0452772019-07-10 17:12:07 +01005534 /* Check that the datagram is large enough to contain a record
5535 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005536 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005537 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5539 (unsigned) len,
5540 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005541 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5542 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005543
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005544 /* Records from other, non-matching epochs are silently discarded.
5545 * (The case of same-port Client reconnects must be considered in
5546 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005547 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005548 {
5549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5550 "expected %d, received %d",
5551 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005552
5553 /* Records from the next epoch are considered for buffering
5554 * (concretely: early Finished messages). */
5555 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5556 {
5557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5558 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5559 }
5560
Hanno Becker87b56262019-07-10 14:37:41 +01005561 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005562 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005563#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005564 /* For records from the correct epoch, check whether their
5565 * sequence number has been seen before. */
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005566 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
5567 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005568 {
5569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5570 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5571 }
5572#endif
5573 }
5574#endif /* MBEDTLS_SSL_PROTO_DTLS */
5575
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005576 return( 0 );
5577}
Paul Bakker5121ce52009-01-03 21:22:43 +00005578
Hanno Becker87b56262019-07-10 14:37:41 +01005579
5580#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5581static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5582{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005583 unsigned int rec_epoch = (unsigned int)
5584 mbedtls_platform_get_uint16_be( &ssl->in_ctr[0] );
Hanno Becker87b56262019-07-10 14:37:41 +01005585
5586 /*
5587 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5588 * access the first byte of record content (handshake type), as we
5589 * have an active transform (possibly iv_len != 0), so use the
5590 * fact that the record header len is 13 instead.
5591 */
5592 if( rec_epoch == 0 &&
5593 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5594 MBEDTLS_SSL_IS_SERVER &&
5595 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5596 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5597 ssl->in_left > 13 &&
5598 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5599 {
5600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5601 "from the same port" ) );
5602 return( ssl_handle_possible_reconnect( ssl ) );
5603 }
5604
5605 return( 0 );
5606}
5607#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5608
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005609/*
Manuel Pégourié-Gonnardae48d862020-01-03 12:18:49 +01005610 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005611 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005612static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5613 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005614{
5615 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005618 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005620#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5621 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005623 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 ret = mbedtls_ssl_hw_record_read( ssl );
5626 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005628 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5629 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005630 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005631
5632 if( ret == 0 )
5633 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005634 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005635#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005636 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005637 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005638 unsigned char const old_msg_type = rec->type;
5639
Hanno Becker611a83b2018-01-03 14:27:32 +00005640 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005641 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005643 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005644
Hanno Beckera5a2b082019-05-15 14:03:01 +01005645#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005646 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005647 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5648 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005649 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005650 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005651 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005652 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005653#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005654
Paul Bakker5121ce52009-01-03 21:22:43 +00005655 return( ret );
5656 }
5657
Hanno Becker106f3da2019-07-12 09:35:58 +01005658 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005659 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005660 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005661 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005662 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005663
Paul Bakker5121ce52009-01-03 21:22:43 +00005664 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005665 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005666
Hanno Beckera5a2b082019-05-15 14:03:01 +01005667#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005668 /* We have already checked the record content type
5669 * in ssl_parse_record_header(), failing or silently
5670 * dropping the record in the case of an unknown type.
5671 *
5672 * Since with the use of CIDs, the record content type
5673 * might change during decryption, re-check the record
5674 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005675 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005676 {
5677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5678 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5679 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005680#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005681
Hanno Becker106f3da2019-07-12 09:35:58 +01005682 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005683 {
5684#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005685 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005686 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005687 {
5688 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5690 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5691 }
5692#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5693
5694 ssl->nb_zero++;
5695
5696 /*
5697 * Three or more empty messages may be a DoS attack
5698 * (excessive CPU consumption).
5699 */
5700 if( ssl->nb_zero > 3 )
5701 {
5702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005703 "messages, possible DoS attack" ) );
5704 /* Treat the records as if they were not properly authenticated,
5705 * thereby failing the connection if we see more than allowed
5706 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005707 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5708 }
5709 }
5710 else
5711 ssl->nb_zero = 0;
5712
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005713 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5714#if defined(MBEDTLS_SSL_PROTO_TLS)
5715 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005716 {
5717 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005718 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005719 if( ++ssl->in_ctr[i - 1] != 0 )
5720 break;
5721
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005722 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005723 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005724 {
5725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5726 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5727 }
5728 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005729#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005730
Paul Bakker5121ce52009-01-03 21:22:43 +00005731 }
5732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005733#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005734 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005736 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005737 }
5738#endif
5739
Hanno Beckerf0242852019-07-09 17:30:02 +01005740 /* Check actual (decrypted) record content length against
5741 * configured maximum. */
5742 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5743 {
5744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5745 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5746 }
5747
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005748 return( 0 );
5749}
5750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005751static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005752
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005753/*
5754 * Read a record.
5755 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005756 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5757 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5758 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005759 */
Hanno Becker1097b342018-08-15 14:09:41 +01005760
5761/* Helper functions for mbedtls_ssl_read_record(). */
5762static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005763static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5764static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005765
Hanno Becker327c93b2018-08-15 13:56:18 +01005766int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005767 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005768{
5769 int ret;
5770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005772
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005773 if( ssl->keep_current_message == 0 )
5774 {
5775 do {
Simon Butcher99000142016-10-13 17:21:01 +01005776
Hanno Becker26994592018-08-15 14:14:59 +01005777 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005778 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005779 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005780
Hanno Beckere74d5562018-08-15 14:26:08 +01005781 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005782 {
Hanno Becker40f50842018-08-15 14:48:01 +01005783#if defined(MBEDTLS_SSL_PROTO_DTLS)
5784 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005785
Hanno Becker40f50842018-08-15 14:48:01 +01005786 /* We only check for buffered messages if the
5787 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005788 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005789 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005790 {
Hanno Becker40f50842018-08-15 14:48:01 +01005791 if( ssl_load_buffered_message( ssl ) == 0 )
5792 have_buffered = 1;
5793 }
5794
5795 if( have_buffered == 0 )
5796#endif /* MBEDTLS_SSL_PROTO_DTLS */
5797 {
5798 ret = ssl_get_next_record( ssl );
5799 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5800 continue;
5801
5802 if( ret != 0 )
5803 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005804 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005805 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005806 return( ret );
5807 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005808 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005809 }
5810
5811 ret = mbedtls_ssl_handle_message_type( ssl );
5812
Hanno Becker40f50842018-08-15 14:48:01 +01005813#if defined(MBEDTLS_SSL_PROTO_DTLS)
5814 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5815 {
5816 /* Buffer future message */
5817 ret = ssl_buffer_message( ssl );
5818 if( ret != 0 )
5819 return( ret );
5820
5821 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5822 }
5823#endif /* MBEDTLS_SSL_PROTO_DTLS */
5824
Hanno Becker90333da2017-10-10 11:27:13 +01005825 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5826 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005827
5828 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005829 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005830 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005831 return( ret );
5832 }
5833
Hanno Becker327c93b2018-08-15 13:56:18 +01005834 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005835 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005836 {
5837 mbedtls_ssl_update_handshake_status( ssl );
5838 }
Simon Butcher99000142016-10-13 17:21:01 +01005839 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005840 else
Simon Butcher99000142016-10-13 17:21:01 +01005841 {
Hanno Becker02f59072018-08-15 14:00:24 +01005842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005843 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005844 }
5845
5846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5847
5848 return( 0 );
5849}
5850
Hanno Becker40f50842018-08-15 14:48:01 +01005851#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005852static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005853{
Hanno Becker40f50842018-08-15 14:48:01 +01005854 if( ssl->in_left > ssl->next_record_offset )
5855 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005856
Hanno Becker40f50842018-08-15 14:48:01 +01005857 return( 0 );
5858}
5859
5860static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5861{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005862 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005863 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005864 int ret = 0;
5865
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005866 if( hs == NULL )
5867 return( -1 );
5868
Hanno Beckere00ae372018-08-20 09:39:42 +01005869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5870
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005871 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5872 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5873 {
5874 /* Check if we have seen a ChangeCipherSpec before.
5875 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005876 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005877 {
5878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5879 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005880 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005881 }
5882
Hanno Becker39b8bc92018-08-28 17:17:13 +01005883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005884 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5885 ssl->in_msglen = 1;
5886 ssl->in_msg[0] = 1;
5887
5888 /* As long as they are equal, the exact value doesn't matter. */
5889 ssl->in_left = 0;
5890 ssl->next_record_offset = 0;
5891
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005892 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005893 goto exit;
5894 }
Hanno Becker37f95322018-08-16 13:55:32 +01005895
Hanno Beckerb8f50142018-08-28 10:01:34 +01005896#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005897 /* Debug only */
5898 {
5899 unsigned offset;
5900 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5901 {
5902 hs_buf = &hs->buffering.hs[offset];
5903 if( hs_buf->is_valid == 1 )
5904 {
5905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5906 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005907 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005908 }
5909 }
5910 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005911#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005912
5913 /* Check if we have buffered and/or fully reassembled the
5914 * next handshake message. */
5915 hs_buf = &hs->buffering.hs[0];
5916 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5917 {
5918 /* Synthesize a record containing the buffered HS message. */
Arto Kinnunen84eeb4f2019-09-10 10:32:30 +03005919 size_t msg_len = mbedtls_platform_get_uint24_be( &hs_buf->data[1] );
Hanno Becker37f95322018-08-16 13:55:32 +01005920
5921 /* Double-check that we haven't accidentally buffered
5922 * a message that doesn't fit into the input buffer. */
5923 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5924 {
5925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5926 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5927 }
5928
5929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5930 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5931 hs_buf->data, msg_len + 12 );
5932
5933 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5934 ssl->in_hslen = msg_len + 12;
5935 ssl->in_msglen = msg_len + 12;
Teppo Järvelin91d79382019-10-02 09:09:31 +03005936 mbedtls_platform_memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
Hanno Becker37f95322018-08-16 13:55:32 +01005937
5938 ret = 0;
5939 goto exit;
5940 }
5941 else
5942 {
5943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5944 hs->in_msg_seq ) );
5945 }
5946
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005947 ret = -1;
5948
5949exit:
5950
5951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5952 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005953}
5954
Hanno Beckera02b0b42018-08-21 17:20:27 +01005955static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5956 size_t desired )
5957{
5958 int offset;
5959 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5961 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005962
Hanno Becker01315ea2018-08-21 17:22:17 +01005963 /* Get rid of future records epoch first, if such exist. */
5964 ssl_free_buffered_record( ssl );
5965
5966 /* Check if we have enough space available now. */
5967 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5968 hs->buffering.total_bytes_buffered ) )
5969 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005971 return( 0 );
5972 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005973
Hanno Becker4f432ad2018-08-28 10:02:32 +01005974 /* We don't have enough space to buffer the next expected handshake
5975 * message. Remove buffers used for future messages to gain space,
5976 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005977 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5978 offset >= 0; offset-- )
5979 {
5980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5981 offset ) );
5982
Hanno Beckerb309b922018-08-23 13:18:05 +01005983 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005984
5985 /* Check if we have enough space available now. */
5986 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5987 hs->buffering.total_bytes_buffered ) )
5988 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005990 return( 0 );
5991 }
5992 }
5993
5994 return( -1 );
5995}
5996
Hanno Becker40f50842018-08-15 14:48:01 +01005997static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5998{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005999 int ret = 0;
6000 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6001
6002 if( hs == NULL )
6003 return( 0 );
6004
6005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
6006
6007 switch( ssl->in_msgtype )
6008 {
6009 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
6010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01006011
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01006012 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006013 break;
6014
6015 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01006016 {
6017 unsigned recv_msg_seq_offset;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03006018 unsigned recv_msg_seq = (unsigned)
6019 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006020
Hanno Becker37f95322018-08-16 13:55:32 +01006021 mbedtls_ssl_hs_buffer *hs_buf;
6022 size_t msg_len = ssl->in_hslen - 12;
6023
6024 /* We should never receive an old handshake
6025 * message - double-check nonetheless. */
6026 if( recv_msg_seq < ssl->handshake->in_msg_seq )
6027 {
6028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6029 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6030 }
6031
6032 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
6033 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
6034 {
6035 /* Silently ignore -- message too far in the future */
6036 MBEDTLS_SSL_DEBUG_MSG( 2,
6037 ( "Ignore future HS message with sequence number %u, "
6038 "buffering window %u - %u",
6039 recv_msg_seq, ssl->handshake->in_msg_seq,
6040 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
6041
6042 goto exit;
6043 }
6044
6045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
6046 recv_msg_seq, recv_msg_seq_offset ) );
6047
6048 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
6049
6050 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01006051 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01006052 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006053 size_t reassembly_buf_sz;
6054
Hanno Becker37f95322018-08-16 13:55:32 +01006055 hs_buf->is_fragmented =
6056 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
6057
6058 /* We copy the message back into the input buffer
6059 * after reassembly, so check that it's not too large.
6060 * This is an implementation-specific limitation
6061 * and not one from the standard, hence it is not
6062 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01006063 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01006064 {
6065 /* Ignore message */
6066 goto exit;
6067 }
6068
Hanno Beckere0b150f2018-08-21 15:51:03 +01006069 /* Check if we have enough space to buffer the message. */
6070 if( hs->buffering.total_bytes_buffered >
6071 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
6072 {
6073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6074 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6075 }
6076
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006077 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
6078 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01006079
6080 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
6081 hs->buffering.total_bytes_buffered ) )
6082 {
6083 if( recv_msg_seq_offset > 0 )
6084 {
6085 /* If we can't buffer a future message because
6086 * of space limitations -- ignore. */
6087 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",
6088 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6089 (unsigned) hs->buffering.total_bytes_buffered ) );
6090 goto exit;
6091 }
Hanno Beckere1801392018-08-21 16:51:05 +01006092 else
6093 {
6094 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",
6095 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6096 (unsigned) hs->buffering.total_bytes_buffered ) );
6097 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006098
Hanno Beckera02b0b42018-08-21 17:20:27 +01006099 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006100 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01006101 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",
6102 (unsigned) msg_len,
6103 (unsigned) reassembly_buf_sz,
6104 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01006105 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006106 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6107 goto exit;
6108 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006109 }
6110
6111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
6112 msg_len ) );
6113
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006114 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
6115 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01006116 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01006117 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01006118 goto exit;
6119 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006120 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006121
6122 /* Prepare final header: copy msg_type, length and message_seq,
6123 * then add standardised fragment_offset and fragment_length */
Teppo Järvelin91d79382019-10-02 09:09:31 +03006124 mbedtls_platform_memcpy( hs_buf->data, ssl->in_msg, 6 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02006125 mbedtls_platform_memset( hs_buf->data + 6, 0, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006126 mbedtls_platform_memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
Hanno Becker37f95322018-08-16 13:55:32 +01006127
6128 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01006129
6130 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006131 }
6132 else
6133 {
6134 /* Make sure msg_type and length are consistent */
Teppo Järvelin0efac532019-10-04 13:21:08 +03006135 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 +01006136 {
6137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6138 /* Ignore */
6139 goto exit;
6140 }
6141 }
6142
Hanno Becker4422bbb2018-08-20 09:40:19 +01006143 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006144 {
6145 size_t frag_len, frag_off;
6146 unsigned char * const msg = hs_buf->data + 12;
6147
6148 /*
6149 * Check and copy current fragment
6150 */
6151
6152 /* Validation of header fields already done in
6153 * mbedtls_ssl_prepare_handshake_record(). */
6154 frag_off = ssl_get_hs_frag_off( ssl );
6155 frag_len = ssl_get_hs_frag_len( ssl );
6156
6157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6158 frag_off, frag_len ) );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006159 mbedtls_platform_memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
Hanno Becker37f95322018-08-16 13:55:32 +01006160
6161 if( hs_buf->is_fragmented )
6162 {
6163 unsigned char * const bitmask = msg + msg_len;
6164 ssl_bitmask_set( bitmask, frag_off, frag_len );
6165 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6166 msg_len ) == 0 );
6167 }
6168 else
6169 {
6170 hs_buf->is_complete = 1;
6171 }
6172
6173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6174 hs_buf->is_complete ? "" : "not yet " ) );
6175 }
6176
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006177 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006178 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006179
6180 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006181 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006182 break;
6183 }
6184
6185exit:
6186
6187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6188 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006189}
6190#endif /* MBEDTLS_SSL_PROTO_DTLS */
6191
Hanno Becker1097b342018-08-15 14:09:41 +01006192static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006193{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006194 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006195 * Consume last content-layer message and potentially
6196 * update in_msglen which keeps track of the contents'
6197 * consumption state.
6198 *
6199 * (1) Handshake messages:
6200 * Remove last handshake message, move content
6201 * and adapt in_msglen.
6202 *
6203 * (2) Alert messages:
6204 * Consume whole record content, in_msglen = 0.
6205 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006206 * (3) Change cipher spec:
6207 * Consume whole record content, in_msglen = 0.
6208 *
6209 * (4) Application data:
6210 * Don't do anything - the record layer provides
6211 * the application data as a stream transport
6212 * and consumes through mbedtls_ssl_read only.
6213 *
6214 */
6215
6216 /* Case (1): Handshake messages */
6217 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006218 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006219 /* Hard assertion to be sure that no application data
6220 * is in flight, as corrupting ssl->in_msglen during
6221 * ssl->in_offt != NULL is fatal. */
6222 if( ssl->in_offt != NULL )
6223 {
6224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6225 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6226 }
6227
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006228 /*
6229 * Get next Handshake message in the current record
6230 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006231
Hanno Becker4a810fb2017-05-24 16:27:30 +01006232 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006233 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006234 * current handshake content: If DTLS handshake
6235 * fragmentation is used, that's the fragment
6236 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006237 * size here is faulty and should be changed at
6238 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006239 * (2) While it doesn't seem to cause problems, one
6240 * has to be very careful not to assume that in_hslen
6241 * is always <= in_msglen in a sensible communication.
6242 * Again, it's wrong for DTLS handshake fragmentation.
6243 * The following check is therefore mandatory, and
6244 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006245 * Additionally, ssl->in_hslen might be arbitrarily out of
6246 * bounds after handling a DTLS message with an unexpected
6247 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006248 */
6249 if( ssl->in_hslen < ssl->in_msglen )
6250 {
6251 ssl->in_msglen -= ssl->in_hslen;
6252 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6253 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006254
Hanno Becker4a810fb2017-05-24 16:27:30 +01006255 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6256 ssl->in_msg, ssl->in_msglen );
6257 }
6258 else
6259 {
6260 ssl->in_msglen = 0;
6261 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006262
Hanno Becker4a810fb2017-05-24 16:27:30 +01006263 ssl->in_hslen = 0;
6264 }
6265 /* Case (4): Application data */
6266 else if( ssl->in_offt != NULL )
6267 {
6268 return( 0 );
6269 }
6270 /* Everything else (CCS & Alerts) */
6271 else
6272 {
6273 ssl->in_msglen = 0;
6274 }
6275
Hanno Becker1097b342018-08-15 14:09:41 +01006276 return( 0 );
6277}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006278
Hanno Beckere74d5562018-08-15 14:26:08 +01006279static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6280{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006281 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006282 return( 1 );
6283
6284 return( 0 );
6285}
6286
Hanno Becker5f066e72018-08-16 14:56:31 +01006287#if defined(MBEDTLS_SSL_PROTO_DTLS)
6288
6289static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6290{
6291 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6292 if( hs == NULL )
6293 return;
6294
Hanno Becker01315ea2018-08-21 17:22:17 +01006295 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006296 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006297 hs->buffering.total_bytes_buffered -=
6298 hs->buffering.future_record.len;
6299
6300 mbedtls_free( hs->buffering.future_record.data );
6301 hs->buffering.future_record.data = NULL;
6302 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006303}
6304
6305static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6306{
6307 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6308 unsigned char * rec;
6309 size_t rec_len;
6310 unsigned rec_epoch;
6311
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006312 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006313 return( 0 );
6314
6315 if( hs == NULL )
6316 return( 0 );
6317
Hanno Becker5f066e72018-08-16 14:56:31 +01006318 rec = hs->buffering.future_record.data;
6319 rec_len = hs->buffering.future_record.len;
6320 rec_epoch = hs->buffering.future_record.epoch;
6321
6322 if( rec == NULL )
6323 return( 0 );
6324
Hanno Becker4cb782d2018-08-20 11:19:05 +01006325 /* Only consider loading future records if the
6326 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006327 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006328 return( 0 );
6329
Hanno Becker5f066e72018-08-16 14:56:31 +01006330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6331
6332 if( rec_epoch != ssl->in_epoch )
6333 {
6334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6335 goto exit;
6336 }
6337
6338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6339
6340 /* Double-check that the record is not too large */
6341 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6342 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6343 {
6344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6345 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6346 }
6347
Teppo Järvelin91d79382019-10-02 09:09:31 +03006348 mbedtls_platform_memcpy( ssl->in_hdr, rec, rec_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006349 ssl->in_left = rec_len;
6350 ssl->next_record_offset = 0;
6351
6352 ssl_free_buffered_record( ssl );
6353
6354exit:
6355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6356 return( 0 );
6357}
6358
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006359static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6360 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006361{
6362 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006363
6364 /* Don't buffer future records outside handshakes. */
6365 if( hs == NULL )
6366 return( 0 );
6367
6368 /* Only buffer handshake records (we are only interested
6369 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006370 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006371 return( 0 );
6372
6373 /* Don't buffer more than one future epoch record. */
6374 if( hs->buffering.future_record.data != NULL )
6375 return( 0 );
6376
Hanno Becker01315ea2018-08-21 17:22:17 +01006377 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006378 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006379 hs->buffering.total_bytes_buffered ) )
6380 {
6381 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 +01006382 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006383 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006384 return( 0 );
6385 }
6386
Hanno Becker5f066e72018-08-16 14:56:31 +01006387 /* Buffer record */
6388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6389 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006390 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006391
6392 /* ssl_parse_record_header() only considers records
6393 * of the next epoch as candidates for buffering. */
6394 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006395 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006396
6397 hs->buffering.future_record.data =
6398 mbedtls_calloc( 1, hs->buffering.future_record.len );
6399 if( hs->buffering.future_record.data == NULL )
6400 {
6401 /* If we run out of RAM trying to buffer a
6402 * record from the next epoch, just ignore. */
6403 return( 0 );
6404 }
6405
Teppo Järvelin91d79382019-10-02 09:09:31 +03006406 mbedtls_platform_memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006407
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006408 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006409 return( 0 );
6410}
6411
6412#endif /* MBEDTLS_SSL_PROTO_DTLS */
6413
Hanno Beckere74d5562018-08-15 14:26:08 +01006414static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006415{
6416 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006417 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006418
Hanno Becker5f066e72018-08-16 14:56:31 +01006419#if defined(MBEDTLS_SSL_PROTO_DTLS)
6420 /* We might have buffered a future record; if so,
6421 * and if the epoch matches now, load it.
6422 * On success, this call will set ssl->in_left to
6423 * the length of the buffered record, so that
6424 * the calls to ssl_fetch_input() below will
6425 * essentially be no-ops. */
6426 ret = ssl_load_buffered_record( ssl );
6427 if( ret != 0 )
6428 return( ret );
6429#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006430
Hanno Becker8b09b732019-05-08 12:03:28 +01006431 /* Ensure that we have enough space available for the default form
6432 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6433 * with no space for CIDs counted in). */
6434 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6435 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006437 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006438 return( ret );
6439 }
6440
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006441 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6442 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006444#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006445 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006446 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006447 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6448 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006449 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006450 if( ret != 0 )
6451 return( ret );
6452
6453 /* Fall through to handling of unexpected records */
6454 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6455 }
6456
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006457 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6458 {
Hanno Becker87b56262019-07-10 14:37:41 +01006459#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006460 /* Reset in pointers to default state for TLS/DTLS records,
6461 * assuming no CID and no offset between record content and
6462 * record plaintext. */
6463 ssl_update_in_pointers( ssl );
6464
Hanno Becker69412452019-07-12 08:33:49 +01006465 /* Setup internal message pointers from record structure. */
6466 ssl->in_msgtype = rec.type;
6467#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6468 ssl->in_len = ssl->in_cid + rec.cid_len;
6469#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006470 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006471 ssl->in_msglen = rec.data_len;
6472
Hanno Becker87b56262019-07-10 14:37:41 +01006473 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02006474 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker87b56262019-07-10 14:37:41 +01006475 if( ret != 0 )
6476 return( ret );
6477#endif
6478
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006479 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006480 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006481
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6483 "(header)" ) );
6484 }
6485 else
6486 {
6487 /* Skip invalid record and the rest of the datagram */
6488 ssl->next_record_offset = 0;
6489 ssl->in_left = 0;
6490
6491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6492 "(header)" ) );
6493 }
6494
6495 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006496 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006497 }
Hanno Becker87b56262019-07-10 14:37:41 +01006498 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006499#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006500 {
6501 return( ret );
6502 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006503 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006505#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006506 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006507 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006508 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006509 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006510 if( ssl->next_record_offset < ssl->in_left )
6511 {
6512 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6513 }
6514 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006515 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006516#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006517#if defined(MBEDTLS_SSL_PROTO_TLS)
6518 {
Hanno Beckere0452772019-07-10 17:12:07 +01006519 /*
6520 * Fetch record contents from underlying transport.
6521 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006522 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006523 if( ret != 0 )
6524 {
6525 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6526 return( ret );
6527 }
6528
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006529 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006530 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006531#endif /* MBEDTLS_SSL_PROTO_TLS */
6532
6533 /*
6534 * Decrypt record contents.
6535 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006536
Hanno Beckera89610a2019-07-11 13:07:45 +01006537 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006539#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006540 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006541 {
6542 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006543 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006544 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006545 /* Except when waiting for Finished as a bad mac here
6546 * probably means something went wrong in the handshake
6547 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6548 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6549 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6550 {
6551#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6552 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6553 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006554 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006555 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6556 }
6557#endif
6558 return( ret );
6559 }
6560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006561#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006562 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6563 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6566 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006567 }
6568#endif
6569
Hanno Becker4a810fb2017-05-24 16:27:30 +01006570 /* As above, invalid records cause
6571 * dismissal of the whole datagram. */
6572
6573 ssl->next_record_offset = 0;
6574 ssl->in_left = 0;
6575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006577 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006578 }
6579
6580 return( ret );
6581 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006582 MBEDTLS_SSL_TRANSPORT_ELSE
6583#endif /* MBEDTLS_SSL_PROTO_DTLS */
6584#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006585 {
6586 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006587#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6588 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006589 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006590 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006592 }
6593#endif
6594 return( ret );
6595 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006596#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006597 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006598
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006599
6600 /* Reset in pointers to default state for TLS/DTLS records,
6601 * assuming no CID and no offset between record content and
6602 * record plaintext. */
6603 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006604#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6605 ssl->in_len = ssl->in_cid + rec.cid_len;
6606#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006607 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006608
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006609 /* The record content type may change during decryption,
6610 * so re-read it. */
6611 ssl->in_msgtype = rec.type;
6612 /* Also update the input buffer, because unfortunately
6613 * the server-side ssl_parse_client_hello() reparses the
6614 * record header when receiving a ClientHello initiating
6615 * a renegotiation. */
6616 ssl->in_hdr[0] = rec.type;
6617 ssl->in_msg = rec.buf + rec.data_offset;
6618 ssl->in_msglen = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006619 (void)mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len );
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006620
Manuel Pégourié-Gonnardae48d862020-01-03 12:18:49 +01006621#if defined(MBEDTLS_ZLIB_SUPPORT)
6622 if( ssl->transform_in != NULL &&
6623 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
6624 {
6625 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
6626 {
6627 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
6628 return( ret );
6629 }
6630
6631 /* Check actual (decompress) record content length against
6632 * configured maximum. */
6633 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
6634 {
6635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
6636 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6637 }
6638 }
6639#endif /* MBEDTLS_ZLIB_SUPPORT */
6640
Simon Butcher99000142016-10-13 17:21:01 +01006641 return( 0 );
6642}
6643
6644int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6645{
6646 int ret;
6647
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006648 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006649 * Handle particular types of records
6650 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006651 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006652 {
Simon Butcher99000142016-10-13 17:21:01 +01006653 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6654 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006655 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006656 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006657 }
6658
Hanno Beckere678eaa2018-08-21 14:57:46 +01006659 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006660 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006661 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006662 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006663 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6664 ssl->in_msglen ) );
6665 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006666 }
6667
Hanno Beckere678eaa2018-08-21 14:57:46 +01006668 if( ssl->in_msg[0] != 1 )
6669 {
6670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6671 ssl->in_msg[0] ) );
6672 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6673 }
6674
6675#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006676 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006677 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6678 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6679 {
6680 if( ssl->handshake == NULL )
6681 {
6682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6683 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6684 }
6685
6686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6687 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6688 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006689#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006690 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006693 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006694 if( ssl->in_msglen != 2 )
6695 {
6696 /* Note: Standard allows for more than one 2 byte alert
6697 to be packed in a single message, but Mbed TLS doesn't
6698 currently support this. */
6699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6700 ssl->in_msglen ) );
6701 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6702 }
6703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006705 ssl->in_msg[0], ssl->in_msg[1] ) );
6706
6707 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006708 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006713 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006715 }
6716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6718 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6721 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006722 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006723
6724#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6725 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6726 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6727 {
Hanno Becker90333da2017-10-10 11:27:13 +01006728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006729 /* Will be handled when trying to parse ServerHello */
6730 return( 0 );
6731 }
6732#endif
6733
6734#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006735 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006736 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6737 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006738 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6739 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6740 {
6741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6742 /* Will be handled in mbedtls_ssl_parse_certificate() */
6743 return( 0 );
6744 }
6745#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6746
6747 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006748 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006749 }
6750
Hanno Beckerc76c6192017-06-06 10:03:17 +01006751#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006752 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006753 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006754 /* Drop unexpected ApplicationData records,
6755 * except at the beginning of renegotiations */
6756 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6757 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6758#if defined(MBEDTLS_SSL_RENEGOTIATION)
6759 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6760 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006761#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006762 )
6763 {
6764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6765 return( MBEDTLS_ERR_SSL_NON_FATAL );
6766 }
6767
6768 if( ssl->handshake != NULL &&
6769 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6770 {
6771 ssl_handshake_wrapup_free_hs_transform( ssl );
6772 }
6773 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006774#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006775
Paul Bakker5121ce52009-01-03 21:22:43 +00006776 return( 0 );
6777}
6778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006780{
6781 int ret;
6782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6784 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6785 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006786 {
6787 return( ret );
6788 }
6789
6790 return( 0 );
6791}
6792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006793int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006794 unsigned char level,
6795 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006796{
6797 int ret;
6798
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006799 if( ssl == NULL || ssl->conf == NULL )
6800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006803 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006805 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006806 ssl->out_msglen = 2;
6807 ssl->out_msg[0] = level;
6808 ssl->out_msg[1] = message;
6809
Hanno Becker67bc7c32018-08-06 11:33:50 +01006810 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006813 return( ret );
6814 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006816
6817 return( 0 );
6818}
6819
Hanno Becker17572472019-02-08 07:19:04 +00006820#if defined(MBEDTLS_X509_CRT_PARSE_C)
6821static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6822{
6823#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6824 if( session->peer_cert != NULL )
6825 {
6826 mbedtls_x509_crt_free( session->peer_cert );
6827 mbedtls_free( session->peer_cert );
6828 session->peer_cert = NULL;
6829 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006830#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006831 if( session->peer_cert_digest != NULL )
6832 {
6833 /* Zeroization is not necessary. */
6834 mbedtls_free( session->peer_cert_digest );
6835 session->peer_cert_digest = NULL;
6836 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6837 session->peer_cert_digest_len = 0;
6838 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006839#else
6840 ((void) session);
6841#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006842}
6843#endif /* MBEDTLS_X509_CRT_PARSE_C */
6844
Paul Bakker5121ce52009-01-03 21:22:43 +00006845/*
6846 * Handshake functions
6847 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006848#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006849/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006851{
Hanno Beckerdf645962019-06-26 13:02:22 +01006852 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6853 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006856
Hanno Becker5097cba2019-02-05 13:36:46 +00006857 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006860 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6861 {
6862 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6863 }
6864 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6865 {
6866 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6867 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006868 else
6869 {
6870 ssl->state = MBEDTLS_SSL_INVALID;
6871 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006872 return( 0 );
6873 }
6874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6876 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006877}
6878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006879int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006880{
Hanno Beckerdf645962019-06-26 13:02:22 +01006881 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6882 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006885
Hanno Becker5097cba2019-02-05 13:36:46 +00006886 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006889 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6890 {
6891 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6892 }
6893 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6894 {
6895 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6896 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006897 else
6898 {
6899 ssl->state = MBEDTLS_SSL_INVALID;
6900 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006901 return( 0 );
6902 }
6903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6905 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006906}
Gilles Peskinef9828522017-05-03 12:28:43 +02006907
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006908#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006909/* Some certificate support -> implement write and parse */
6910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006911int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006912{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006913 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006914 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006915 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006916 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6917 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006920
Hanno Becker5097cba2019-02-05 13:36:46 +00006921 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006924 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6925 {
6926 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6927 }
6928 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6929 {
6930 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6931 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006932 else
6933 {
6934 ssl->state = MBEDTLS_SSL_INVALID;
6935 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006936 return( 0 );
6937 }
6938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006940 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6941 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006942 {
6943 if( ssl->client_auth == 0 )
6944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006946 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6947 {
6948 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6949 }
6950 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6951 {
6952 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6953 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006954 else
6955 {
6956 ssl->state = MBEDTLS_SSL_INVALID;
6957 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006958 return( 0 );
6959 }
6960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006962 /*
6963 * If using SSLv3 and got no cert, send an Alert message
6964 * (otherwise an empty Certificate message will be sent).
6965 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006967 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006968 {
6969 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6971 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6972 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006975 goto write_msg;
6976 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006978 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006979#endif /* MBEDTLS_SSL_CLI_C */
6980#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006981 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6986 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006987 }
6988 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006989#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006992
6993 /*
6994 * 0 . 0 handshake type
6995 * 1 . 3 handshake length
6996 * 4 . 6 length of all certs
6997 * 7 . 9 length of cert. 1
6998 * 10 . n-1 peer certificate
6999 * n . n+2 length of cert. 2
7000 * n+3 . ... upper level cert, etc.
7001 */
7002 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007003 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007004
Paul Bakker29087132010-03-21 21:03:34 +00007005 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007006 {
7007 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10007008 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00007009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10007011 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007012 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007013 }
7014
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03007015 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007016
Teppo Järvelin91d79382019-10-02 09:09:31 +03007017 i += 3; mbedtls_platform_memcpy( ssl->out_msg + i, crt->raw.p, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007018 i += n; crt = crt->next;
7019 }
7020
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03007021 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007022
7023 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007024 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7025 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007026
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007027#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007028write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007029#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007030
Jarno Lamsa2b205162019-11-12 15:36:21 +02007031 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7032 {
7033 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7034 }
7035 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7036 {
7037 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7038 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007039 else
7040 {
7041 ssl->state = MBEDTLS_SSL_INVALID;
7042 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007043
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007044 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007045 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007046 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007047 return( ret );
7048 }
7049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007051
Paul Bakkered27a042013-04-18 22:46:23 +02007052 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007053}
7054
Hanno Becker285ff0c2019-01-31 07:44:03 +00007055#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00007056
7057#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00007058static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7059 unsigned char *crt_buf,
7060 size_t crt_buf_len )
7061{
7062 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7063
7064 if( peer_crt == NULL )
7065 return( -1 );
7066
7067 if( peer_crt->raw.len != crt_buf_len )
7068 return( -1 );
7069
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007070 return( mbedtls_platform_memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007071}
Hanno Becker5882dd02019-06-06 16:25:57 +01007072#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00007073static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7074 unsigned char *crt_buf,
7075 size_t crt_buf_len )
7076{
7077 int ret;
7078 unsigned char const * const peer_cert_digest =
7079 ssl->session->peer_cert_digest;
7080 mbedtls_md_type_t const peer_cert_digest_type =
7081 ssl->session->peer_cert_digest_type;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007082 mbedtls_md_handle_t digest_info =
Hanno Beckerdf759382019-02-05 17:02:46 +00007083 mbedtls_md_info_from_type( peer_cert_digest_type );
7084 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7085 size_t digest_len;
7086
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007087 if( peer_cert_digest == NULL ||
7088 digest_info == MBEDTLS_MD_INVALID_HANDLE )
7089 {
Hanno Beckerdf759382019-02-05 17:02:46 +00007090 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007091 }
Hanno Beckerdf759382019-02-05 17:02:46 +00007092
7093 digest_len = mbedtls_md_get_size( digest_info );
7094 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
7095 return( -1 );
7096
7097 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
7098 if( ret != 0 )
7099 return( -1 );
7100
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007101 return( mbedtls_platform_memcmp( tmp_digest, peer_cert_digest, digest_len ) );
Hanno Beckerdf759382019-02-05 17:02:46 +00007102}
Hanno Becker5882dd02019-06-06 16:25:57 +01007103#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00007104#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00007105
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007106/*
7107 * Once the certificate message is read, parse it into a cert chain and
7108 * perform basic checks, but leave actual verification to the caller
7109 */
Hanno Becker35e41772019-02-05 15:37:23 +00007110static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
7111 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00007112{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007113 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00007114#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7115 int crt_cnt=0;
7116#endif
Paul Bakker23986e52011-04-24 08:57:21 +00007117 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02007118 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00007119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007120 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007123 mbedtls_ssl_pend_fatal_alert( ssl,
7124 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007126 }
7127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007128 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
7129 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007132 mbedtls_ssl_pend_fatal_alert( ssl,
7133 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007135 }
7136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007137 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007138
Paul Bakker5121ce52009-01-03 21:22:43 +00007139 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00007141 */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007142 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007143
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007144 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007145 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007148 mbedtls_ssl_pend_fatal_alert( ssl,
7149 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007150 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007151 }
7152
Hanno Becker33c3dc82019-01-30 14:46:46 +00007153 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7154 i += 3;
7155
Hanno Becker33c3dc82019-01-30 14:46:46 +00007156 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007157 while( i < ssl->in_hslen )
7158 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007159 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02007160 if ( i + 3 > ssl->in_hslen ) {
7161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007162 mbedtls_ssl_pend_fatal_alert( ssl,
7163 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02007164 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
7165 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007166 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7167 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007168 if( ssl->in_msg[i] != 0 )
7169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007171 mbedtls_ssl_pend_fatal_alert( ssl,
7172 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007174 }
7175
Hanno Becker33c3dc82019-01-30 14:46:46 +00007176 /* Read length of the next CRT in the chain. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007177 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007178 i += 3;
7179
7180 if( n < 128 || i + n > ssl->in_hslen )
7181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007183 mbedtls_ssl_pend_fatal_alert( ssl,
7184 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007186 }
7187
Hanno Becker33c3dc82019-01-30 14:46:46 +00007188 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00007189#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7190 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01007191 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7192 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00007193 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007194 {
Hanno Becker68b856d2019-02-08 14:00:04 +00007195 /* During client-side renegotiation, check that the server's
7196 * end-CRTs hasn't changed compared to the initial handshake,
7197 * mitigating the triple handshake attack. On success, reuse
7198 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00007199 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7200 if( ssl_check_peer_crt_unchanged( ssl,
7201 &ssl->in_msg[i],
7202 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007203 {
Hanno Becker35e41772019-02-05 15:37:23 +00007204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007205 mbedtls_ssl_pend_fatal_alert( ssl,
7206 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007207 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007208 }
Hanno Becker35e41772019-02-05 15:37:23 +00007209
7210 /* Now we can safely free the original chain. */
7211 ssl_clear_peer_cert( ssl->session );
7212 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007213#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7214
Hanno Becker33c3dc82019-01-30 14:46:46 +00007215 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007216#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007217 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007218#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007219 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007220 * it in-place from the input buffer instead of making a copy. */
7221 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7222#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007223 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007224 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007225 case 0: /*ok*/
7226 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7227 /* Ignore certificate with an unknown algorithm: maybe a
7228 prior certificate was already trusted. */
7229 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007230
Hanno Becker33c3dc82019-01-30 14:46:46 +00007231 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7232 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7233 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007234
Hanno Becker33c3dc82019-01-30 14:46:46 +00007235 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7236 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7237 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007238
Hanno Becker33c3dc82019-01-30 14:46:46 +00007239 default:
7240 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7241 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007242 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007243 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7244 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007245 }
7246
7247 i += n;
7248 }
7249
Hanno Becker35e41772019-02-05 15:37:23 +00007250 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007251 return( 0 );
7252}
7253
Hanno Beckerb8a08572019-02-05 12:49:06 +00007254#if defined(MBEDTLS_SSL_SRV_C)
7255static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7256{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007257 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007258 return( -1 );
7259
7260#if defined(MBEDTLS_SSL_PROTO_SSL3)
7261 /*
7262 * Check if the client sent an empty certificate
7263 */
Hanno Becker2881d802019-05-22 14:44:53 +01007264 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007265 {
7266 if( ssl->in_msglen == 2 &&
7267 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7268 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7269 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7270 {
7271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7272 return( 0 );
7273 }
7274
7275 return( -1 );
7276 }
7277#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7278
7279#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7280 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7281 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7282 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7283 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Teppo Järvelin0efac532019-10-04 13:21:08 +03007284 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 +00007285 {
7286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7287 return( 0 );
7288 }
7289
Hanno Beckerb8a08572019-02-05 12:49:06 +00007290#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7291 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007292
7293 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007294}
7295#endif /* MBEDTLS_SSL_SRV_C */
7296
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007297/* Check if a certificate message is expected.
7298 * Return either
7299 * - SSL_CERTIFICATE_EXPECTED, or
7300 * - SSL_CERTIFICATE_SKIP
7301 * indicating whether a Certificate message is expected or not.
7302 */
7303#define SSL_CERTIFICATE_EXPECTED 0
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007304#define SSL_CERTIFICATE_SKIP 0xff
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007305static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7306 int authmode )
7307{
Hanno Becker473f98f2019-06-26 10:27:32 +01007308 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007309 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007310
7311 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7312 return( SSL_CERTIFICATE_SKIP );
7313
7314#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007315 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007316 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007317 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7318 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7319 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007320 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007321 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007322
7323 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7324 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007325 ssl->session_negotiate->verify_result =
7326 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7327 return( SSL_CERTIFICATE_SKIP );
7328 }
7329 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007330#else
7331 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007332#endif /* MBEDTLS_SSL_SRV_C */
7333
7334 return( SSL_CERTIFICATE_EXPECTED );
7335}
7336
Hanno Becker3cf50612019-02-05 14:36:34 +00007337static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007338 volatile int authmode,
Hanno Becker3cf50612019-02-05 14:36:34 +00007339 mbedtls_x509_crt *chain,
7340 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007341{
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007342 volatile int verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Andrzej Kurekfd56f402020-05-25 11:52:05 -04007343 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007344 volatile int flow_counter = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01007345 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007346 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007347 mbedtls_x509_crt *ca_chain;
7348 mbedtls_x509_crl *ca_crl;
7349
7350 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
Jarno Lamsae1621d42019-12-19 08:58:56 +02007351 {
Hanno Becker3cf50612019-02-05 14:36:34 +00007352 return( 0 );
Jarno Lamsae1621d42019-12-19 08:58:56 +02007353 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007354
Jarno Lamsae1621d42019-12-19 08:58:56 +02007355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Hanno Becker3cf50612019-02-05 14:36:34 +00007356#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7357 if( ssl->handshake->sni_ca_chain != NULL )
7358 {
7359 ca_chain = ssl->handshake->sni_ca_chain;
7360 ca_crl = ssl->handshake->sni_ca_crl;
7361 }
7362 else
7363#endif
7364 {
7365 ca_chain = ssl->conf->ca_chain;
7366 ca_crl = ssl->conf->ca_crl;
7367 }
7368
7369 /*
7370 * Main check: verify certificate
7371 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007372 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007373 chain,
7374 ca_chain, ca_crl,
7375 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007376#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007377 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007378#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007379 &ssl->session_negotiate->verify_result,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01007380#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
7381 ssl->conf->f_vrfy, ssl->conf->p_vrfy,
7382#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
7383 rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007384
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007385 if( verify_ret == 0 )
7386 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007387 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007388 if( verify_ret == 0 )
7389 {
7390 flow_counter++;
7391 }
7392 else
7393 {
7394 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7395 }
7396 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007397 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007398 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007399 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007400 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007401 }
7402
7403#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007404 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007405 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7406#endif
7407
7408 /*
7409 * Secondary checks: always done, but change 'ret' only if it was 0
7410 */
7411
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007412#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007413 {
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007414#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007415 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007416#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007417 mbedtls_pk_context *pk;
7418 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7419 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007420 {
7421 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007422 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007423 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007424
7425 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007426 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007427 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007428 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007429 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007430
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007431 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007432#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007433
7434 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007435 {
7436 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7437
7438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007439 if( verify_ret == 0 )
7440 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007441 flow_counter++;
7442 }
7443 if( ret == 0 )
7444 {
7445 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007446 }
7447 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007448#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007449
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007450 ret = mbedtls_ssl_check_cert_usage( chain,
Hanno Becker3cf50612019-02-05 14:36:34 +00007451 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007452 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7453 MBEDTLS_SSL_IS_CLIENT ),
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007454 &ssl->session_negotiate->verify_result );
7455 if( ret == 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007456 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007457 flow_counter++;
7458 }
7459 else
7460 {
7461 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007463 if( verify_ret == 0 )
7464 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007465 }
7466
7467 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7468 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7469 * with details encoded in the verification flags. All other kinds
7470 * of error codes, including those from the user provided f_vrfy
7471 * functions, are treated as fatal and lead to a failure of
7472 * ssl_parse_certificate even if verification was optional. */
7473 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007474 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7475 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007476 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007477 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007478 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7479 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7480 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7481 {
7482 verify_ret = 0;
7483 flow_counter++;
7484 }
7485 else
7486 {
7487 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7488 }
7489 } else {
7490 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007491 }
7492
7493 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7494 {
7495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007496 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007497 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007498 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007499 else
7500 {
7501 flow_counter++;
7502 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007503
Hanno Becker8c13ee62019-02-26 16:48:17 +00007504 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007505 {
7506 uint8_t alert;
7507
7508 /* The certificate may have been rejected for several reasons.
7509 Pick one and send the corresponding alert. Which alert to send
7510 may be a subject of debate in some cases. */
7511 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7512 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7513 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7514 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7515 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7516 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7517 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7518 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7519 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7520 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7521 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7522 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7523 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7524 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7525 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7526 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7527 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7528 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7529 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7530 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7531 else
7532 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007533 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007534 }
7535
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007536 if( verify_ret == 0 &&
7537#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7538 flow_counter == 5 )
7539#else
7540 flow_counter == 4 )
7541#endif
7542 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007543 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007544 if( verify_ret == 0 &&
7545#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7546 flow_counter == 5 )
7547#else
7548 flow_counter == 4 )
7549#endif
7550 {
Jarno Lamsae1621d42019-12-19 08:58:56 +02007551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER AUTHENTICATED" ) );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007552 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
7553 }
7554 else
7555 {
7556 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7557 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007558 } else {
7559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER NOT AUTHENTICATED, %d", flow_counter));
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007560 }
7561
Hanno Becker3cf50612019-02-05 14:36:34 +00007562#if defined(MBEDTLS_DEBUG_C)
7563 if( ssl->session_negotiate->verify_result != 0 )
7564 {
7565 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7566 ssl->session_negotiate->verify_result ) );
7567 }
7568 else
7569 {
7570 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7571 }
7572#endif /* MBEDTLS_DEBUG_C */
7573
Hanno Becker8c13ee62019-02-26 16:48:17 +00007574 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007575}
7576
Hanno Becker34106f62019-02-08 14:59:05 +00007577#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007578
7579#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007580static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7581 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007582{
7583 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007584 /* Remember digest of the peer's end-CRT. */
7585 ssl->session_negotiate->peer_cert_digest =
7586 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7587 if( ssl->session_negotiate->peer_cert_digest == NULL )
7588 {
7589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7590 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007591 mbedtls_ssl_pend_fatal_alert( ssl,
7592 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007593
7594 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7595 }
7596
7597 ret = mbedtls_md( mbedtls_md_info_from_type(
7598 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7599 start, len,
7600 ssl->session_negotiate->peer_cert_digest );
7601
7602 ssl->session_negotiate->peer_cert_digest_type =
7603 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7604 ssl->session_negotiate->peer_cert_digest_len =
7605 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7606
7607 return( ret );
7608}
Hanno Becker5882dd02019-06-06 16:25:57 +01007609#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007610
7611static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7612 unsigned char *start, size_t len )
7613{
7614 unsigned char *end = start + len;
7615 int ret;
7616
7617 /* Make a copy of the peer's raw public key. */
7618 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7619 ret = mbedtls_pk_parse_subpubkey( &start, end,
7620 &ssl->handshake->peer_pubkey );
7621 if( ret != 0 )
7622 {
7623 /* We should have parsed the public key before. */
7624 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7625 }
7626
Manuel Pégourié-Gonnard2829bbf2019-09-19 10:45:14 +02007627 ssl->handshake->got_peer_pubkey = 1;
Hanno Becker34106f62019-02-08 14:59:05 +00007628 return( 0 );
7629}
7630#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7631
Hanno Becker3cf50612019-02-05 14:36:34 +00007632int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7633{
7634 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007635 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007636#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7637 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7638 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007639 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007640#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007641 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007642#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007643 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007644 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007645
7646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7647
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007648 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7649 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007650 {
7651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007652 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007653 }
7654
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007655#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7656 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007657 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007658 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007659 chain = ssl->handshake->ecrs_peer_cert;
7660 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007661 goto crt_verify;
7662 }
7663#endif
7664
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007665 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007666 {
7667 /* mbedtls_ssl_read_record may have sent an alert already. We
7668 let it decide whether to alert. */
7669 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007670 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007671 }
7672
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007673#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007674 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7675 {
7676 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007677
Hanno Beckerb8a08572019-02-05 12:49:06 +00007678 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007679 ret = 0;
7680 else
7681 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007682
Hanno Becker613d4902019-02-05 13:11:17 +00007683 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007684 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007685#endif /* MBEDTLS_SSL_SRV_C */
7686
Hanno Becker35e41772019-02-05 15:37:23 +00007687 /* Clear existing peer CRT structure in case we tried to
7688 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007689 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007690
7691 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7692 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007693 {
7694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7695 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007696 mbedtls_ssl_pend_fatal_alert( ssl,
7697 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007698
Hanno Beckere4aeb762019-02-05 17:19:52 +00007699 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7700 goto exit;
7701 }
7702 mbedtls_x509_crt_init( chain );
7703
7704 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007705 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007706 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007707
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007708#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7709 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007710 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007711
7712crt_verify:
7713 if( ssl->handshake->ecrs_enabled)
7714 rs_ctx = &ssl->handshake->ecrs_ctx;
7715#endif
7716
Hanno Becker3cf50612019-02-05 14:36:34 +00007717 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007718 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007719 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007720 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007721
Hanno Becker3008d282019-02-05 17:02:28 +00007722#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007723 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007724 size_t pk_len;
7725 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007726
Hanno Becker34106f62019-02-08 14:59:05 +00007727 /* We parse the CRT chain without copying, so
7728 * these pointers point into the input buffer,
7729 * and are hence still valid after freeing the
7730 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007731
Hanno Becker5882dd02019-06-06 16:25:57 +01007732#if defined(MBEDTLS_SSL_RENEGOTIATION)
7733 unsigned char *crt_start;
7734 size_t crt_len;
7735
Hanno Becker34106f62019-02-08 14:59:05 +00007736 crt_start = chain->raw.p;
7737 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007738#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007739
Hanno Becker8c13ee62019-02-26 16:48:17 +00007740 pk_start = chain->cache->pk_raw.p;
7741 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007742
7743 /* Free the CRT structures before computing
7744 * digest and copying the peer's public key. */
7745 mbedtls_x509_crt_free( chain );
7746 mbedtls_free( chain );
7747 chain = NULL;
7748
Hanno Becker5882dd02019-06-06 16:25:57 +01007749#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007750 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007751 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007752 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007753#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007754
Hanno Becker34106f62019-02-08 14:59:05 +00007755 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007756 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007757 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007758 }
Hanno Becker34106f62019-02-08 14:59:05 +00007759#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7760 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007761 ssl->session_negotiate->peer_cert = chain;
7762 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007763#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007766
Hanno Becker613d4902019-02-05 13:11:17 +00007767exit:
7768
Hanno Beckere4aeb762019-02-05 17:19:52 +00007769 if( ret == 0 )
Jarno Lamsa2b205162019-11-12 15:36:21 +02007770 {
7771 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7772 {
7773 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7774 }
7775 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7776 {
7777 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7778 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007779 else
7780 {
7781 ssl->state = MBEDTLS_SSL_INVALID;
7782 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02007783 }
Hanno Beckere4aeb762019-02-05 17:19:52 +00007784
7785#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7786 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7787 {
7788 ssl->handshake->ecrs_peer_cert = chain;
7789 chain = NULL;
7790 }
7791#endif
7792
7793 if( chain != NULL )
7794 {
7795 mbedtls_x509_crt_free( chain );
7796 mbedtls_free( chain );
7797 }
7798
Paul Bakker5121ce52009-01-03 21:22:43 +00007799 return( ret );
7800}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007801#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007804{
7805 int ret;
7806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007810 ssl->out_msglen = 1;
7811 ssl->out_msg[0] = 1;
7812
Jarno Lamsa2b205162019-11-12 15:36:21 +02007813 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7814 {
7815 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7816 }
7817 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7818 {
7819 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7820 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007821 else
7822 {
7823 ssl->state = MBEDTLS_SSL_INVALID;
7824 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007825
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007826 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007827 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007828 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007829 return( ret );
7830 }
7831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007833
7834 return( 0 );
7835}
7836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007837int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007838{
7839 int ret;
7840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007842
Hanno Becker327c93b2018-08-15 13:56:18 +01007843 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007846 return( ret );
7847 }
7848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007852 mbedtls_ssl_pend_fatal_alert( ssl,
7853 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007855 }
7856
Hanno Beckere678eaa2018-08-21 14:57:46 +01007857 /* CCS records are only accepted if they have length 1 and content '1',
7858 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007859
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007860 /*
7861 * Switch to our negotiated transform and session parameters for inbound
7862 * data.
7863 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007865 ssl->transform_in = ssl->transform_negotiate;
7866 ssl->session_in = ssl->session_negotiate;
7867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007869 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007872 ssl_dtls_replay_reset( ssl );
7873#endif
7874
7875 /* Increment epoch */
7876 if( ++ssl->in_epoch == 0 )
7877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007879 /* This is highly unlikely to happen for legitimate reasons, so
7880 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007882 }
7883 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007884 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007886#if defined(MBEDTLS_SSL_PROTO_TLS)
7887 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02007888 mbedtls_platform_memset( ssl->in_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007889 }
7890#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007891
Hanno Beckerf5970a02019-05-08 09:38:41 +01007892 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7895 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007897 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007900 mbedtls_ssl_pend_fatal_alert( ssl,
7901 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007902 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007903 }
7904 }
7905#endif
7906
Jarno Lamsa2b205162019-11-12 15:36:21 +02007907 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7908 {
7909 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7910 }
7911 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7912 {
7913 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7914 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007915 else
7916 {
7917 ssl->state = MBEDTLS_SSL_INVALID;
7918 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007921
7922 return( 0 );
7923}
7924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007926{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7928 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007929 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007930 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007931#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7933#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007934 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007935#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007937 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007938#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007940}
7941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007942static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007943{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007945
7946 /*
7947 * Free our handshake params
7948 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007949 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007951 ssl->handshake = NULL;
7952
7953 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007954 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007955 */
7956 if( ssl->transform )
7957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958 mbedtls_ssl_transform_free( ssl->transform );
7959 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007960 }
7961 ssl->transform = ssl->transform_negotiate;
7962 ssl->transform_negotiate = NULL;
7963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007965}
7966
Jarno Lamsae1621d42019-12-19 08:58:56 +02007967int mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007968{
Andrzej Kurekfd56f402020-05-25 11:52:05 -04007969 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007970
7971#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Jarno Lamsa015aa442019-12-20 12:09:37 +02007972 volatile const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007973 ? ssl->handshake->sni_authmode
7974 : mbedtls_ssl_conf_get_authmode( ssl->conf );
7975#else
Jarno Lamsa015aa442019-12-20 12:09:37 +02007976 volatile const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007977#endif
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007978#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7979 volatile int crt_expected = SSL_CERTIFICATE_EXPECTED;
7980 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7981#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007984#if defined(MBEDTLS_SSL_RENEGOTIATION)
7985 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007987 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007988 ssl->renego_records_seen = 0;
7989 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007990#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007991
7992 /*
7993 * Free the previous session and switch in the current one
7994 */
Paul Bakker0a597072012-09-25 21:55:46 +00007995 if( ssl->session )
7996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007998 /* RFC 7366 3.1: keep the EtM state */
7999 ssl->session_negotiate->encrypt_then_mac =
8000 ssl->session->encrypt_then_mac;
8001#endif
8002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008003 mbedtls_ssl_session_free( ssl->session );
8004 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00008005 }
8006 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00008007 ssl->session_negotiate = NULL;
8008
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02008009#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00008010 /*
8011 * Add cache entry
8012 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008013 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02008014 ssl->session->id_len != 0 &&
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008015 ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_UNSET )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02008016 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008017 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02008019 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02008020#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00008021
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008022 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8023 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8024#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8025 crt_expected == SSL_CERTIFICATE_SKIP )
8026#else
8027 1 )
8028#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008029 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008030 mbedtls_platform_random_delay();
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008031 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8032 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8033#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8034 crt_expected == SSL_CERTIFICATE_SKIP )
8035#else
8036 1 )
8037#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008038 {
8039 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
8040 }
8041 else
8042 {
8043 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8044 goto cleanup;
8045 }
8046 }
8047
Jarno Lamsae1621d42019-12-19 08:58:56 +02008048#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008049 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008050 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008051 mbedtls_platform_random_delay();
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008052 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008053 {
Jarno Lamsa06164052019-12-19 14:40:36 +02008054 /* When doing session resume, no premaster or peer authentication */
Jarno Lamsae1621d42019-12-19 08:58:56 +02008055 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsa06164052019-12-19 14:40:36 +02008056 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008057 }
8058 else
8059 {
8060 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008061 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008062 }
8063 }
8064#endif
8065
8066 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8067 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008068 mbedtls_platform_random_delay();
Jarno Lamsae1621d42019-12-19 08:58:56 +02008069 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8070 {
8071 ret = 0;
8072 }
8073 else
8074 {
8075 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008076 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008077 }
8078 }
8079 else
8080 {
8081 ret = MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008082 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008083 }
8084
Jarno Lamsa06164052019-12-19 14:40:36 +02008085 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8086 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8087 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8088 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008089 mbedtls_platform_random_delay();
Jarno Lamsa06164052019-12-19 14:40:36 +02008090 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8091 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8092 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8093 {
8094 ret = 0;
8095 }
8096 else
8097 {
8098 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8099 goto cleanup;
8100 }
8101 }
8102 else
8103 {
8104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "hello random %d", ssl->handshake->hello_random_set ) );
8105 MBEDTLS_SSL_DEBUG_MSG( 3, ( "key_derivation_done %d", ssl->handshake->key_derivation_done ) );
8106 MBEDTLS_SSL_DEBUG_MSG( 3, ( "premaster_generated %d", ssl->handshake->premaster_generated ) );
8107 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8108 }
8109
8110cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008111#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008112 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008113 ssl->handshake->flight != NULL )
8114 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008115 /* Cancel handshake timer */
8116 ssl_set_timer( ssl, 0 );
8117
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008118 /* Keep last flight around in case we need to resend it:
8119 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008120 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008121 }
8122 else
8123#endif
8124 ssl_handshake_wrapup_free_hs_transform( ssl );
8125
Jarno Lamsa2b205162019-11-12 15:36:21 +02008126 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Paul Bakker48916f92012-09-16 19:57:18 +00008127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008128 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Jarno Lamsae1621d42019-12-19 08:58:56 +02008129 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00008130}
8131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008132int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00008133{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008134 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00008135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008137
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008138 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01008139
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008140 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8141 mbedtls_ssl_suite_get_mac(
8142 mbedtls_ssl_ciphersuite_from_id(
8143 mbedtls_ssl_session_get_ciphersuite(
8144 ssl->session_negotiate ) ) ),
8145 ssl, ssl->out_msg + 4,
8146 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008147
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01008148 /*
8149 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8150 * may define some other value. Currently (early 2016), no defined
8151 * ciphersuite does this (and this is unlikely to change as activity has
8152 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8153 */
Hanno Becker2881d802019-05-22 14:44:53 +01008154 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008157 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008158 mbedtls_platform_memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008159#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008160
Paul Bakker5121ce52009-01-03 21:22:43 +00008161 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008162 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8163 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008164
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008165#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00008166 /*
8167 * In case of session resuming, invert the client and server
8168 * ChangeCipherSpec messages order.
8169 */
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008170 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008172#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008173 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8174 MBEDTLS_SSL_IS_CLIENT )
8175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008176 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008177 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008178#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008179#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008180 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8181 MBEDTLS_SSL_IS_SERVER )
8182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008184 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008185#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008186 }
8187 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008188#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008189 {
8190 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8191 {
8192 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8193 }
8194 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8195 {
8196 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8197 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008198 else
8199 {
8200 ssl->state = MBEDTLS_SSL_INVALID;
8201 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008202 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008203
Paul Bakker48916f92012-09-16 19:57:18 +00008204 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008205 * Switch to our negotiated transform and session parameters for outbound
8206 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008208 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008210#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008211 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008212 {
8213 unsigned char i;
8214
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008215 /* Remember current epoch settings for resending */
8216 ssl->handshake->alt_transform_out = ssl->transform_out;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008217 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008218
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008219 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008220 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008221
8222 /* Increment epoch */
8223 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008224 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008225 break;
8226
8227 /* The loop goes to its end iff the counter is wrapping */
8228 if( i == 0 )
8229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8231 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008232 }
8233 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008234 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008236#if defined(MBEDTLS_SSL_PROTO_TLS)
8237 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008238 mbedtls_platform_memset( ssl->cur_out_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008239 }
8240#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008241
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008242 ssl->transform_out = ssl->transform_negotiate;
8243 ssl->session_out = ssl->session_negotiate;
8244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008245#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8246 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008248 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008250 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8251 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008252 }
8253 }
8254#endif
8255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008256#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008257 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008258 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008259#endif
8260
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008261 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008262 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008263 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008264 return( ret );
8265 }
8266
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008267#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008268 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008269 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8270 {
8271 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8272 return( ret );
8273 }
8274#endif
8275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008277
8278 return( 0 );
8279}
8280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008281#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008282#define SSL_MAX_HASH_LEN 36
8283#else
8284#define SSL_MAX_HASH_LEN 12
8285#endif
8286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008288{
Paul Bakker23986e52011-04-24 08:57:21 +00008289 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008290 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008291 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008294
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008295 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8296 mbedtls_ssl_suite_get_mac(
8297 mbedtls_ssl_ciphersuite_from_id(
8298 mbedtls_ssl_session_get_ciphersuite(
8299 ssl->session_negotiate ) ) ),
8300 ssl, buf,
8301 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008302
Hanno Becker327c93b2018-08-15 13:56:18 +01008303 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008305 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008306 return( ret );
8307 }
8308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008309 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008312 mbedtls_ssl_pend_fatal_alert( ssl,
8313 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008314 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008315 }
8316
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008317 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008318#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01008319 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008320 hash_len = 36;
8321 else
8322#endif
8323 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008325 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8326 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008329 mbedtls_ssl_pend_fatal_alert( ssl,
8330 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008332 }
8333
Teppo Järvelin707ceb82019-10-04 07:49:39 +03008334 if( mbedtls_platform_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008335 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008338 mbedtls_ssl_pend_fatal_alert( ssl,
8339 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008340 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008341 }
8342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008343#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008344 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008345 mbedtls_platform_memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008346#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008347
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008348#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008349 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008351#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008352 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008353 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008354#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008356 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008357 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008358#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008359 }
8360 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008361#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008362 {
8363 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8364 {
8365 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8366 }
8367 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8368 {
8369 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8370 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008371 else
8372 {
8373 ssl->state = MBEDTLS_SSL_INVALID;
8374 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008375 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008377#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008378 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008379 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008380#endif
8381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008383
8384 return( 0 );
8385}
8386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008387static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008388{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008389 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008391#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8392 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8393 mbedtls_md5_init( &handshake->fin_md5 );
8394 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008395 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8396 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008397#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008398#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8399#if defined(MBEDTLS_SHA256_C)
8400 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008401 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008402#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008403#if defined(MBEDTLS_SHA512_C)
8404 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008405 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008406#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008407#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008408
Hanno Becker7e5437a2017-04-28 17:15:26 +01008409#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8410 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8411 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8412#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008414#if defined(MBEDTLS_DHM_C)
8415 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008416#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008417#if defined(MBEDTLS_ECDH_C)
8418 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008419#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008420#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008421 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008422#if defined(MBEDTLS_SSL_CLI_C)
8423 handshake->ecjpake_cache = NULL;
8424 handshake->ecjpake_cache_len = 0;
8425#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008426#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008427
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008428#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008429 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008430#endif
8431
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008432#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8433 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8434#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008435
8436#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8437 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8438 mbedtls_pk_init( &handshake->peer_pubkey );
8439#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008440}
8441
Hanno Becker611a83b2018-01-03 14:27:32 +00008442void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008443{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008444 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008446 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8447 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008448
Hanno Becker92231322018-01-03 15:32:51 +00008449#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008450 mbedtls_md_init( &transform->md_ctx_enc );
8451 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008452#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008453}
8454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008455void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008456{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008457 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008458}
8459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008460static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008461{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008462 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008463 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008464 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008465 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008466 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008467 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008468 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008469
8470 /*
8471 * Either the pointers are now NULL or cleared properly and can be freed.
8472 * Now allocate missing structures.
8473 */
8474 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008475 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008476 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008477 }
Paul Bakker48916f92012-09-16 19:57:18 +00008478
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008479 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008480 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008481 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008482 }
Paul Bakker48916f92012-09-16 19:57:18 +00008483
Paul Bakker82788fb2014-10-20 13:59:19 +02008484 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008485 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008486 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008487 }
Paul Bakker48916f92012-09-16 19:57:18 +00008488
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008489 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008490 if( ssl->handshake == NULL ||
8491 ssl->transform_negotiate == NULL ||
8492 ssl->session_negotiate == NULL )
8493 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008496 mbedtls_free( ssl->handshake );
8497 mbedtls_free( ssl->transform_negotiate );
8498 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008499
8500 ssl->handshake = NULL;
8501 ssl->transform_negotiate = NULL;
8502 ssl->session_negotiate = NULL;
8503
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008504 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008505 }
8506
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008507 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008508 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008509 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008510 ssl_handshake_params_init( ssl->handshake );
8511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008512#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008513 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008514 {
8515 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008516
Hanno Becker2d9623f2019-06-13 12:07:05 +01008517 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008518 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8519 else
8520 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8521 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008522#endif
8523
Paul Bakker48916f92012-09-16 19:57:18 +00008524 return( 0 );
8525}
8526
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008527#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008528/* Dummy cookie callbacks for defaults */
8529static int ssl_cookie_write_dummy( void *ctx,
8530 unsigned char **p, unsigned char *end,
8531 const unsigned char *cli_id, size_t cli_id_len )
8532{
8533 ((void) ctx);
8534 ((void) p);
8535 ((void) end);
8536 ((void) cli_id);
8537 ((void) cli_id_len);
8538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008539 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008540}
8541
8542static int ssl_cookie_check_dummy( void *ctx,
8543 const unsigned char *cookie, size_t cookie_len,
8544 const unsigned char *cli_id, size_t cli_id_len )
8545{
8546 ((void) ctx);
8547 ((void) cookie);
8548 ((void) cookie_len);
8549 ((void) cli_id);
8550 ((void) cli_id_len);
8551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008552 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008553}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008554#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008555
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008556/* Once ssl->out_hdr as the address of the beginning of the
8557 * next outgoing record is set, deduce the other pointers.
8558 *
8559 * Note: For TLS, we save the implicit record sequence number
8560 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8561 * and the caller has to make sure there's space for this.
8562 */
8563
8564static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8565 mbedtls_ssl_transform *transform )
8566{
8567#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008568 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008569 {
8570 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008571#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008572 ssl->out_cid = ssl->out_ctr + 8;
8573 ssl->out_len = ssl->out_cid;
8574 if( transform != NULL )
8575 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008576#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008577 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008578#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008579 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008580 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008581 MBEDTLS_SSL_TRANSPORT_ELSE
8582#endif /* MBEDTLS_SSL_PROTO_DTLS */
8583#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008584 {
8585 ssl->out_ctr = ssl->out_hdr - 8;
8586 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008587#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008588 ssl->out_cid = ssl->out_len;
8589#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008590 ssl->out_iv = ssl->out_hdr + 5;
8591 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008592#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008593
8594 /* Adjust out_msg to make space for explicit IV, if used. */
8595 if( transform != NULL &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008596 mbedtls_ssl_ver_geq(
8597 mbedtls_ssl_get_minor_ver( ssl ),
8598 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008599 {
8600 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8601 }
8602 else
8603 ssl->out_msg = ssl->out_iv;
8604}
8605
8606/* Once ssl->in_hdr as the address of the beginning of the
8607 * next incoming record is set, deduce the other pointers.
8608 *
8609 * Note: For TLS, we save the implicit record sequence number
8610 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8611 * and the caller has to make sure there's space for this.
8612 */
8613
Hanno Beckerf5970a02019-05-08 09:38:41 +01008614static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008615{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008616 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008617 * of unprotected TLS/DTLS records, with ssl->in_msg
8618 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008619 *
8620 * When decrypting a protected record, ssl->in_msg
8621 * will be shifted to point to the beginning of the
8622 * record plaintext.
8623 */
8624
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008625#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008626 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008627 {
Hanno Becker70e79282019-05-03 14:34:53 +01008628 /* This sets the header pointers to match records
8629 * without CID. When we receive a record containing
8630 * a CID, the fields are shifted accordingly in
8631 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008632 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008633#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008634 ssl->in_cid = ssl->in_ctr + 8;
8635 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008636#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008637 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008638#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008639 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008640 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008641 MBEDTLS_SSL_TRANSPORT_ELSE
8642#endif /* MBEDTLS_SSL_PROTO_DTLS */
8643#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008644 {
8645 ssl->in_ctr = ssl->in_hdr - 8;
8646 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008647#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008648 ssl->in_cid = ssl->in_len;
8649#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008650 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008651 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008652#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008653}
8654
Paul Bakker5121ce52009-01-03 21:22:43 +00008655/*
8656 * Initialize an SSL context
8657 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008658void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8659{
8660 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8661}
8662
8663/*
8664 * Setup an SSL context
8665 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008666
8667static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8668{
8669 /* Set the incoming and outgoing record pointers. */
8670#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008671 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008672 {
8673 ssl->out_hdr = ssl->out_buf;
8674 ssl->in_hdr = ssl->in_buf;
8675 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008676 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008677#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008678#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008679 {
8680 ssl->out_hdr = ssl->out_buf + 8;
8681 ssl->in_hdr = ssl->in_buf + 8;
8682 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008683#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008684
8685 /* Derive other internal pointers. */
8686 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008687 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008688}
8689
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008690int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008691 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008692{
Paul Bakker48916f92012-09-16 19:57:18 +00008693 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008694
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008695 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008696
Hanno Beckeref982d52019-07-23 15:56:18 +01008697#if defined(MBEDTLS_USE_TINYCRYPT)
8698 uECC_set_rng( &uecc_rng_wrapper );
8699#endif
8700
Paul Bakker62f2dee2012-09-28 07:31:51 +00008701 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008702 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008703 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008704
8705 /* Set to NULL in case of an error condition */
8706 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008707
Angus Grattond8213d02016-05-25 20:56:48 +10008708 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8709 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008710 {
Angus Grattond8213d02016-05-25 20:56:48 +10008711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008712 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008713 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008714 }
8715
8716 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8717 if( ssl->out_buf == NULL )
8718 {
8719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008720 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008721 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008722 }
8723
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008724 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008725
Paul Bakker48916f92012-09-16 19:57:18 +00008726 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008727 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008728
Hanno Beckerc8f52992019-07-25 11:15:08 +01008729 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008730
Paul Bakker5121ce52009-01-03 21:22:43 +00008731 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008732
8733error:
8734 mbedtls_free( ssl->in_buf );
8735 mbedtls_free( ssl->out_buf );
8736
8737 ssl->conf = NULL;
8738
8739 ssl->in_buf = NULL;
8740 ssl->out_buf = NULL;
8741
8742 ssl->in_hdr = NULL;
8743 ssl->in_ctr = NULL;
8744 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008745 ssl->in_msg = NULL;
8746
8747 ssl->out_hdr = NULL;
8748 ssl->out_ctr = NULL;
8749 ssl->out_len = NULL;
8750 ssl->out_iv = NULL;
8751 ssl->out_msg = NULL;
8752
k-stachowiak9f7798e2018-07-31 16:52:32 +02008753 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008754}
8755
8756/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008757 * Reset an initialized and used SSL context for re-use while retaining
8758 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008759 *
8760 * If partial is non-zero, keep data in the input buffer and client ID.
8761 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008762 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008763static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008764{
Paul Bakker48916f92012-09-16 19:57:18 +00008765 int ret;
8766
Hanno Becker7e772132018-08-10 12:38:21 +01008767#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8768 !defined(MBEDTLS_SSL_SRV_C)
8769 ((void) partial);
8770#endif
8771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008772 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008773
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008774 /* Cancel any possibly running timer */
8775 ssl_set_timer( ssl, 0 );
8776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008777#if defined(MBEDTLS_SSL_RENEGOTIATION)
8778 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008779 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008780
8781 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008782 mbedtls_platform_memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8783 mbedtls_platform_memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008784#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008785 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008786
Paul Bakker7eb013f2011-10-06 12:37:39 +00008787 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008788 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008789
8790 ssl->in_msgtype = 0;
8791 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008792#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008793 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008794 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008795#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008796#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008797 ssl_dtls_replay_reset( ssl );
8798#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008799
8800 ssl->in_hslen = 0;
8801 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008802
8803 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008804
8805 ssl->out_msgtype = 0;
8806 ssl->out_msglen = 0;
8807 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008808#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8809 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008810 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008811#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008812
Hanno Becker19859472018-08-06 09:40:20 +01008813 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8814
Paul Bakker48916f92012-09-16 19:57:18 +00008815 ssl->transform_in = NULL;
8816 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008817
Hanno Becker78640902018-08-13 16:35:15 +01008818 ssl->session_in = NULL;
8819 ssl->session_out = NULL;
8820
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008821 mbedtls_platform_memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008822
8823#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008824 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008825#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8826 {
8827 ssl->in_left = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008828 mbedtls_platform_memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008829 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008831#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8832 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8835 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008837 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8838 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008839 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008840 }
8841#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008842
Paul Bakker48916f92012-09-16 19:57:18 +00008843 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845 mbedtls_ssl_transform_free( ssl->transform );
8846 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008847 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008848 }
Paul Bakker48916f92012-09-16 19:57:18 +00008849
Paul Bakkerc0463502013-02-14 11:19:38 +01008850 if( ssl->session )
8851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008852 mbedtls_ssl_session_free( ssl->session );
8853 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008854 ssl->session = NULL;
8855 }
8856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008857#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008858 ssl->alpn_chosen = NULL;
8859#endif
8860
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008861#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008862#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008863 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008864#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008865 {
8866 mbedtls_free( ssl->cli_id );
8867 ssl->cli_id = NULL;
8868 ssl->cli_id_len = 0;
8869 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008870#endif
8871
Paul Bakker48916f92012-09-16 19:57:18 +00008872 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8873 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008874
8875 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008876}
8877
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008878/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008879 * Reset an initialized and used SSL context for re-use while retaining
8880 * all application-set variables, function pointers and data.
8881 */
8882int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8883{
8884 return( ssl_session_reset_int( ssl, 0 ) );
8885}
8886
8887/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008888 * SSL set accessors
8889 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008890#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008891void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008892{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008893 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008894}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008895#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008896
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008897void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008898{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008899 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008900}
8901
Hanno Becker7f376f42019-06-12 16:20:48 +01008902#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8903 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008904void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008905{
Hanno Becker7f376f42019-06-12 16:20:48 +01008906 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008907}
Hanno Becker7f376f42019-06-12 16:20:48 +01008908#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008909
Hanno Beckerde671542019-06-12 16:30:46 +01008910#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8911 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8912void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8913 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008914{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008915 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008916}
Hanno Beckerde671542019-06-12 16:30:46 +01008917#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008919#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008920
Hanno Becker1841b0a2018-08-24 11:13:57 +01008921void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8922 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008923{
8924 ssl->disable_datagram_packing = !allow_packing;
8925}
8926
Hanno Becker1f835fa2019-06-13 10:14:59 +01008927#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8928 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008929void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8930 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008931{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008932 conf->hs_timeout_min = min;
8933 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008934}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008935#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8936 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8937void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8938 uint32_t min, uint32_t max )
8939{
8940 ((void) conf);
8941 ((void) min);
8942 ((void) max);
8943}
8944#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8945 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8946
8947#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008948
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008949void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008950{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008951#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8952 conf->authmode = authmode;
8953#else
8954 ((void) conf);
8955 ((void) authmode);
8956#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008957}
8958
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008959#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8960 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008961void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008962 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008963 void *p_vrfy )
8964{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008965 conf->f_vrfy = f_vrfy;
8966 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008967}
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008968#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008969
Hanno Beckerece325c2019-06-13 15:39:27 +01008970#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008971void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008972 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008973 void *p_rng )
8974{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008975 conf->f_rng = f_rng;
8976 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008977}
Hanno Beckerece325c2019-06-13 15:39:27 +01008978#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008979
Hanno Becker14a4a442019-07-02 17:00:34 +01008980#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008981void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008982 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008983 void *p_dbg )
8984{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008985 conf->f_dbg = f_dbg;
8986 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008987}
Hanno Becker14a4a442019-07-02 17:00:34 +01008988#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008989
Hanno Beckera58a8962019-06-13 16:11:15 +01008990#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8991 !defined(MBEDTLS_SSL_CONF_SEND) && \
8992 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008993void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008994 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008995 mbedtls_ssl_send_t *f_send,
8996 mbedtls_ssl_recv_t *f_recv,
8997 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008998{
Hanno Beckera58a8962019-06-13 16:11:15 +01008999 ssl->p_bio = p_bio;
9000 ssl->f_send = f_send;
9001 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009002 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009003}
Hanno Beckera58a8962019-06-13 16:11:15 +01009004#else
9005void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
9006 void *p_bio )
9007{
9008 ssl->p_bio = p_bio;
9009}
9010#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009011
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02009012#if defined(MBEDTLS_SSL_PROTO_DTLS)
9013void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
9014{
9015 ssl->mtu = mtu;
9016}
9017#endif
9018
Hanno Becker1f835fa2019-06-13 10:14:59 +01009019#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009020void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009021{
9022 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009023}
Hanno Becker1f835fa2019-06-13 10:14:59 +01009024#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009025
Hanno Becker0ae6b242019-06-13 16:45:36 +01009026#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
9027 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009028void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
9029 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00009030 mbedtls_ssl_set_timer_t *f_set_timer,
9031 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009032{
9033 ssl->p_timer = p_timer;
9034 ssl->f_set_timer = f_set_timer;
9035 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009036 /* Make sure we start with no timer running */
9037 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009038}
Hanno Becker0ae6b242019-06-13 16:45:36 +01009039#else
9040void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
9041 void *p_timer )
9042{
9043 ssl->p_timer = p_timer;
9044 /* Make sure we start with no timer running */
9045 ssl_set_timer( ssl, 0 );
9046}
9047#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009048
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009049#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009050void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009051 void *p_cache,
9052 int (*f_get_cache)(void *, mbedtls_ssl_session *),
9053 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00009054{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009055 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009056 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009057 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00009058}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009059#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009060
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009061#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009062int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00009063{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009064 int ret;
9065
9066 if( ssl == NULL ||
9067 session == NULL ||
9068 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009069 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009071 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009072 }
9073
Hanno Becker58fccf22019-02-06 14:30:46 +00009074 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
9075 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009076 return( ret );
9077
Jarno Lamsa8d09e572019-12-19 15:20:19 +02009078 ssl->handshake->resume = MBEDTLS_SSL_FI_FLAG_SET;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009079
9080 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009081}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009082#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009083
Hanno Becker73f4cb12019-06-27 13:51:07 +01009084#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009085void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009086 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00009087{
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009088 conf->ciphersuite_list[0] = ciphersuites;
9089 conf->ciphersuite_list[1] = ciphersuites;
9090 conf->ciphersuite_list[2] = ciphersuites;
9091 conf->ciphersuite_list[3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009092}
9093
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009094void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02009095 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009096 int major, int minor )
9097{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009098 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009099 return;
9100
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009101 if( mbedtls_ssl_ver_lt( minor, MBEDTLS_SSL_MINOR_VERSION_0 ) ||
9102 mbedtls_ssl_ver_gt( minor, MBEDTLS_SSL_MINOR_VERSION_3 ) )
9103 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009104 return;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009105 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009106
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009107 conf->ciphersuite_list[mbedtls_ssl_minor_ver_index( minor )] =
9108 ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00009109}
Hanno Becker73f4cb12019-06-27 13:51:07 +01009110#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009112#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009113void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01009114 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009115{
9116 conf->cert_profile = profile;
9117}
9118
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009119/* Append a new keycert entry to a (possibly empty) list */
9120static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
9121 mbedtls_x509_crt *cert,
9122 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009123{
niisato8ee24222018-06-25 19:05:48 +09009124 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009125
niisato8ee24222018-06-25 19:05:48 +09009126 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
9127 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009128 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009129
niisato8ee24222018-06-25 19:05:48 +09009130 new_cert->cert = cert;
9131 new_cert->key = key;
9132 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009133
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009134 /* Update head is the list was null, else add to the end */
9135 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01009136 {
niisato8ee24222018-06-25 19:05:48 +09009137 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01009138 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009139 else
9140 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009141 mbedtls_ssl_key_cert *cur = *head;
9142 while( cur->next != NULL )
9143 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09009144 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009145 }
9146
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009147 return( 0 );
9148}
9149
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009150int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009151 mbedtls_x509_crt *own_cert,
9152 mbedtls_pk_context *pk_key )
9153{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02009154 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009155}
9156
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009157void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009158 mbedtls_x509_crt *ca_chain,
9159 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009160{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009161 conf->ca_chain = ca_chain;
9162 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00009163}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009164#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00009165
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009166#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9167int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
9168 mbedtls_x509_crt *own_cert,
9169 mbedtls_pk_context *pk_key )
9170{
9171 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
9172 own_cert, pk_key ) );
9173}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02009174
9175void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
9176 mbedtls_x509_crt *ca_chain,
9177 mbedtls_x509_crl *ca_crl )
9178{
9179 ssl->handshake->sni_ca_chain = ca_chain;
9180 ssl->handshake->sni_ca_crl = ca_crl;
9181}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02009182
9183void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
9184 int authmode )
9185{
9186 ssl->handshake->sni_authmode = authmode;
9187}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009188#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9189
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009190#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009191/*
9192 * Set EC J-PAKE password for current handshake
9193 */
9194int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
9195 const unsigned char *pw,
9196 size_t pw_len )
9197{
9198 mbedtls_ecjpake_role role;
9199
Janos Follath8eb64132016-06-03 15:40:57 +01009200 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009201 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9202
Hanno Becker2d9623f2019-06-13 12:07:05 +01009203 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009204 role = MBEDTLS_ECJPAKE_SERVER;
9205 else
9206 role = MBEDTLS_ECJPAKE_CLIENT;
9207
9208 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
9209 role,
9210 MBEDTLS_MD_SHA256,
9211 MBEDTLS_ECP_DP_SECP256R1,
9212 pw, pw_len ) );
9213}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009214#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009216#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009217int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009218 const unsigned char *psk, size_t psk_len,
9219 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009220{
Paul Bakker6db455e2013-09-18 17:29:31 +02009221 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009222 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02009223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009224 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9225 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01009226
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009227 /* Identity len will be encoded on two bytes */
9228 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009229 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009230 {
9231 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9232 }
9233
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009234 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02009235 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009236 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009237
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009238 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009239 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009240 conf->psk_len = 0;
9241 }
9242 if( conf->psk_identity != NULL )
9243 {
9244 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009245 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009246 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02009247 }
9248
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009249 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
9250 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05009251 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009252 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009253 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009254 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009255 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009256 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05009257 }
Paul Bakker6db455e2013-09-18 17:29:31 +02009258
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009259 conf->psk_len = psk_len;
9260 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02009261
Teppo Järvelin91d79382019-10-02 09:09:31 +03009262 mbedtls_platform_memcpy( conf->psk, psk, conf->psk_len );
9263 mbedtls_platform_memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02009264
9265 return( 0 );
9266}
9267
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009268int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9269 const unsigned char *psk, size_t psk_len )
9270{
9271 if( psk == NULL || ssl->handshake == NULL )
9272 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9273
9274 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9275 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9276
9277 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009278 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009279 mbedtls_platform_zeroize( ssl->handshake->psk,
9280 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01009281 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009282 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009283 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009284
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009285 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009286 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009287
9288 ssl->handshake->psk_len = psk_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03009289 mbedtls_platform_memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009290
9291 return( 0 );
9292}
9293
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009294void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009296 size_t),
9297 void *p_psk )
9298{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009299 conf->f_psk = f_psk;
9300 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009301}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009302#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009303
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009304#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009305
9306#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009307int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009308{
9309 int ret;
9310
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009311 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9312 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9313 {
9314 mbedtls_mpi_free( &conf->dhm_P );
9315 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009316 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009317 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009318
9319 return( 0 );
9320}
Hanno Becker470a8c42017-10-04 15:28:46 +01009321#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009322
Hanno Beckera90658f2017-10-04 15:29:08 +01009323int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9324 const unsigned char *dhm_P, size_t P_len,
9325 const unsigned char *dhm_G, size_t G_len )
9326{
9327 int ret;
9328
9329 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9330 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9331 {
9332 mbedtls_mpi_free( &conf->dhm_P );
9333 mbedtls_mpi_free( &conf->dhm_G );
9334 return( ret );
9335 }
9336
9337 return( 0 );
9338}
Paul Bakker5121ce52009-01-03 21:22:43 +00009339
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009340int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009341{
9342 int ret;
9343
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009344 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9345 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9346 {
9347 mbedtls_mpi_free( &conf->dhm_P );
9348 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009349 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009350 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009351
9352 return( 0 );
9353}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009354#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009355
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009356#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9357/*
9358 * Set the minimum length for Diffie-Hellman parameters
9359 */
9360void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9361 unsigned int bitlen )
9362{
9363 conf->dhm_min_bitlen = bitlen;
9364}
9365#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9366
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009367#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009368/*
9369 * Set allowed/preferred hashes for handshake signatures
9370 */
9371void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9372 const int *hashes )
9373{
Hanno Becker56595f42019-06-19 16:31:38 +01009374#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009375 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01009376#else
9377 ((void) conf);
9378 ((void) hashes);
9379#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009380}
Hanno Becker947194e2017-04-07 13:25:49 +01009381#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009382
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009383#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01009384#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009385/*
9386 * Set the allowed elliptic curves
9387 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009388void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009389 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009390{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009391 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009392}
Hanno Beckerc1096e72019-06-19 12:30:41 +01009393#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01009394#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009395
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009396#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009397int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009398{
Hanno Becker947194e2017-04-07 13:25:49 +01009399 /* Initialize to suppress unnecessary compiler warning */
9400 size_t hostname_len = 0;
9401
9402 /* Check if new hostname is valid before
9403 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009404 if( hostname != NULL )
9405 {
9406 hostname_len = strlen( hostname );
9407
9408 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9409 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9410 }
9411
9412 /* Now it's clear that we will overwrite the old hostname,
9413 * so we can free it safely */
9414
9415 if( ssl->hostname != NULL )
9416 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009417 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009418 mbedtls_free( ssl->hostname );
9419 }
9420
9421 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009422
Paul Bakker5121ce52009-01-03 21:22:43 +00009423 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009424 {
9425 ssl->hostname = NULL;
9426 }
9427 else
9428 {
9429 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009430 if( ssl->hostname == NULL )
9431 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009432
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03009433 /* Not using more secure mbedtls_platform_memcpy as hostname is public in initial handshake */
9434 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009435
Hanno Becker947194e2017-04-07 13:25:49 +01009436 ssl->hostname[hostname_len] = '\0';
9437 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009438
9439 return( 0 );
9440}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009441#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009442
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009443#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009444void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009445 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009446 const unsigned char *, size_t),
9447 void *p_sni )
9448{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009449 conf->f_sni = f_sni;
9450 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009451}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009452#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009454#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009455int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009456{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009457 size_t cur_len, tot_len;
9458 const char **p;
9459
9460 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009461 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9462 * MUST NOT be truncated."
9463 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009464 */
9465 tot_len = 0;
9466 for( p = protos; *p != NULL; p++ )
9467 {
9468 cur_len = strlen( *p );
9469 tot_len += cur_len;
9470
9471 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009472 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009473 }
9474
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009475 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009476
9477 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009478}
9479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009480const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009481{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009482 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009484#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009485
Hanno Becker33b9b252019-07-05 11:23:25 +01009486#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9487 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9488void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9489 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009490{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009491 conf->max_major_ver = major;
9492 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009493}
Hanno Becker33b9b252019-07-05 11:23:25 +01009494#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9495 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009496
Hanno Becker33b9b252019-07-05 11:23:25 +01009497#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9498 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9499void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9500 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009501{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009502 conf->min_major_ver = major;
9503 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009504}
Hanno Becker33b9b252019-07-05 11:23:25 +01009505#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9506 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009508#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009509void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009510{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009511 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009512}
9513#endif
9514
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009515#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009516void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9517 char cert_req_ca_list )
9518{
9519 conf->cert_req_ca_list = cert_req_ca_list;
9520}
9521#endif
9522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009523#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009524void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009525{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009526 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009527}
9528#endif
9529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009530#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009531#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009532void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009533{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009534 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009535}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009536#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9537#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009538void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009539 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009540{
9541 conf->enforce_extended_master_secret = ems_enf;
9542}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009543#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009544#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009545
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009546#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009547void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009548{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009549 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009550}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009551#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009553#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009554int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009555{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009557 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009559 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009560 }
9561
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009562 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009563
9564 return( 0 );
9565}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009568#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009569void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009570{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009571 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009572}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009573#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009575#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009576void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009577{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009578 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009579}
9580#endif
9581
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009582#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009583void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009584{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009585 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009586}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009587#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009589#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009590void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009591{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009592 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009593}
9594
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009595void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009596{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009597 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009598}
9599
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009600void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009601 const unsigned char period[8] )
9602{
Teppo Järvelin91d79382019-10-02 09:09:31 +03009603 mbedtls_platform_memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009604}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009605#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009607#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009608#if defined(MBEDTLS_SSL_CLI_C)
9609void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009610{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009611 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009612}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009613#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009614
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009615#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009616void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9617 mbedtls_ssl_ticket_write_t *f_ticket_write,
9618 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9619 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009620{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009621 conf->f_ticket_write = f_ticket_write;
9622 conf->f_ticket_parse = f_ticket_parse;
9623 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009624}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009625#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009626#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009627
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009628#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9629void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9630 mbedtls_ssl_export_keys_t *f_export_keys,
9631 void *p_export_keys )
9632{
9633 conf->f_export_keys = f_export_keys;
9634 conf->p_export_keys = p_export_keys;
9635}
9636#endif
9637
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009638#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009639void mbedtls_ssl_conf_async_private_cb(
9640 mbedtls_ssl_config *conf,
9641 mbedtls_ssl_async_sign_t *f_async_sign,
9642 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9643 mbedtls_ssl_async_resume_t *f_async_resume,
9644 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009645 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009646{
9647 conf->f_async_sign_start = f_async_sign;
9648 conf->f_async_decrypt_start = f_async_decrypt;
9649 conf->f_async_resume = f_async_resume;
9650 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009651 conf->p_async_config_data = async_config_data;
9652}
9653
Gilles Peskine8f97af72018-04-26 11:46:10 +02009654void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9655{
9656 return( conf->p_async_config_data );
9657}
9658
Gilles Peskine1febfef2018-04-30 11:54:39 +02009659void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009660{
9661 if( ssl->handshake == NULL )
9662 return( NULL );
9663 else
9664 return( ssl->handshake->user_async_ctx );
9665}
9666
Gilles Peskine1febfef2018-04-30 11:54:39 +02009667void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009668 void *ctx )
9669{
9670 if( ssl->handshake != NULL )
9671 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009672}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009673#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009674
Paul Bakker5121ce52009-01-03 21:22:43 +00009675/*
9676 * SSL get accessors
9677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009679{
9680 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9681}
9682
Hanno Becker8b170a02017-10-10 11:51:19 +01009683int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9684{
9685 /*
9686 * Case A: We're currently holding back
9687 * a message for further processing.
9688 */
9689
9690 if( ssl->keep_current_message == 1 )
9691 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009692 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009693 return( 1 );
9694 }
9695
9696 /*
9697 * Case B: Further records are pending in the current datagram.
9698 */
9699
9700#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009701 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009702 ssl->in_left > ssl->next_record_offset )
9703 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009704 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009705 return( 1 );
9706 }
9707#endif /* MBEDTLS_SSL_PROTO_DTLS */
9708
9709 /*
9710 * Case C: A handshake message is being processed.
9711 */
9712
Hanno Becker8b170a02017-10-10 11:51:19 +01009713 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9714 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009715 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009716 return( 1 );
9717 }
9718
9719 /*
9720 * Case D: An application data message is being processed
9721 */
9722 if( ssl->in_offt != NULL )
9723 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009724 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009725 return( 1 );
9726 }
9727
9728 /*
9729 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009730 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009731 * we implement support for multiple alerts in single records.
9732 */
9733
9734 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9735 return( 0 );
9736}
9737
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009738uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009739{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009740 if( ssl->session != NULL )
9741 return( ssl->session->verify_result );
9742
9743 if( ssl->session_negotiate != NULL )
9744 return( ssl->session_negotiate->verify_result );
9745
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009746 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009747}
9748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009750{
Hanno Beckere02758c2019-06-26 15:31:31 +01009751 int suite;
9752
Paul Bakker926c8e42013-03-06 10:23:34 +01009753 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009754 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009755
Hanno Beckere02758c2019-06-26 15:31:31 +01009756 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9757 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009758}
9759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009760const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009761{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009762#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009763 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009764 {
Hanno Becker2881d802019-05-22 14:44:53 +01009765 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009768 return( "DTLSv1.0" );
9769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009771 return( "DTLSv1.2" );
9772
9773 default:
9774 return( "unknown (DTLS)" );
9775 }
9776 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009777 MBEDTLS_SSL_TRANSPORT_ELSE
9778#endif /* MBEDTLS_SSL_PROTO_DTLS */
9779#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009780 {
Hanno Becker2881d802019-05-22 14:44:53 +01009781 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009782 {
9783 case MBEDTLS_SSL_MINOR_VERSION_0:
9784 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009785
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009786 case MBEDTLS_SSL_MINOR_VERSION_1:
9787 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009788
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009789 case MBEDTLS_SSL_MINOR_VERSION_2:
9790 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009791
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009792 case MBEDTLS_SSL_MINOR_VERSION_3:
9793 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009794
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009795 default:
9796 return( "unknown" );
9797 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009798 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009799#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009800}
9801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009803{
Hanno Becker3136ede2018-08-17 15:28:19 +01009804 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009805 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009806
Hanno Becker43395762019-05-03 14:46:38 +01009807 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9808
Hanno Becker78640902018-08-13 16:35:15 +01009809 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009810 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009812#if defined(MBEDTLS_ZLIB_SUPPORT)
9813 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9814 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009815#endif
9816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009817 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009818 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009819#if defined(MBEDTLS_GCM_C) || \
9820 defined(MBEDTLS_CCM_C) || \
9821 defined(MBEDTLS_CHACHAPOLY_C)
9822#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009823 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009824#endif
9825#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009826 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009827#endif
9828#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009829 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009830#endif
9831 transform_expansion =
9832 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009833 break;
9834
Hanno Beckera9d5c452019-07-25 16:47:12 +01009835#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9836 MBEDTLS_CHACHAPOLY_C */
9837
9838#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9839 case MBEDTLS_MODE_STREAM:
9840 transform_expansion = transform->maclen;
9841 break;
9842#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9843
9844#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009845 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009846 {
9847 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009848
9849 block_size = mbedtls_cipher_get_block_size(
9850 &transform->cipher_ctx_enc );
9851
Hanno Becker3136ede2018-08-17 15:28:19 +01009852 /* Expansion due to the addition of the MAC. */
9853 transform_expansion += transform->maclen;
9854
9855 /* Expansion due to the addition of CBC padding;
9856 * Theoretically up to 256 bytes, but we never use
9857 * more than the block size of the underlying cipher. */
9858 transform_expansion += block_size;
9859
9860 /* For TLS 1.1 or higher, an explicit IV is added
9861 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009862#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009863 if( mbedtls_ssl_ver_geq(
9864 mbedtls_ssl_get_minor_ver( ssl ),
9865 MBEDTLS_SSL_MINOR_VERSION_2 ) )
9866 {
Hanno Becker3136ede2018-08-17 15:28:19 +01009867 transform_expansion += block_size;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009868 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01009869#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009870
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009871 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009872 }
9873#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009874
9875 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009877 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009878 }
9879
Hanno Beckera5a2b082019-05-15 14:03:01 +01009880#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009881 if( transform->out_cid_len != 0 )
9882 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009883#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009884
Hanno Becker43395762019-05-03 14:46:38 +01009885 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009886}
9887
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009888#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9889size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9890{
9891 size_t max_len;
9892
9893 /*
9894 * Assume mfl_code is correct since it was checked when set
9895 */
Angus Grattond8213d02016-05-25 20:56:48 +10009896 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009897
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009898 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009899 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009900 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009901 {
Angus Grattond8213d02016-05-25 20:56:48 +10009902 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009903 }
9904
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009905 /* During a handshake, use the value being negotiated */
9906 if( ssl->session_negotiate != NULL &&
9907 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9908 {
9909 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9910 }
9911
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009912 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009913}
9914#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9915
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009916#if defined(MBEDTLS_SSL_PROTO_DTLS)
9917static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9918{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009919 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009920 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009921 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9922 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009923 return( 0 );
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009924
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009925 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9926 return( ssl->mtu );
9927
9928 if( ssl->mtu == 0 )
9929 return( ssl->handshake->mtu );
9930
9931 return( ssl->mtu < ssl->handshake->mtu ?
9932 ssl->mtu : ssl->handshake->mtu );
9933}
9934#endif /* MBEDTLS_SSL_PROTO_DTLS */
9935
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009936int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9937{
9938 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9939
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009940#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9941 !defined(MBEDTLS_SSL_PROTO_DTLS)
9942 (void) ssl;
9943#endif
9944
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009945#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9946 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9947
9948 if( max_len > mfl )
9949 max_len = mfl;
9950#endif
9951
9952#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009953 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009954 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009955 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009956 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9957 const size_t overhead = (size_t) ret;
9958
9959 if( ret < 0 )
9960 return( ret );
9961
9962 if( mtu <= overhead )
9963 {
9964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9965 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9966 }
9967
9968 if( max_len > mtu - overhead )
9969 max_len = mtu - overhead;
9970 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009971#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009972
Hanno Becker0defedb2018-08-10 12:35:02 +01009973#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9974 !defined(MBEDTLS_SSL_PROTO_DTLS)
9975 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009976#endif
9977
9978 return( (int) max_len );
9979}
9980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009981#if defined(MBEDTLS_X509_CRT_PARSE_C)
9982const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009983{
9984 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009985 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009986
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009987#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009988 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009989#else
9990 return( NULL );
9991#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009992}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009993#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009995#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009996int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9997 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009998{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009999 if( ssl == NULL ||
10000 dst == NULL ||
10001 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +010010002 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010004 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010005 }
10006
Hanno Becker58fccf22019-02-06 14:30:46 +000010007 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010008}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010009#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010010
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +020010011const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
10012{
10013 if( ssl == NULL )
10014 return( NULL );
10015
10016 return( ssl->session );
10017}
10018
Paul Bakker5121ce52009-01-03 21:22:43 +000010019/*
Hanno Beckerb5352f02019-05-16 12:39:07 +010010020 * Define ticket header determining Mbed TLS version
10021 * and structure of the ticket.
10022 */
10023
Hanno Becker41527622019-05-16 12:50:45 +010010024/*
Hanno Becker26829e92019-05-28 14:30:45 +010010025 * Define bitflag determining compile-time settings influencing
10026 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +010010027 */
10028
Hanno Becker26829e92019-05-28 14:30:45 +010010029#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010030#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +010010031#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010032#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +010010033#endif /* MBEDTLS_HAVE_TIME */
10034
10035#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010036#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +010010037#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010038#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +010010039#endif /* MBEDTLS_X509_CRT_PARSE_C */
10040
10041#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010042#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +010010043#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010044#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +010010045#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
10046
10047#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010048#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +010010049#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010050#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +010010051#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
10052
10053#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010054#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +010010055#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010056#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +010010057#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
10058
10059#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010060#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +010010061#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010062#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +010010063#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
10064
Hanno Becker41527622019-05-16 12:50:45 +010010065#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10066#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
10067#else
10068#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
10069#endif /* MBEDTLS_SSL_SESSION_TICKETS */
10070
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010071#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10072#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
10073#else
10074#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
10075#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10076
Hanno Becker88440552019-07-03 14:16:13 +010010077#if defined(MBEDTLS_ZLIB_SUPPORT)
10078#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
10079#else
10080#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
10081#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10082
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010083#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
10084#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
10085#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
10086#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
10087#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
10088#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
10089#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010090#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +010010091#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010092
Hanno Becker26829e92019-05-28 14:30:45 +010010093#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010094 ( (uint16_t) ( \
10095 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
10096 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
10097 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
10098 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
10099 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
10100 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010101 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +010010102 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010103 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +010010104
Hanno Becker557fe9f2019-05-16 12:41:07 +010010105static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +010010106 MBEDTLS_VERSION_MAJOR,
10107 MBEDTLS_VERSION_MINOR,
10108 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +010010109 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
10110 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +010010111};
Hanno Beckerb5352f02019-05-16 12:39:07 +010010112
10113/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010114 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010115 * (in the presentation language of TLS, RFC 8446 section 3)
10116 *
Hanno Becker26829e92019-05-28 14:30:45 +010010117 * opaque mbedtls_version[3]; // major, minor, patch
10118 * opaque session_format[2]; // version-specific 16-bit field determining
10119 * // the format of the remaining
10120 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +010010121 *
10122 * Note: When updating the format, remember to keep
10123 * these version+format bytes.
10124 *
Hanno Becker7bf77102019-06-04 09:43:16 +010010125 * // In this version, `session_format` determines
10126 * // the setting of those compile-time
10127 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +010010128 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010129 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +010010130 * uint8 ciphersuite[2]; // defined by the standard
10131 * uint8 compression; // 0 or 1
10132 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010133 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +010010134 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010135 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +010010136 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
10137 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
10138 * case disabled: uint8_t peer_cert_digest_type;
10139 * opaque peer_cert_digest<0..2^8-1>;
10140 * }
Hanno Becker26829e92019-05-28 14:30:45 +010010141 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010142 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +010010143 * uint8 mfl_code; // up to 255 according to standard
10144 * uint8 trunc_hmac; // 0 or 1
10145 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010146 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010147 * The order is the same as in the definition of the structure, except
10148 * verify_result is put before peer_cert so that all mandatory fields come
10149 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010150 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010151static int ssl_session_save( const mbedtls_ssl_session *session,
10152 unsigned char omit_header,
10153 unsigned char *buf,
10154 size_t buf_len,
10155 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010156{
10157 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010158 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010159#if defined(MBEDTLS_HAVE_TIME)
10160 uint64_t start;
10161#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010162#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010163#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010164 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010165#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +000010166#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010167
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010168 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010169 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010170 /*
10171 * Add version identifier
10172 */
10173
10174 used += sizeof( ssl_serialized_session_header );
10175
10176 if( used <= buf_len )
10177 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010178 mbedtls_platform_memcpy( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010179 sizeof( ssl_serialized_session_header ) );
10180 p += sizeof( ssl_serialized_session_header );
10181 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010182 }
10183
10184 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010185 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010186 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010187#if defined(MBEDTLS_HAVE_TIME)
10188 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010189
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010190 if( used <= buf_len )
10191 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010192 start = (uint64_t) session->start;
10193
10194 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
10195 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
10196 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
10197 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
10198 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
10199 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
10200 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
10201 *p++ = (unsigned char)( ( start ) & 0xFF );
10202 }
10203#endif /* MBEDTLS_HAVE_TIME */
10204
10205 /*
10206 * Basic mandatory fields
10207 */
Hanno Becker88440552019-07-03 14:16:13 +010010208 {
10209 size_t const ciphersuite_len = 2;
10210#if defined(MBEDTLS_ZLIB_SUPPORT)
10211 size_t const compression_len = 1;
10212#else
10213 size_t const compression_len = 0;
10214#endif
10215 size_t const id_len_len = 1;
10216 size_t const id_len = 32;
10217 size_t const master_len = 48;
10218 size_t const verif_result_len = 4;
10219
10220 size_t const basic_len =
10221 ciphersuite_len +
10222 compression_len +
10223 id_len_len +
10224 id_len +
10225 master_len +
10226 verif_result_len;
10227
10228 used += basic_len;
10229 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010230
10231 if( used <= buf_len )
10232 {
Hanno Beckere02758c2019-06-26 15:31:31 +010010233 const int ciphersuite =
10234 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010235 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010236
Hanno Becker88440552019-07-03 14:16:13 +010010237#if defined(MBEDTLS_ZLIB_SUPPORT)
10238 *p++ = (unsigned char)(
10239 mbedtls_ssl_session_get_compression( session ) );
10240#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010241
10242 *p++ = (unsigned char)( session->id_len & 0xFF );
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010243 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10244 memcpy( p, session->id, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010245 p += 32;
10246
Teppo Järvelin91d79382019-10-02 09:09:31 +030010247 mbedtls_platform_memcpy( p, session->master, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010248 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010249 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010250 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010251
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010252 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010253 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010254 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010255#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010256#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010257 if( session->peer_cert == NULL )
10258 cert_len = 0;
10259 else
10260 cert_len = session->peer_cert->raw.len;
10261
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010262 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010263
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010264 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010265 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010266 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010267
10268 if( session->peer_cert != NULL )
10269 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010270 mbedtls_platform_memcpy( p, session->peer_cert->raw.p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010271 p += cert_len;
10272 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010273 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010274
Hanno Becker5882dd02019-06-06 16:25:57 +010010275#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010276 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010277 if( session->peer_cert_digest != NULL )
10278 {
10279 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
10280 if( used <= buf_len )
10281 {
10282 *p++ = (unsigned char) session->peer_cert_digest_type;
10283 *p++ = (unsigned char) session->peer_cert_digest_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +030010284 mbedtls_platform_memcpy( p, session->peer_cert_digest,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010285 session->peer_cert_digest_len );
10286 p += session->peer_cert_digest_len;
10287 }
10288 }
10289 else
10290 {
10291 used += 2;
10292 if( used <= buf_len )
10293 {
10294 *p++ = (unsigned char) MBEDTLS_MD_NONE;
10295 *p++ = 0;
10296 }
10297 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010298#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010299#endif /* MBEDTLS_X509_CRT_PARSE_C */
10300
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010301 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010302 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010303 */
10304#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010305 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010306
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010307 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010308 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010309 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010310
10311 if( session->ticket != NULL )
10312 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010313 mbedtls_platform_memcpy( p, session->ticket, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010314 p += session->ticket_len;
10315 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010316
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010317 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010318 }
10319#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10320
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010321 /*
10322 * Misc extension-related info
10323 */
10324#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10325 used += 1;
10326
10327 if( used <= buf_len )
10328 *p++ = session->mfl_code;
10329#endif
10330
10331#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10332 used += 1;
10333
10334 if( used <= buf_len )
10335 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
10336#endif
10337
10338#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10339 used += 1;
10340
10341 if( used <= buf_len )
10342 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
10343#endif
10344
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010345 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010346 *olen = used;
10347
10348 if( used > buf_len )
10349 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010350
10351 return( 0 );
10352}
10353
10354/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010355 * Public wrapper for ssl_session_save()
10356 */
10357int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
10358 unsigned char *buf,
10359 size_t buf_len,
10360 size_t *olen )
10361{
10362 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
10363}
10364
10365/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010366 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010367 *
10368 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010369 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010370 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010371static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010372 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010373 const unsigned char *buf,
10374 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010375{
10376 const unsigned char *p = buf;
10377 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +010010378 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010379#if defined(MBEDTLS_HAVE_TIME)
10380 uint64_t start;
10381#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010382#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010383#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010384 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +000010385#endif
10386#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010387
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010388 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010389 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010390 /*
10391 * Check version identifier
10392 */
10393
10394 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
10395 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10396
Teppo Järvelin0efac532019-10-04 13:21:08 +030010397 // use regular memcmp as session header is public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010398 if( memcmp( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010399 sizeof( ssl_serialized_session_header ) ) != 0 )
10400 {
10401 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10402 }
10403 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +010010404 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010405
10406 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010407 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010408 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010409#if defined(MBEDTLS_HAVE_TIME)
10410 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010411 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10412
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010413 start = ( (uint64_t) p[0] << 56 ) |
10414 ( (uint64_t) p[1] << 48 ) |
10415 ( (uint64_t) p[2] << 40 ) |
10416 ( (uint64_t) p[3] << 32 ) |
10417 ( (uint64_t) p[4] << 24 ) |
10418 ( (uint64_t) p[5] << 16 ) |
10419 ( (uint64_t) p[6] << 8 ) |
10420 ( (uint64_t) p[7] );
10421 p += 8;
10422
10423 session->start = (time_t) start;
10424#endif /* MBEDTLS_HAVE_TIME */
10425
10426 /*
10427 * Basic mandatory fields
10428 */
Hanno Becker88440552019-07-03 14:16:13 +010010429 {
10430 size_t const ciphersuite_len = 2;
10431#if defined(MBEDTLS_ZLIB_SUPPORT)
10432 size_t const compression_len = 1;
10433#else
10434 size_t const compression_len = 0;
10435#endif
10436 size_t const id_len_len = 1;
10437 size_t const id_len = 32;
10438 size_t const master_len = 48;
10439 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010440
Hanno Becker88440552019-07-03 14:16:13 +010010441 size_t const basic_len =
10442 ciphersuite_len +
10443 compression_len +
10444 id_len_len +
10445 id_len +
10446 master_len +
10447 verif_result_len;
10448
10449 if( basic_len > (size_t)( end - p ) )
10450 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10451 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010452
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010453 ciphersuite = (int)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010454 p += 2;
10455
Hanno Becker73f4cb12019-06-27 13:51:07 +010010456#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010457 session->ciphersuite = ciphersuite;
10458#else
10459 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010460 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010461 {
10462 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10463 }
10464#endif
10465
Hanno Becker88440552019-07-03 14:16:13 +010010466#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010467 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010468#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010469
10470 session->id_len = *p++;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010471 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10472 memcpy( session->id, p, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010473 p += 32;
10474
Teppo Järvelin91d79382019-10-02 09:09:31 +030010475 mbedtls_platform_memcpy( session->master, p, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010476 p += 48;
10477
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010478 session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010479 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010480
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010481 /* Immediately clear invalid pointer values that have been read, in case
10482 * we exit early before we replaced them with valid ones. */
10483#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010484#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010485 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010486#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010487 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010488#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010489#endif
10490#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10491 session->ticket = NULL;
10492#endif
10493
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010494 /*
10495 * Peer certificate
10496 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010497#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010498#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010499 if( 3 > (size_t)( end - p ) )
10500 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10501
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010502 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10503
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010504 p += 3;
10505
10506 if( cert_len == 0 )
10507 {
10508 session->peer_cert = NULL;
10509 }
10510 else
10511 {
10512 int ret;
10513
10514 if( cert_len > (size_t)( end - p ) )
10515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10516
10517 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10518
10519 if( session->peer_cert == NULL )
10520 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10521
10522 mbedtls_x509_crt_init( session->peer_cert );
10523
10524 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10525 p, cert_len ) ) != 0 )
10526 {
10527 mbedtls_x509_crt_free( session->peer_cert );
10528 mbedtls_free( session->peer_cert );
10529 session->peer_cert = NULL;
10530 return( ret );
10531 }
10532
10533 p += cert_len;
10534 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010535#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010536 /* Deserialize CRT digest from the end of the ticket. */
10537 if( 2 > (size_t)( end - p ) )
10538 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10539
10540 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10541 session->peer_cert_digest_len = (size_t) *p++;
10542
10543 if( session->peer_cert_digest_len != 0 )
10544 {
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010545 mbedtls_md_handle_t md_info =
Hanno Becker2326d202019-06-06 14:54:55 +010010546 mbedtls_md_info_from_type( session->peer_cert_digest_type );
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010547 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Hanno Becker2326d202019-06-06 14:54:55 +010010548 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10549 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10551
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010552 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10554
10555 session->peer_cert_digest =
10556 mbedtls_calloc( 1, session->peer_cert_digest_len );
10557 if( session->peer_cert_digest == NULL )
10558 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10559
Teppo Järvelin91d79382019-10-02 09:09:31 +030010560 mbedtls_platform_memcpy( session->peer_cert_digest, p,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010561 session->peer_cert_digest_len );
10562 p += session->peer_cert_digest_len;
10563 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010564#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010565#endif /* MBEDTLS_X509_CRT_PARSE_C */
10566
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010567 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010568 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010569 */
10570#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10571 if( 3 > (size_t)( end - p ) )
10572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10573
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010574 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010575 p += 3;
10576
10577 if( session->ticket_len != 0 )
10578 {
10579 if( session->ticket_len > (size_t)( end - p ) )
10580 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10581
10582 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10583 if( session->ticket == NULL )
10584 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10585
Teppo Järvelin91d79382019-10-02 09:09:31 +030010586 mbedtls_platform_memcpy( session->ticket, p, session->ticket_len );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010587 p += session->ticket_len;
10588 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010589
10590 if( 4 > (size_t)( end - p ) )
10591 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10592
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010593 session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010594 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010595#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10596
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010597 /*
10598 * Misc extension-related info
10599 */
10600#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10601 if( 1 > (size_t)( end - p ) )
10602 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10603
10604 session->mfl_code = *p++;
10605#endif
10606
10607#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10608 if( 1 > (size_t)( end - p ) )
10609 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10610
10611 session->trunc_hmac = *p++;
10612#endif
10613
10614#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10615 if( 1 > (size_t)( end - p ) )
10616 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10617
10618 session->encrypt_then_mac = *p++;
10619#endif
10620
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010621 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010622 if( p != end )
10623 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10624
10625 return( 0 );
10626}
10627
10628/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010629 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010630 */
10631int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10632 const unsigned char *buf,
10633 size_t len )
10634{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010635 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010636
10637 if( ret != 0 )
10638 mbedtls_ssl_session_free( session );
10639
10640 return( ret );
10641}
10642
10643/*
Paul Bakker1961b702013-01-25 14:49:24 +010010644 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010645 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010646int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010647{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010648 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010649
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010650 if( ssl == NULL || ssl->conf == NULL )
10651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010653#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010654 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010655 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010656#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010657#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010658 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010659 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010660#endif
10661
Hanno Beckerb82350b2019-07-26 07:24:05 +010010662 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010663 return( ret );
10664}
10665
10666/*
10667 * Perform the SSL handshake
10668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010669int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010670{
10671 int ret = 0;
10672
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010673 if( ssl == NULL || ssl->conf == NULL )
10674 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010678 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010680 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010681
10682 if( ret != 0 )
10683 break;
10684 }
10685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010687
10688 return( ret );
10689}
10690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010691#if defined(MBEDTLS_SSL_RENEGOTIATION)
10692#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010693/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010694 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010696static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010697{
10698 int ret;
10699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010701
10702 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010703 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10704 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010705
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010706 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010707 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010708 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010709 return( ret );
10710 }
10711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010713
10714 return( 0 );
10715}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010716#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010717
10718/*
10719 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010720 * - any side: calling mbedtls_ssl_renegotiate(),
10721 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10722 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010723 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010724 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010725 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010727static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010728{
10729 int ret;
10730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010732
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010733 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10734 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010735
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010736 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10737 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010738#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010739 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010740 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010741 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010742 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10743 MBEDTLS_SSL_IS_SERVER )
10744 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010745 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010746 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010747 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010748 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010749 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010750 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010751 }
10752#endif
10753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010754 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10755 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010757 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010759 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010760 return( ret );
10761 }
10762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010764
10765 return( 0 );
10766}
10767
10768/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010769 * Renegotiate current connection on client,
10770 * or request renegotiation on server
10771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010772int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010773{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010774 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010775
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010776 if( ssl == NULL || ssl->conf == NULL )
10777 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010779#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010780 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010781 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010783 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10784 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010786 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010787
10788 /* Did we already try/start sending HelloRequest? */
10789 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010790 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010791
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010792 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010793 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010794#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010796#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010797 /*
10798 * On client, either start the renegotiation process or,
10799 * if already in progress, continue the handshake
10800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010801 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010803 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10804 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010805
10806 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010808 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010809 return( ret );
10810 }
10811 }
10812 else
10813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010814 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010817 return( ret );
10818 }
10819 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010820#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010821
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010822 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010823}
10824
10825/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010826 * Check record counters and renegotiate if they're above the limit.
10827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010828static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010829{
Andres AG2196c7f2016-12-15 17:01:16 +000010830 size_t ep_len = ssl_ep_len( ssl );
10831 int in_ctr_cmp;
10832 int out_ctr_cmp;
10833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010834 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10835 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010836 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010837 {
10838 return( 0 );
10839 }
10840
Teppo Järvelin0efac532019-10-04 13:21:08 +030010841 // use regular memcmp as counters are public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010842 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010843 ssl->conf->renego_period + ep_len, 8 - ep_len );
Teppo Järvelin650343c2019-10-03 15:36:59 +030010844 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010845 ssl->conf->renego_period + ep_len, 8 - ep_len );
10846
10847 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010848 {
10849 return( 0 );
10850 }
10851
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010853 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010854}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010855#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010856
10857/*
10858 * Receive application data decrypted from the SSL layer
10859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010860int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010861{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010862 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010863 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010864
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010865 if( ssl == NULL || ssl->conf == NULL )
10866 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010870#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010871 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010873 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010874 return( ret );
10875
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010876 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010877 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010878 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010879 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010880 return( ret );
10881 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010882 }
10883#endif
10884
Hanno Becker4a810fb2017-05-24 16:27:30 +010010885 /*
10886 * Check if renegotiation is necessary and/or handshake is
10887 * in process. If yes, perform/continue, and fall through
10888 * if an unexpected packet is received while the client
10889 * is waiting for the ServerHello.
10890 *
10891 * (There is no equivalent to the last condition on
10892 * the server-side as it is not treated as within
10893 * a handshake while waiting for the ClientHello
10894 * after a renegotiation request.)
10895 */
10896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010897#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010898 ret = ssl_check_ctr_renegotiate( ssl );
10899 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10900 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010902 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010903 return( ret );
10904 }
10905#endif
10906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010907 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010909 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010910 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10911 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010913 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010914 return( ret );
10915 }
10916 }
10917
Hanno Beckere41158b2017-10-23 13:30:32 +010010918 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010919 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010920 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010921 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010922 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10923 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010924 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010925 ssl_set_timer( ssl,
10926 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010927 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010928
Hanno Becker327c93b2018-08-15 13:56:18 +010010929 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010930 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010931 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10932 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010933
Hanno Becker4a810fb2017-05-24 16:27:30 +010010934 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10935 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010936 }
10937
10938 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010939 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010940 {
10941 /*
10942 * OpenSSL sends empty messages to randomize the IV
10943 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010944 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010946 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010947 return( 0 );
10948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010949 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010950 return( ret );
10951 }
10952 }
10953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010954 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010957
Hanno Becker4a810fb2017-05-24 16:27:30 +010010958 /*
10959 * - For client-side, expect SERVER_HELLO_REQUEST.
10960 * - For server-side, expect CLIENT_HELLO.
10961 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10962 */
10963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010964#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010965 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10966 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010967 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010968 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010971
10972 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010973#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010974 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010975 {
10976 continue;
10977 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010978 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010979#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010980#if defined(MBEDTLS_SSL_PROTO_TLS)
10981 {
10982 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10983 }
10984#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010985 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010986#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010987
Hanno Becker4a810fb2017-05-24 16:27:30 +010010988#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010989 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10990 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010991 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010994
10995 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010996#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010997 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010998 {
10999 continue;
11000 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020011001 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020011002#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020011003#if defined(MBEDTLS_SSL_PROTO_TLS)
11004 {
11005 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
11006 }
11007#endif
Paul Bakker48916f92012-09-16 19:57:18 +000011008 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010011009#endif /* MBEDTLS_SSL_SRV_C */
11010
Hanno Becker21df7f92017-10-17 11:03:26 +010011011#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010011012 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011013 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
11014 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010011015 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011016 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
11017 {
11018 /*
11019 * Accept renegotiation request
11020 */
Paul Bakker48916f92012-09-16 19:57:18 +000011021
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011022 /* DTLS clients need to know renego is server-initiated */
11023#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011024 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010011025 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11026 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011027 {
11028 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
11029 }
11030#endif
11031 ret = ssl_start_renegotiation( ssl );
11032 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
11033 ret != 0 )
11034 {
11035 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
11036 return( ret );
11037 }
11038 }
11039 else
Hanno Becker21df7f92017-10-17 11:03:26 +010011040#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000011041 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010011042 /*
11043 * Refuse renegotiation
11044 */
11045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011046 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011048#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010011049 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000011050 {
Gilles Peskine92e44262017-05-10 17:27:49 +020011051 /* SSLv3 does not have a "no_renegotiation" warning, so
11052 we send a fatal alert and abort the connection. */
11053 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11054 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
11055 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011056 }
11057 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011058#endif /* MBEDTLS_SSL_PROTO_SSL3 */
11059#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11060 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011061 if( mbedtls_ssl_ver_geq(
11062 mbedtls_ssl_get_minor_ver( ssl ),
11063 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011064 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010011065 ret = mbedtls_ssl_send_alert_message( ssl,
11066 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11067 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
11068 if( ret != 0 )
11069 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000011070 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020011071 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011072#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
11073 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020011074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
11076 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020011077 }
Paul Bakker48916f92012-09-16 19:57:18 +000011078 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011079
Hanno Becker90333da2017-10-10 11:27:13 +010011080 /* At this point, we don't know whether the renegotiation has been
11081 * completed or not. The cases to consider are the following:
11082 * 1) The renegotiation is complete. In this case, no new record
11083 * has been read yet.
11084 * 2) The renegotiation is incomplete because the client received
11085 * an application data record while awaiting the ServerHello.
11086 * 3) The renegotiation is incomplete because the client received
11087 * a non-handshake, non-application data message while awaiting
11088 * the ServerHello.
11089 * In each of these case, looping will be the proper action:
11090 * - For 1), the next iteration will read a new record and check
11091 * if it's application data.
11092 * - For 2), the loop condition isn't satisfied as application data
11093 * is present, hence continue is the same as break
11094 * - For 3), the loop condition is satisfied and read_record
11095 * will re-deliver the message that was held back by the client
11096 * when expecting the ServerHello.
11097 */
11098 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000011099 }
Hanno Becker21df7f92017-10-17 11:03:26 +010011100#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011101 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011102 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011103 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011104 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011105 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011108 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011109 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011110 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011111 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011112 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011113#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011115 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
11116 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010011119 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011120 }
11121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011122 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000011123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
11125 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000011126 }
11127
11128 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020011129
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011130 /* We're going to return something now, cancel timer,
11131 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011132 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011133 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011134
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020011135#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011136 /* If we requested renego but received AppData, resend HelloRequest.
11137 * Do it now, after setting in_offt, to avoid taking this branch
11138 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011139#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010011140 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11141 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011142 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011143 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011144 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011146 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011147 return( ret );
11148 }
11149 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011150#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010011151#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000011152 }
11153
11154 n = ( len < ssl->in_msglen )
11155 ? len : ssl->in_msglen;
11156
Teppo Järvelin91d79382019-10-02 09:09:31 +030011157 mbedtls_platform_memcpy( buf, ssl->in_offt, n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011158 ssl->in_msglen -= n;
11159
Teppo Järvelincafb6c92020-01-08 09:19:07 +020011160 // clear incoming data after it's copied to buffer
11161 mbedtls_platform_memset(ssl->in_offt, 0, n);
11162
Paul Bakker5121ce52009-01-03 21:22:43 +000011163 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010011164 {
11165 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000011166 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010011167 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011168 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011169 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010011170 {
Paul Bakker5121ce52009-01-03 21:22:43 +000011171 /* more data available */
11172 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011173 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011176
Paul Bakker23986e52011-04-24 08:57:21 +000011177 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011178}
11179
11180/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011181 * Send application data to be encrypted by the SSL layer, taking care of max
11182 * fragment length and buffer size.
11183 *
11184 * According to RFC 5246 Section 6.2.1:
11185 *
11186 * Zero-length fragments of Application data MAY be sent as they are
11187 * potentially useful as a traffic analysis countermeasure.
11188 *
11189 * Therefore, it is possible that the input message length is 0 and the
11190 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000011191 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011192static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011193 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000011194{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020011195 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
11196 const size_t max_len = (size_t) ret;
11197
11198 if( ret < 0 )
11199 {
11200 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
11201 return( ret );
11202 }
11203
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011204 if( len > max_len )
11205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011206#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011207 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011210 "maximum fragment length: %d > %d",
11211 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011212 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011213 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011214 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011215#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011216#if defined(MBEDTLS_SSL_PROTO_TLS)
11217 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011218 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011219 }
11220#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011221 }
Paul Bakker887bd502011-06-08 13:10:54 +000011222
Paul Bakker5121ce52009-01-03 21:22:43 +000011223 if( ssl->out_left != 0 )
11224 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011225 /*
11226 * The user has previously tried to send the data and
11227 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
11228 * written. In this case, we expect the high-level write function
11229 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
11230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011231 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011233 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011234 return( ret );
11235 }
11236 }
Paul Bakker887bd502011-06-08 13:10:54 +000011237 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000011238 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011239 /*
11240 * The user is trying to send a message the first time, so we need to
11241 * copy the data into the internal buffers and setup the data structure
11242 * to keep track of partial writes
11243 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011244 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011245 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Teppo Järvelin91d79382019-10-02 09:09:31 +030011246 mbedtls_platform_memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000011247
Hanno Becker67bc7c32018-08-06 11:33:50 +010011248 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000011249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011250 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000011251 return( ret );
11252 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011253 }
11254
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011255 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000011256}
11257
11258/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011259 * Write application data, doing 1/n-1 splitting if necessary.
11260 *
11261 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011262 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010011263 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011265#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011266static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011267 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011268{
11269 int ret;
11270
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011271 if( ssl->conf->cbc_record_splitting ==
11272 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011273 len <= 1 ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011274 mbedtls_ssl_ver_gt(
11275 mbedtls_ssl_get_minor_ver( ssl ),
11276 MBEDTLS_SSL_MINOR_VERSION_1 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011277 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
11278 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011279 {
11280 return( ssl_write_real( ssl, buf, len ) );
11281 }
11282
11283 if( ssl->split_done == 0 )
11284 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011285 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011286 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011287 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011288 }
11289
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011290 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
11291 return( ret );
11292 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011293
11294 return( ret + 1 );
11295}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011296#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011297
11298/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011299 * Write application data (public-facing wrapper)
11300 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011301int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011302{
11303 int ret;
11304
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011306
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011307 if( ssl == NULL || ssl->conf == NULL )
11308 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11309
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011310#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011311 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
11312 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011313 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011314 return( ret );
11315 }
11316#endif
11317
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011318 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011319 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011320 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011321 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020011322 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011323 return( ret );
11324 }
11325 }
11326
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011327#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011328 ret = ssl_write_split( ssl, buf, len );
11329#else
11330 ret = ssl_write_real( ssl, buf, len );
11331#endif
11332
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011334
11335 return( ret );
11336}
11337
11338/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011339 * Notify the peer that the connection is being closed
11340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011341int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011342{
11343 int ret;
11344
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011345 if( ssl == NULL || ssl->conf == NULL )
11346 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011349
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011350 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011351 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011353 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000011354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011355 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
11356 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11357 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011360 return( ret );
11361 }
11362 }
11363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011365
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011366 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000011367}
11368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011369void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000011370{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011371 if( transform == NULL )
11372 return;
11373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011374#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000011375 deflateEnd( &transform->ctx_deflate );
11376 inflateEnd( &transform->ctx_inflate );
11377#endif
11378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011379 mbedtls_cipher_free( &transform->cipher_ctx_enc );
11380 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020011381
Hanno Becker92231322018-01-03 15:32:51 +000011382#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011383 mbedtls_md_free( &transform->md_ctx_enc );
11384 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000011385#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011386
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011387 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011388}
11389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011390#if defined(MBEDTLS_X509_CRT_PARSE_C)
11391static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011392{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011393 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011394
11395 while( cur != NULL )
11396 {
11397 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011398 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011399 cur = next;
11400 }
11401}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011402#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011403
Hanno Becker0271f962018-08-16 13:23:47 +010011404#if defined(MBEDTLS_SSL_PROTO_DTLS)
11405
11406static void ssl_buffering_free( mbedtls_ssl_context *ssl )
11407{
11408 unsigned offset;
11409 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11410
11411 if( hs == NULL )
11412 return;
11413
Hanno Becker283f5ef2018-08-24 09:34:47 +010011414 ssl_free_buffered_record( ssl );
11415
Hanno Becker0271f962018-08-16 13:23:47 +010011416 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010011417 ssl_buffering_free_slot( ssl, offset );
11418}
11419
11420static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
11421 uint8_t slot )
11422{
11423 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11424 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010011425
11426 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
11427 return;
11428
Hanno Beckere605b192018-08-21 15:59:07 +010011429 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010011430 {
Hanno Beckere605b192018-08-21 15:59:07 +010011431 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010011432 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010011433 mbedtls_free( hs_buf->data );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020011434 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010011435 }
11436}
11437
11438#endif /* MBEDTLS_SSL_PROTO_DTLS */
11439
Gilles Peskine9b562d52018-04-25 20:32:43 +020011440void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011441{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011442 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11443
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011444 if( handshake == NULL )
11445 return;
11446
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011447#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11448 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11449 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011450 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011451 handshake->async_in_progress = 0;
11452 }
11453#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11454
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011455#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11456 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11457 mbedtls_md5_free( &handshake->fin_md5 );
11458 mbedtls_sha1_free( &handshake->fin_sha1 );
11459#endif
11460#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11461#if defined(MBEDTLS_SHA256_C)
11462 mbedtls_sha256_free( &handshake->fin_sha256 );
11463#endif
11464#if defined(MBEDTLS_SHA512_C)
11465 mbedtls_sha512_free( &handshake->fin_sha512 );
11466#endif
11467#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011469#if defined(MBEDTLS_DHM_C)
11470 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011471#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011472#if defined(MBEDTLS_ECDH_C)
11473 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011474#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011475#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011476 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011477#if defined(MBEDTLS_SSL_CLI_C)
11478 mbedtls_free( handshake->ecjpake_cache );
11479 handshake->ecjpake_cache = NULL;
11480 handshake->ecjpake_cache_len = 0;
11481#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011482#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011483
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011484#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11485 if( handshake->psk != NULL )
11486 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011487 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011488 mbedtls_free( handshake->psk );
11489 }
11490#endif
11491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011492#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11493 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011494 /*
11495 * Free only the linked list wrapper, not the keys themselves
11496 * since the belong to the SNI callback
11497 */
11498 if( handshake->sni_key_cert != NULL )
11499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011500 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011501
11502 while( cur != NULL )
11503 {
11504 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011505 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011506 cur = next;
11507 }
11508 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011509#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011510
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011511#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011512 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011513 if( handshake->ecrs_peer_cert != NULL )
11514 {
11515 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11516 mbedtls_free( handshake->ecrs_peer_cert );
11517 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011518#endif
11519
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011520#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11521 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11522 mbedtls_pk_free( &handshake->peer_pubkey );
11523#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011525#if defined(MBEDTLS_SSL_PROTO_DTLS)
11526 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011527 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011528 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011529#endif
11530
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011531 mbedtls_platform_zeroize( handshake,
11532 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011533}
11534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011535void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011536{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011537 if( session == NULL )
11538 return;
11539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011540#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011541 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011542#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011543
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011544#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011545 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011546#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011547
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011548 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011549}
11550
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011551#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011552
11553#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11554#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11555#else
11556#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11557#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11558
11559#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11560#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11561#else
11562#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11563#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11564
11565#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11566#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11567#else
11568#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11569#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11570
11571#if defined(MBEDTLS_SSL_ALPN)
11572#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11573#else
11574#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11575#endif /* MBEDTLS_SSL_ALPN */
11576
11577#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11578#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11579#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11580#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11581
11582#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11583 ( (uint32_t) ( \
11584 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11585 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11586 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11587 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11588 0u ) )
11589
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011590static unsigned char ssl_serialized_context_header[] = {
11591 MBEDTLS_VERSION_MAJOR,
11592 MBEDTLS_VERSION_MINOR,
11593 MBEDTLS_VERSION_PATCH,
11594 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11595 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011596 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11597 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11598 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011599};
11600
Paul Bakker5121ce52009-01-03 21:22:43 +000011601/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011602 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011603 *
11604 * The format of the serialized data is:
11605 * (in the presentation language of TLS, RFC 8446 section 3)
11606 *
11607 * // header
11608 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011609 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011610 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011611 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011612 * Note: When updating the format, remember to keep these
11613 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011614 *
11615 * // session sub-structure
11616 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11617 * // transform sub-structure
11618 * uint8 random[64]; // ServerHello.random+ClientHello.random
11619 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11620 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11621 * // fields from ssl_context
11622 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11623 * uint64 in_window_top; // DTLS: last validated record seq_num
11624 * uint64 in_window; // DTLS: bitmask for replay protection
11625 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11626 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11627 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11628 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11629 *
11630 * Note that many fields of the ssl_context or sub-structures are not
11631 * serialized, as they fall in one of the following categories:
11632 *
11633 * 1. forced value (eg in_left must be 0)
11634 * 2. pointer to dynamically-allocated memory (eg session, transform)
11635 * 3. value can be re-derived from other data (eg session keys from MS)
11636 * 4. value was temporary (eg content of input buffer)
11637 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011638 */
11639int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11640 unsigned char *buf,
11641 size_t buf_len,
11642 size_t *olen )
11643{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011644 unsigned char *p = buf;
11645 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011646 size_t session_len;
11647 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011648
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011649 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011650 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11651 * this function's documentation.
11652 *
11653 * These are due to assumptions/limitations in the implementation. Some of
11654 * them are likely to stay (no handshake in progress) some might go away
11655 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011656 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011657 /* The initial handshake must be over */
11658 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011659 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011660 if( ssl->handshake != NULL )
11661 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11662 /* Double-check that sub-structures are indeed ready */
11663 if( ssl->transform == NULL || ssl->session == NULL )
11664 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11665 /* There must be no pending incoming or outgoing data */
11666 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11667 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11668 if( ssl->out_left != 0 )
11669 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11670 /* Protocol must be DLTS, not TLS */
11671 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11672 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11673 /* Version must be 1.2 */
11674 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11675 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11676 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11677 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11678 /* We must be using an AEAD ciphersuite */
11679 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11680 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11681 /* Renegotiation must not be enabled */
11682 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11683 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011684
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011685 /*
11686 * Version and format identifier
11687 */
11688 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011689
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011690 if( used <= buf_len )
11691 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011692 mbedtls_platform_memcpy( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011693 sizeof( ssl_serialized_context_header ) );
11694 p += sizeof( ssl_serialized_context_header );
11695 }
11696
11697 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011698 * Session (length + data)
11699 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011700 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011701 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11702 return( ret );
11703
11704 used += 4 + session_len;
11705 if( used <= buf_len )
11706 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011707 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011708
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011709 ret = ssl_session_save( ssl->session, 1,
11710 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011711 if( ret != 0 )
11712 return( ret );
11713
11714 p += session_len;
11715 }
11716
11717 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011718 * Transform
11719 */
11720 used += sizeof( ssl->transform->randbytes );
11721 if( used <= buf_len )
11722 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011723 mbedtls_platform_memcpy( p, ssl->transform->randbytes,
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011724 sizeof( ssl->transform->randbytes ) );
11725 p += sizeof( ssl->transform->randbytes );
11726 }
11727
11728#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11729 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11730 if( used <= buf_len )
11731 {
11732 *p++ = ssl->transform->in_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011733 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11734 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011735 p += ssl->transform->in_cid_len;
11736
11737 *p++ = ssl->transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011738 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11739 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011740 p += ssl->transform->out_cid_len;
11741 }
11742#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11743
11744 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011745 * Saved fields from top-level ssl_context structure
11746 */
11747#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11748 used += 4;
11749 if( used <= buf_len )
11750 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011751 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011752 }
11753#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11754
11755#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11756 used += 16;
11757 if( used <= buf_len )
11758 {
11759 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11760 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11761 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11762 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11763 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11764 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11765 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11766 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11767
11768 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11769 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11770 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11771 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11772 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11773 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11774 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11775 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11776 }
11777#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11778
11779#if defined(MBEDTLS_SSL_PROTO_DTLS)
11780 used += 1;
11781 if( used <= buf_len )
11782 {
11783 *p++ = ssl->disable_datagram_packing;
11784 }
11785#endif /* MBEDTLS_SSL_PROTO_DTLS */
11786
11787 used += 8;
11788 if( used <= buf_len )
11789 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011790 mbedtls_platform_memcpy( p, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011791 p += 8;
11792 }
11793
11794#if defined(MBEDTLS_SSL_PROTO_DTLS)
11795 used += 2;
11796 if( used <= buf_len )
11797 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011798 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011799 }
11800#endif /* MBEDTLS_SSL_PROTO_DTLS */
11801
11802#if defined(MBEDTLS_SSL_ALPN)
11803 {
11804 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011805 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011806 : 0;
11807
11808 used += 1 + alpn_len;
11809 if( used <= buf_len )
11810 {
11811 *p++ = alpn_len;
11812
11813 if( ssl->alpn_chosen != NULL )
11814 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011815 mbedtls_platform_memcpy( p, ssl->alpn_chosen, alpn_len );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011816 p += alpn_len;
11817 }
11818 }
11819 }
11820#endif /* MBEDTLS_SSL_ALPN */
11821
11822 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011823 * Done
11824 */
11825 *olen = used;
11826
11827 if( used > buf_len )
11828 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011829
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011830 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11831
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011832 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011833}
11834
11835/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011836 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011837 *
11838 * This internal version is wrapped by a public function that cleans up in
11839 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011840 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011841static int ssl_context_load( mbedtls_ssl_context *ssl,
11842 const unsigned char *buf,
11843 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011844{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011845 const unsigned char *p = buf;
11846 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011847 size_t session_len;
11848 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011849
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011850 /*
11851 * The context should have been freshly setup or reset.
11852 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011853 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011854 * renegotiating, or if the user mistakenly loaded a session first.)
11855 */
11856 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11857 ssl->session != NULL )
11858 {
11859 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11860 }
11861
11862 /*
11863 * We can't check that the config matches the initial one, but we can at
11864 * least check it matches the requirements for serializing.
11865 */
11866 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011867 mbedtls_ssl_ver_lt(
11868 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ),
11869 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11870 mbedtls_ssl_ver_gt(
11871 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ),
11872 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11873 mbedtls_ssl_ver_lt(
11874 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ),
11875 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
11876 mbedtls_ssl_ver_gt(
11877 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ),
11878 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011879 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011880 {
11881 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11882 }
11883
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011884 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11885
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011886 /*
11887 * Check version identifier
11888 */
11889 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11890 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11891
Teppo Järvelin650343c2019-10-03 15:36:59 +030011892 // use regular memcmp as header is not that critical
11893 if( memcmp( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011894 sizeof( ssl_serialized_context_header ) ) != 0 )
11895 {
11896 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11897 }
11898 p += sizeof( ssl_serialized_context_header );
11899
11900 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011901 * Session
11902 */
11903 if( (size_t)( end - p ) < 4 )
11904 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11905
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011906 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011907 p += 4;
11908
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011909 /* This has been allocated by ssl_handshake_init(), called by
11910 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11911 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011912 ssl->session_in = ssl->session;
11913 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011914 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011915
11916 if( (size_t)( end - p ) < session_len )
11917 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11918
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011919 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011920 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011921 {
11922 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011923 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011924 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011925
11926 p += session_len;
11927
11928 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011929 * Transform
11930 */
11931
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011932 /* This has been allocated by ssl_handshake_init(), called by
11933 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11934 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011935 ssl->transform_in = ssl->transform;
11936 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011937 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011938
11939 /* Read random bytes and populate structure */
11940 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11941 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11942
11943 ret = ssl_populate_transform( ssl->transform,
11944 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11945 ssl->session->master,
11946#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11947#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11948 ssl->session->encrypt_then_mac,
11949#endif
11950#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11951 ssl->session->trunc_hmac,
11952#endif
11953#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11954#if defined(MBEDTLS_ZLIB_SUPPORT)
11955 ssl->session->compression,
11956#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011957 p, /* currently pointing to randbytes */
11958 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11959 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11960 ssl );
11961 if( ret != 0 )
11962 return( ret );
11963
11964 p += sizeof( ssl->transform->randbytes );
11965
11966#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11967 /* Read connection IDs and store them */
11968 if( (size_t)( end - p ) < 1 )
11969 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11970
11971 ssl->transform->in_cid_len = *p++;
11972
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011973 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011974 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11975
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011976 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11977 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011978 p += ssl->transform->in_cid_len;
11979
11980 ssl->transform->out_cid_len = *p++;
11981
11982 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11983 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11984
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011985 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11986 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011987 p += ssl->transform->out_cid_len;
11988#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11989
11990 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011991 * Saved fields from top-level ssl_context structure
11992 */
11993#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11994 if( (size_t)( end - p ) < 4 )
11995 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11996
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011997 ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011998 p += 4;
11999#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
12000
12001#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
12002 if( (size_t)( end - p ) < 16 )
12003 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12004
12005 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
12006 ( (uint64_t) p[1] << 48 ) |
12007 ( (uint64_t) p[2] << 40 ) |
12008 ( (uint64_t) p[3] << 32 ) |
12009 ( (uint64_t) p[4] << 24 ) |
12010 ( (uint64_t) p[5] << 16 ) |
12011 ( (uint64_t) p[6] << 8 ) |
12012 ( (uint64_t) p[7] );
12013 p += 8;
12014
12015 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
12016 ( (uint64_t) p[1] << 48 ) |
12017 ( (uint64_t) p[2] << 40 ) |
12018 ( (uint64_t) p[3] << 32 ) |
12019 ( (uint64_t) p[4] << 24 ) |
12020 ( (uint64_t) p[5] << 16 ) |
12021 ( (uint64_t) p[6] << 8 ) |
12022 ( (uint64_t) p[7] );
12023 p += 8;
12024#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
12025
12026#if defined(MBEDTLS_SSL_PROTO_DTLS)
12027 if( (size_t)( end - p ) < 1 )
12028 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12029
12030 ssl->disable_datagram_packing = *p++;
12031#endif /* MBEDTLS_SSL_PROTO_DTLS */
12032
12033 if( (size_t)( end - p ) < 8 )
12034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12035
Teppo Järvelin91d79382019-10-02 09:09:31 +030012036 mbedtls_platform_memcpy( ssl->cur_out_ctr, p, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012037 p += 8;
12038
12039#if defined(MBEDTLS_SSL_PROTO_DTLS)
12040 if( (size_t)( end - p ) < 2 )
12041 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030012042 ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012043 p += 2;
12044#endif /* MBEDTLS_SSL_PROTO_DTLS */
12045
12046#if defined(MBEDTLS_SSL_ALPN)
12047 {
12048 uint8_t alpn_len;
12049 const char **cur;
12050
12051 if( (size_t)( end - p ) < 1 )
12052 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12053
12054 alpn_len = *p++;
12055
12056 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
12057 {
12058 /* alpn_chosen should point to an item in the configured list */
12059 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
12060 {
12061 if( strlen( *cur ) == alpn_len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +030012062 mbedtls_platform_memcmp( p, cur, alpn_len ) == 0 )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012063 {
12064 ssl->alpn_chosen = *cur;
12065 break;
12066 }
12067 }
12068 }
12069
12070 /* can only happen on conf mismatch */
12071 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
12072 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12073
12074 p += alpn_len;
12075 }
12076#endif /* MBEDTLS_SSL_ALPN */
12077
12078 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012079 * Forced fields from top-level ssl_context structure
12080 *
12081 * Most of them already set to the correct value by mbedtls_ssl_init() and
12082 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
12083 */
12084 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
12085
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012086#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012087 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012088#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
12089#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012090 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012091#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012092
Hanno Becker83985822019-08-30 10:42:49 +010012093 /* Adjust pointers for header fields of outgoing records to
12094 * the given transform, accounting for explicit IV and CID. */
12095 ssl_update_out_pointers( ssl, ssl->transform );
12096
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012097#if defined(MBEDTLS_SSL_PROTO_DTLS)
12098 ssl->in_epoch = 1;
12099#endif
12100
12101 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
12102 * which we don't want - otherwise we'd end up freeing the wrong transform
12103 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
12104 if( ssl->handshake != NULL )
12105 {
12106 mbedtls_ssl_handshake_free( ssl );
12107 mbedtls_free( ssl->handshake );
12108 ssl->handshake = NULL;
12109 }
12110
12111 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020012112 * Done - should have consumed entire buffer
12113 */
12114 if( p != end )
12115 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012116
12117 return( 0 );
12118}
12119
12120/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020012121 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012122 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012123int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012124 const unsigned char *buf,
12125 size_t len )
12126{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012127 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012128
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012129 if( ret != 0 )
12130 mbedtls_ssl_free( context );
12131
12132 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012133}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020012134#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012135
12136/*
Paul Bakker5121ce52009-01-03 21:22:43 +000012137 * Free an SSL context
12138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012139void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000012140{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020012141 if( ssl == NULL )
12142 return;
12143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012145
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012146 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012147 {
Angus Grattond8213d02016-05-25 20:56:48 +100012148 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012149 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012150 }
12151
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012152 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012153 {
Angus Grattond8213d02016-05-25 20:56:48 +100012154 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012155 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012156 }
12157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012158#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020012159 if( ssl->compress_buf != NULL )
12160 {
Angus Grattond8213d02016-05-25 20:56:48 +100012161 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012162 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020012163 }
12164#endif
12165
Paul Bakker48916f92012-09-16 19:57:18 +000012166 if( ssl->transform )
12167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012168 mbedtls_ssl_transform_free( ssl->transform );
12169 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000012170 }
12171
12172 if( ssl->handshake )
12173 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020012174 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012175 mbedtls_ssl_transform_free( ssl->transform_negotiate );
12176 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012178 mbedtls_free( ssl->handshake );
12179 mbedtls_free( ssl->transform_negotiate );
12180 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012181 }
12182
Paul Bakkerc0463502013-02-14 11:19:38 +010012183 if( ssl->session )
12184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012185 mbedtls_ssl_session_free( ssl->session );
12186 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010012187 }
12188
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030012189#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020012190 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012191 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012192 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012193 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000012194 }
Paul Bakker0be444a2013-08-27 21:55:01 +020012195#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000012196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012197#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
12198 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000012199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
12201 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000012202 }
12203#endif
12204
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012205#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012206 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020012207#endif
12208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000012210
Paul Bakker86f04f42013-02-14 11:20:09 +010012211 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012212 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012213}
12214
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012215/*
12216 * Initialze mbedtls_ssl_config
12217 */
12218void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
12219{
12220 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010012221
12222#if !defined(MBEDTLS_SSL_PROTO_TLS)
12223 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
12224#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012225}
12226
Simon Butcherc97b6972015-12-27 23:48:17 +000012227#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012228#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012229static int ssl_preset_default_hashes[] = {
12230#if defined(MBEDTLS_SHA512_C)
12231 MBEDTLS_MD_SHA512,
12232 MBEDTLS_MD_SHA384,
12233#endif
12234#if defined(MBEDTLS_SHA256_C)
12235 MBEDTLS_MD_SHA256,
12236 MBEDTLS_MD_SHA224,
12237#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020012238#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012239 MBEDTLS_MD_SHA1,
12240#endif
12241 MBEDTLS_MD_NONE
12242};
Simon Butcherc97b6972015-12-27 23:48:17 +000012243#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012244#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012245
Hanno Becker73f4cb12019-06-27 13:51:07 +010012246#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012247static int ssl_preset_suiteb_ciphersuites[] = {
12248 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
12249 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
12250 0
12251};
Hanno Becker73f4cb12019-06-27 13:51:07 +010012252#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012253
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012254#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012255#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012256static int ssl_preset_suiteb_hashes[] = {
12257 MBEDTLS_MD_SHA256,
12258 MBEDTLS_MD_SHA384,
12259 MBEDTLS_MD_NONE
12260};
12261#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012262#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012263
Hanno Beckerc1096e72019-06-19 12:30:41 +010012264#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012265static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010012266#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012267 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012268#endif
12269#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012270 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012271#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012272 MBEDTLS_ECP_DP_NONE
12273};
12274#endif
12275
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012276/*
Tillmann Karras588ad502015-09-25 04:27:22 +020012277 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012278 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012279int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012280 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012281{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012282#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012283 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012284#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012285
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020012286 /* Use the functions here so that they are covered in tests,
12287 * but otherwise access member directly for efficiency */
12288 mbedtls_ssl_conf_endpoint( conf, endpoint );
12289 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012290
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012291 /*
12292 * Things that are common to all presets
12293 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012294#if defined(MBEDTLS_SSL_CLI_C)
12295 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
12296 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012297#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012298 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012299#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012300#if defined(MBEDTLS_SSL_SESSION_TICKETS)
12301 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
12302#endif
12303 }
12304#endif
12305
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012306#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012307 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012308#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012309
12310#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
12311 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
12312#endif
12313
12314#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010012315#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012316 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012317#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
12318#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030012319 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030012320 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012321#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012322#endif
12323
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010012324#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
12325 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
12326#endif
12327
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012328#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012329 conf->f_cookie_write = ssl_cookie_write_dummy;
12330 conf->f_cookie_check = ssl_cookie_check_dummy;
12331#endif
12332
Hanno Becker7f376f42019-06-12 16:20:48 +010012333#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
12334 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012335 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
12336#endif
12337
Janos Follath088ce432017-04-10 12:42:31 +010012338#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012339#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010012340 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012341#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
12342#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010012343
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012344#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010012345#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012346 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012347#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
12348#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012349 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012350#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
12351#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012352
12353#if defined(MBEDTLS_SSL_RENEGOTIATION)
12354 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020012355 mbedtls_platform_memset( conf->renego_period, 0x00, 2 );
12356 mbedtls_platform_memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012357#endif
12358
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012359#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
12360 if( endpoint == MBEDTLS_SSL_IS_SERVER )
12361 {
Hanno Becker00d0a682017-10-04 13:14:29 +010012362 const unsigned char dhm_p[] =
12363 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
12364 const unsigned char dhm_g[] =
12365 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
12366
Hanno Beckera90658f2017-10-04 15:29:08 +010012367 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
12368 dhm_p, sizeof( dhm_p ),
12369 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012370 {
12371 return( ret );
12372 }
12373 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020012374#endif
12375
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012376 /*
12377 * Preset-specific defaults
12378 */
12379 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012380 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012381 /*
12382 * NSA Suite B
12383 */
12384 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010012385#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012386 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010012387#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12388#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012389 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010012390#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12391#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012392 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012393#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12394#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012395 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012396#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012397
Hanno Becker73f4cb12019-06-27 13:51:07 +010012398#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012399 conf->ciphersuite_list[0] =
12400 conf->ciphersuite_list[1] =
12401 conf->ciphersuite_list[2] =
12402 conf->ciphersuite_list[3] =
12403 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010012404#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012405
12406#if defined(MBEDTLS_X509_CRT_PARSE_C)
12407 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012408#endif
12409
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012410#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012411#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012412 conf->sig_hashes = ssl_preset_suiteb_hashes;
12413#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012414#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012415
12416#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012417#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012418 conf->curve_list = ssl_preset_suiteb_curves;
12419#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012420#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020012421 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012422
12423 /*
12424 * Default
12425 */
12426 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010012427#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012428 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
12429 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
12430 MBEDTLS_SSL_MIN_MAJOR_VERSION :
12431 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012432#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12433#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012434 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
12435 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
12436 MBEDTLS_SSL_MIN_MINOR_VERSION :
12437 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012438#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020012439 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012440 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
12441#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010012442#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12443#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
12444 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12445#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12446#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12447 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12448#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012449
Hanno Becker73f4cb12019-06-27 13:51:07 +010012450#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012451 conf->ciphersuite_list[0] =
12452 conf->ciphersuite_list[1] =
12453 conf->ciphersuite_list[2] =
12454 conf->ciphersuite_list[3] =
12455 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012456#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012457
12458#if defined(MBEDTLS_X509_CRT_PARSE_C)
12459 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12460#endif
12461
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012462#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012463#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012464 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012465#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012466#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012467
12468#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012469#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012470 conf->curve_list = mbedtls_ecp_grp_id_list();
12471#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012472#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012473
12474#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12475 conf->dhm_min_bitlen = 1024;
12476#endif
12477 }
12478
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012479 return( 0 );
12480}
12481
12482/*
12483 * Free mbedtls_ssl_config
12484 */
12485void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12486{
12487#if defined(MBEDTLS_DHM_C)
12488 mbedtls_mpi_free( &conf->dhm_P );
12489 mbedtls_mpi_free( &conf->dhm_G );
12490#endif
12491
12492#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12493 if( conf->psk != NULL )
12494 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012495 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012496 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012497 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012498 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012499 }
12500
12501 if( conf->psk_identity != NULL )
12502 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012503 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012504 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012505 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012506 conf->psk_identity_len = 0;
12507 }
12508#endif
12509
12510#if defined(MBEDTLS_X509_CRT_PARSE_C)
12511 ssl_key_cert_free( conf->key_cert );
12512#endif
12513
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012514 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012515}
12516
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012517#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012518 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12519 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012520/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012521 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012523unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012524{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012525#if defined(MBEDTLS_RSA_C)
12526 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12527 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012528#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012529#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012530 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12531 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012532#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012533 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012534}
12535
Hanno Becker7e5437a2017-04-28 17:15:26 +010012536unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12537{
12538 switch( type ) {
12539 case MBEDTLS_PK_RSA:
12540 return( MBEDTLS_SSL_SIG_RSA );
12541 case MBEDTLS_PK_ECDSA:
12542 case MBEDTLS_PK_ECKEY:
12543 return( MBEDTLS_SSL_SIG_ECDSA );
12544 default:
12545 return( MBEDTLS_SSL_SIG_ANON );
12546 }
12547}
12548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012549mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012550{
12551 switch( sig )
12552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012553#if defined(MBEDTLS_RSA_C)
12554 case MBEDTLS_SSL_SIG_RSA:
12555 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012556#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012557#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012558 case MBEDTLS_SSL_SIG_ECDSA:
12559 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012560#endif
12561 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012562 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012563 }
12564}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012565#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012566
Hanno Becker7e5437a2017-04-28 17:15:26 +010012567#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12568 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12569
12570/* Find an entry in a signature-hash set matching a given hash algorithm. */
12571mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12572 mbedtls_pk_type_t sig_alg )
12573{
12574 switch( sig_alg )
12575 {
12576 case MBEDTLS_PK_RSA:
12577 return( set->rsa );
12578 case MBEDTLS_PK_ECDSA:
12579 return( set->ecdsa );
12580 default:
12581 return( MBEDTLS_MD_NONE );
12582 }
12583}
12584
12585/* Add a signature-hash-pair to a signature-hash set */
12586void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12587 mbedtls_pk_type_t sig_alg,
12588 mbedtls_md_type_t md_alg )
12589{
12590 switch( sig_alg )
12591 {
12592 case MBEDTLS_PK_RSA:
12593 if( set->rsa == MBEDTLS_MD_NONE )
12594 set->rsa = md_alg;
12595 break;
12596
12597 case MBEDTLS_PK_ECDSA:
12598 if( set->ecdsa == MBEDTLS_MD_NONE )
12599 set->ecdsa = md_alg;
12600 break;
12601
12602 default:
12603 break;
12604 }
12605}
12606
12607/* Allow exactly one hash algorithm for each signature. */
12608void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12609 mbedtls_md_type_t md_alg )
12610{
12611 set->rsa = md_alg;
12612 set->ecdsa = md_alg;
12613}
12614
12615#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12616 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12617
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012618/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012619 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012621mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012622{
12623 switch( hash )
12624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012625#if defined(MBEDTLS_MD5_C)
12626 case MBEDTLS_SSL_HASH_MD5:
12627 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012628#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012629#if defined(MBEDTLS_SHA1_C)
12630 case MBEDTLS_SSL_HASH_SHA1:
12631 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012632#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012633#if defined(MBEDTLS_SHA256_C)
12634 case MBEDTLS_SSL_HASH_SHA224:
12635 return( MBEDTLS_MD_SHA224 );
12636 case MBEDTLS_SSL_HASH_SHA256:
12637 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012638#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012639#if defined(MBEDTLS_SHA512_C)
12640 case MBEDTLS_SSL_HASH_SHA384:
12641 return( MBEDTLS_MD_SHA384 );
12642 case MBEDTLS_SSL_HASH_SHA512:
12643 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012644#endif
12645 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012646 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012647 }
12648}
12649
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012650/*
12651 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12652 */
12653unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12654{
12655 switch( md )
12656 {
12657#if defined(MBEDTLS_MD5_C)
12658 case MBEDTLS_MD_MD5:
12659 return( MBEDTLS_SSL_HASH_MD5 );
12660#endif
12661#if defined(MBEDTLS_SHA1_C)
12662 case MBEDTLS_MD_SHA1:
12663 return( MBEDTLS_SSL_HASH_SHA1 );
12664#endif
12665#if defined(MBEDTLS_SHA256_C)
12666 case MBEDTLS_MD_SHA224:
12667 return( MBEDTLS_SSL_HASH_SHA224 );
12668 case MBEDTLS_MD_SHA256:
12669 return( MBEDTLS_SSL_HASH_SHA256 );
12670#endif
12671#if defined(MBEDTLS_SHA512_C)
12672 case MBEDTLS_MD_SHA384:
12673 return( MBEDTLS_SSL_HASH_SHA384 );
12674 case MBEDTLS_MD_SHA512:
12675 return( MBEDTLS_SSL_HASH_SHA512 );
12676#endif
12677 default:
12678 return( MBEDTLS_SSL_HASH_NONE );
12679 }
12680}
12681
Hanno Beckeree902df2019-08-23 13:47:47 +010012682#if defined(MBEDTLS_USE_TINYCRYPT)
12683/*
12684 * Check if a curve proposed by the peer is in our list.
12685 * Return 0 if we're willing to use it, -1 otherwise.
12686 */
12687int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12688 mbedtls_uecc_group_id grp_id )
12689{
12690 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12691 if( own_ec_id == grp_id )
12692 return( 0 );
12693 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12694
12695 return( -1 );
12696}
12697#endif /* MBEDTLS_USE_TINYCRYPT */
12698
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012699#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012700/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012701 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012702 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012703 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012704int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12705 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012706{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012707 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12708 if( own_ec_id == grp_id )
12709 return( 0 );
12710 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012711
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012712 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012713}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012714#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012715
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012716#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012717/*
12718 * Check if a hash proposed by the peer is in our list.
12719 * Return 0 if we're willing to use it, -1 otherwise.
12720 */
12721int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12722 mbedtls_md_type_t md )
12723{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012724 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12725 if( md_alg == md )
12726 return( 0 );
12727 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012728
12729 return( -1 );
12730}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012731#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012733#if defined(MBEDTLS_X509_CRT_PARSE_C)
12734int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012735 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012736 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012737 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012738{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012739 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012740#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012741 int usage = 0;
12742#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012743#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012744 const char *ext_oid;
12745 size_t ext_len;
12746#endif
12747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012748#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12749 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012750 ((void) cert);
12751 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012752 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012753#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012755#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12756 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012757 {
12758 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012759 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012761 case MBEDTLS_KEY_EXCHANGE_RSA:
12762 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012763 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012764 break;
12765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012766 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12767 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12768 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12769 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012770 break;
12771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012772 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12773 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012774 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012775 break;
12776
12777 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012778 case MBEDTLS_KEY_EXCHANGE_NONE:
12779 case MBEDTLS_KEY_EXCHANGE_PSK:
12780 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12781 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012782 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012783 usage = 0;
12784 }
12785 }
12786 else
12787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012788 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12789 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012790 }
12791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012792 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012793 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012794 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012795 ret = -1;
12796 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012797#else
12798 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012799#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012801#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12802 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012804 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12805 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012806 }
12807 else
12808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012809 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12810 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012811 }
12812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012813 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012814 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012815 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012816 ret = -1;
12817 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012818#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012819
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012820 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012821}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012822#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012823
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012824#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12825 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12826int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12827 unsigned char *output,
12828 unsigned char *data, size_t data_len )
12829{
12830 int ret = 0;
12831 mbedtls_md5_context mbedtls_md5;
12832 mbedtls_sha1_context mbedtls_sha1;
12833
12834 mbedtls_md5_init( &mbedtls_md5 );
12835 mbedtls_sha1_init( &mbedtls_sha1 );
12836
12837 /*
12838 * digitally-signed struct {
12839 * opaque md5_hash[16];
12840 * opaque sha_hash[20];
12841 * };
12842 *
12843 * md5_hash
12844 * MD5(ClientHello.random + ServerHello.random
12845 * + ServerParams);
12846 * sha_hash
12847 * SHA(ClientHello.random + ServerHello.random
12848 * + ServerParams);
12849 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012850 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012851 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012852 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012853 goto exit;
12854 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012855 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012856 ssl->handshake->randbytes, 64 ) ) != 0 )
12857 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012858 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012859 goto exit;
12860 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012861 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012862 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012863 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012864 goto exit;
12865 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012866 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012867 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012868 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012869 goto exit;
12870 }
12871
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012872 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012873 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012874 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012875 goto exit;
12876 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012877 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012878 ssl->handshake->randbytes, 64 ) ) != 0 )
12879 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012880 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012881 goto exit;
12882 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012883 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012884 data_len ) ) != 0 )
12885 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012886 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012887 goto exit;
12888 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012889 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012890 output + 16 ) ) != 0 )
12891 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012892 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012893 goto exit;
12894 }
12895
12896exit:
12897 mbedtls_md5_free( &mbedtls_md5 );
12898 mbedtls_sha1_free( &mbedtls_sha1 );
12899
12900 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012901 mbedtls_ssl_pend_fatal_alert( ssl,
12902 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012903
12904 return( ret );
12905
12906}
12907#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12908 MBEDTLS_SSL_PROTO_TLS1_1 */
12909
12910#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12911 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12912int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012913 unsigned char *hash, size_t *hashlen,
12914 unsigned char *data, size_t data_len,
12915 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012916{
12917 int ret = 0;
12918 mbedtls_md_context_t ctx;
Hanno Beckera5cedbc2019-07-17 11:21:02 +010012919 mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012920 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012921
12922 mbedtls_md_init( &ctx );
12923
12924 /*
12925 * digitally-signed struct {
12926 * opaque client_random[32];
12927 * opaque server_random[32];
12928 * ServerDHParams params;
12929 * };
12930 */
12931 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12932 {
12933 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12934 goto exit;
12935 }
12936 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12937 {
12938 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12939 goto exit;
12940 }
12941 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12942 {
12943 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12944 goto exit;
12945 }
12946 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12947 {
12948 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12949 goto exit;
12950 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012951 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012952 {
12953 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12954 goto exit;
12955 }
12956
12957exit:
12958 mbedtls_md_free( &ctx );
12959
12960 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012961 mbedtls_ssl_pend_fatal_alert( ssl,
12962 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012963
12964 return( ret );
12965}
12966#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12967 MBEDTLS_SSL_PROTO_TLS1_2 */
12968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012969#endif /* MBEDTLS_SSL_TLS_C */