blob: b81df29aa2fb1f4a2bd615d44724f3702fc90257 [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é-Gonnard12a3f442019-05-06 12:55:40 +02001255 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001256{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001257 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001258 unsigned char keyblk[256];
1259 unsigned char *key1;
1260 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001261 unsigned char *mac_enc;
1262 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001263 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001264 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001265 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001266 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 const mbedtls_cipher_info_t *cipher_info;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001268 mbedtls_md_handle_t md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001269
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001270#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1271 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1272 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001273 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001274 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001275#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001276
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001277 /*
1278 * Some data just needs copying into the structure
1279 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001280#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1281 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001282 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001283#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001284
1285#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001286 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001287#else
1288 ((void) minor_ver);
1289#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001290
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001291#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Teppo Järvelin91d79382019-10-02 09:09:31 +03001292 mbedtls_platform_memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001293#endif
1294
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001295 /*
1296 * Get various info structures
1297 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001298 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001299 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001300 {
1301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001302 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001303 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1304 }
1305
Hanno Becker473f98f2019-06-26 10:27:32 +01001306 cipher_info = mbedtls_cipher_info_from_type(
1307 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001308 if( cipher_info == NULL )
1309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001311 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001313 }
1314
Hanno Becker473f98f2019-06-26 10:27:32 +01001315 md_info = mbedtls_md_info_from_type(
1316 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001317 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Paul Bakker68884e32013-01-07 18:20:04 +01001318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001320 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001322 }
1323
Hanno Beckera5a2b082019-05-15 14:03:01 +01001324#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001325 /* Copy own and peer's CID if the use of the CID
1326 * extension has been negotiated. */
1327 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1328 {
1329 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001330
Hanno Becker4932f9f2019-05-03 15:23:51 +01001331 transform->in_cid_len = ssl->own_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001332 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1333 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001334 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001335 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001336
1337 transform->out_cid_len = ssl->handshake->peer_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001338 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1339 memcpy( transform->out_cid, ssl->handshake->peer_cid,
Hanno Beckere582d122019-05-15 10:21:55 +01001340 ssl->handshake->peer_cid_len );
1341 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1342 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001343 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001344#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001345
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001347 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001348 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001349 ret = ssl_prf( minor_ver,
1350 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1351 master, 48, "key expansion", randbytes, 64,
1352 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001353 if( ret != 0 )
1354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001356 return( ret );
1357 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001360 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001361 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001362 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001364
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 /*
1366 * Determine the appropriate key, IV and MAC length.
1367 */
Paul Bakker68884e32013-01-07 18:20:04 +01001368
Hanno Beckere7f2df02017-12-27 08:17:40 +00001369 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001370
Hanno Beckerf1229442018-01-03 15:32:31 +00001371#if defined(MBEDTLS_GCM_C) || \
1372 defined(MBEDTLS_CCM_C) || \
1373 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001375 cipher_info->mode == MBEDTLS_MODE_CCM ||
1376 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001377 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001378 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001379 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001380 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1381 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001382
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001383 /* All modes haves 96-bit IVs;
1384 * GCM and CCM has 4 implicit and 8 explicit bytes
1385 * ChachaPoly has all 12 bytes implicit
1386 */
Paul Bakker68884e32013-01-07 18:20:04 +01001387 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001388 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1389 transform->fixed_ivlen = 12;
1390 else
1391 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001392 }
1393 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001394#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1395#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1396 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1397 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001398 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001399 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1401 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001404 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001405 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001407 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001408 mac_key_len = mbedtls_md_get_size( md_info );
1409 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001412 /*
1413 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1414 * (rfc 6066 page 13 or rfc 2104 section 4),
1415 * so we only need to adjust the length here.
1416 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001417 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001420
1421#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1422 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001423 * HMAC implementation which also truncates the key
1424 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001425 mac_key_len = transform->maclen;
1426#endif
1427 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001429
1430 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001431 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001432 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001433 else
1434#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1435 {
1436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1437 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1438 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001439
Hanno Beckera9d5c452019-07-25 16:47:12 +01001440 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001441 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001442 (unsigned) transform->ivlen,
1443 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001444
1445 /*
1446 * Finally setup the cipher contexts, IVs and MAC secrets.
1447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001449 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001450 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001451 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001452 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001453
Paul Bakker68884e32013-01-07 18:20:04 +01001454 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001455 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001456
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001457 /*
1458 * This is not used in TLS v1.1.
1459 */
Paul Bakker48916f92012-09-16 19:57:18 +00001460 iv_copy_len = ( transform->fixed_ivlen ) ?
1461 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001462 mbedtls_platform_memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1463 mbedtls_platform_memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001464 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001465 }
1466 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467#endif /* MBEDTLS_SSL_CLI_C */
1468#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001469 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001470 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001471 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001472 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001473
Hanno Becker81c7b182017-11-09 18:39:33 +00001474 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001475 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001476
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001477 /*
1478 * This is not used in TLS v1.1.
1479 */
Paul Bakker48916f92012-09-16 19:57:18 +00001480 iv_copy_len = ( transform->fixed_ivlen ) ?
1481 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001482 mbedtls_platform_memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1483 mbedtls_platform_memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001484 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001485 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001486 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1490 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001491 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001492
Hanno Becker92231322018-01-03 15:32:51 +00001493#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001495 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001496 {
Hanno Becker92231322018-01-03 15:32:51 +00001497 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1500 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001501 }
1502
Teppo Järvelin91d79382019-10-02 09:09:31 +03001503 mbedtls_platform_memcpy( transform->mac_enc, mac_enc, mac_key_len );
1504 mbedtls_platform_memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001505 }
1506 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1508#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1509 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001510 if( mbedtls_ssl_ver_geq( minor_ver, MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakker68884e32013-01-07 18:20:04 +01001511 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001512 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1513 For AEAD-based ciphersuites, there is nothing to do here. */
1514 if( mac_key_len != 0 )
1515 {
1516 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1517 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1518 }
Paul Bakker68884e32013-01-07 18:20:04 +01001519 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001520 else
1521#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1524 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001525 }
Hanno Becker92231322018-01-03 15:32:51 +00001526#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1529 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001530 {
1531 int ret = 0;
1532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001534
Hanno Beckere7f2df02017-12-27 08:17:40 +00001535 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001536 transform->iv_enc, transform->iv_dec,
1537 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001538 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001539 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1542 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001543 }
1544 }
Hanno Becker92231322018-01-03 15:32:51 +00001545#else
1546 ((void) mac_dec);
1547 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001549
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001550#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1551 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001552 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001553 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001554 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001555 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001556 iv_copy_len );
1557 }
1558#endif
1559
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001560 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001561 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001562 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001563 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001564 return( ret );
1565 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001566
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001567 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001568 cipher_info ) ) != 0 )
1569 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001570 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001571 return( ret );
1572 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001575 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001579 return( ret );
1580 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001583 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001587 return( ret );
1588 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590#if defined(MBEDTLS_CIPHER_MODE_CBC)
1591 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1594 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001597 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001598 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1601 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001604 return( ret );
1605 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001606 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001608
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001609 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001610
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001611 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001613 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001616
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001617 mbedtls_platform_memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1618 mbedtls_platform_memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001619
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001620 if( deflateInit( &transform->ctx_deflate,
1621 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001622 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1625 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001626 }
1627 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001629
Paul Bakker5121ce52009-01-03 21:22:43 +00001630 return( 0 );
1631}
1632
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001633#if defined(MBEDTLS_SSL_PROTO_SSL3)
1634static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1635 unsigned char hash[36],
1636 size_t *hlen )
1637{
1638 mbedtls_md5_context md5;
1639 mbedtls_sha1_context sha1;
1640 unsigned char pad_1[48];
1641 unsigned char pad_2[48];
1642
1643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1644
1645 mbedtls_md5_init( &md5 );
1646 mbedtls_sha1_init( &sha1 );
1647
1648 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1649 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1650
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001651 mbedtls_platform_memset( pad_1, 0x36, 48 );
1652 mbedtls_platform_memset( pad_2, 0x5C, 48 );
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001653
1654 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1655 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1656 mbedtls_md5_finish_ret( &md5, hash );
1657
1658 mbedtls_md5_starts_ret( &md5 );
1659 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1660 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1661 mbedtls_md5_update_ret( &md5, hash, 16 );
1662 mbedtls_md5_finish_ret( &md5, hash );
1663
1664 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1665 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1666 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1667
1668 mbedtls_sha1_starts_ret( &sha1 );
1669 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1670 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1671 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1672 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1673
1674 *hlen = 36;
1675
1676 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1678
1679 mbedtls_md5_free( &md5 );
1680 mbedtls_sha1_free( &sha1 );
1681
1682 return;
1683}
1684#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1685
1686#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1687static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1688 unsigned char hash[36],
1689 size_t *hlen )
1690{
1691 mbedtls_md5_context md5;
1692 mbedtls_sha1_context sha1;
1693
1694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1695
1696 mbedtls_md5_init( &md5 );
1697 mbedtls_sha1_init( &sha1 );
1698
1699 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1700 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1701
1702 mbedtls_md5_finish_ret( &md5, hash );
1703 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1704
1705 *hlen = 36;
1706
1707 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1709
1710 mbedtls_md5_free( &md5 );
1711 mbedtls_sha1_free( &sha1 );
1712
1713 return;
1714}
1715#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1716
1717#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1718#if defined(MBEDTLS_SHA256_C)
1719static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1720 unsigned char hash[32],
1721 size_t *hlen )
1722{
1723 mbedtls_sha256_context sha256;
1724
1725 mbedtls_sha256_init( &sha256 );
1726
1727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1728
1729 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1730 mbedtls_sha256_finish_ret( &sha256, hash );
1731
1732 *hlen = 32;
1733
1734 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1736
1737 mbedtls_sha256_free( &sha256 );
1738
1739 return;
1740}
1741#endif /* MBEDTLS_SHA256_C */
1742
1743#if defined(MBEDTLS_SHA512_C)
1744static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1745 unsigned char hash[48],
1746 size_t *hlen )
1747{
1748 mbedtls_sha512_context sha512;
1749
1750 mbedtls_sha512_init( &sha512 );
1751
1752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1753
1754 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1755 mbedtls_sha512_finish_ret( &sha512, hash );
1756
1757 *hlen = 48;
1758
1759 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1761
1762 mbedtls_sha512_free( &sha512 );
1763
1764 return;
1765}
1766#endif /* MBEDTLS_SHA512_C */
1767#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1768
Hanno Becker2f41b242019-08-15 17:29:43 +01001769int mbedtls_ssl_calc_verify( int minor_ver,
1770 mbedtls_md_type_t hash,
1771 mbedtls_ssl_context const *ssl,
1772 unsigned char *dst,
1773 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001774{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001775#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1776 (void) hash;
1777#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001778
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001779#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001780 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001781 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001782 else
1783#endif
1784#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001785 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker2f41b242019-08-15 17:29:43 +01001786 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001787 else
1788#endif
1789#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1790#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001791 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1792 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001793 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001794 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001795 }
1796 else
1797#endif
1798#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001799 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001800 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001801 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001802 }
1803 else
1804#endif
1805#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1806 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001807 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1808 }
1809
1810 return( 0 );
1811}
1812
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001813/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001814 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001815 *
1816 * Parameters:
1817 * [in/out] handshake
1818 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1819 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001820 * [out] master
1821 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001822 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001823static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001824 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001825 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001826{
1827 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001828
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001829/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1830/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1831/* (void) ssl; */
1832/* #endif */
1833
1834 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1835 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001836
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001837#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02001838 if( handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001839 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001840 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1841 return( 0 );
1842 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001843#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001844
1845 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1846 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001847
1848#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001849 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1850 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001851 {
1852 unsigned char session_hash[48];
1853 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001854
Hanno Becker2f41b242019-08-15 17:29:43 +01001855 mbedtls_ssl_calc_verify(
1856 mbedtls_ssl_get_minor_ver( ssl ),
1857 mbedtls_ssl_suite_get_mac( ciphersuite ),
1858 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001859
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001860 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1861 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001862
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001863 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1864 mbedtls_ssl_suite_get_mac( ciphersuite ),
1865 handshake->premaster, handshake->pmslen,
1866 "extended master secret",
1867 session_hash, hash_len,
1868 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001869 }
1870 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001871#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001872 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001873 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1874 mbedtls_ssl_suite_get_mac( ciphersuite ),
1875 handshake->premaster, handshake->pmslen,
1876 "master secret",
1877 handshake->randbytes, 64,
1878 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001879 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001880 if( ret != 0 )
1881 {
1882 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1883 return( ret );
1884 }
1885
1886 mbedtls_platform_zeroize( handshake->premaster,
1887 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001888
1889 return( 0 );
1890}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001891
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001892int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1893{
Andrzej Kurekfd56f402020-05-25 11:52:05 -04001894 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001895
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001897 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_UNSET;
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001898 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001899 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001900 ssl->session_negotiate->master,
1901 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001902 if( ret != 0 )
1903 {
1904 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1905 return( ret );
1906 }
1907
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001908 /* Swap the client and server random values:
1909 * - MS derivation wanted client+server (RFC 5246 8.1)
1910 * - key derivation wants server+client (RFC 5246 6.3) */
1911 {
1912 unsigned char tmp[64];
Teppo Järvelin91d79382019-10-02 09:09:31 +03001913 mbedtls_platform_memcpy( tmp, ssl->handshake->randbytes, 64 );
1914 mbedtls_platform_memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1915 mbedtls_platform_memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001916 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1917 }
1918
1919 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001920 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001921 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1922 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001923#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001924#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001925 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001926#endif
1927#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001928 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001929#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001930#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001931#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001932 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001933#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001934 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001935 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001936 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1937 ssl );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001938 if( ret == 0 )
1939 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02001940 mbedtls_platform_random_delay();
Jarno Lamsa4031a452019-12-19 08:11:12 +02001941 if( ret == 0 )
1942 {
1943 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_SET;
1944 }
1945 else
1946 {
1947 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
1948 }
1949 }
1950 else
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001951 {
1952 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1953 return( ret );
1954 }
1955
1956 /* We no longer need Server/ClientHello.random values */
1957 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1958 sizeof( ssl->handshake->randbytes ) );
1959
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001960 /* Allocate compression buffer */
1961#if defined(MBEDTLS_ZLIB_SUPPORT)
jiblime92af9a92019-12-18 21:40:01 -08001962 if( ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001963 ssl->compress_buf == NULL )
1964 {
1965 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1966 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1967 if( ssl->compress_buf == NULL )
1968 {
1969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001970 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001971 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1972 }
1973 }
1974#endif
1975
Paul Bakker5121ce52009-01-03 21:22:43 +00001976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1977
1978 return( 0 );
1979}
1980
Hanno Becker09d23642019-07-22 17:18:18 +01001981int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1982{
Andrzej Kurekfd56f402020-05-25 11:52:05 -04001983 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Hanno Becker09d23642019-07-22 17:18:18 +01001984
1985 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1986 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Beckera3c2c172019-07-23 16:51:57 +01001987#if defined(MBEDTLS_USE_TINYCRYPT)
1988 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001989 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Hanno Beckera3c2c172019-07-23 16:51:57 +01001990 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001991 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1992 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1993 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1994 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1995 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001996 {
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01001997 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1998 ssl->handshake->ecdh_privkey,
1999 ssl->handshake->premaster );
2000 if( ret == UECC_FAULT_DETECTED )
2001 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2002 if( ret != UECC_SUCCESS )
Hanno Beckera3c2c172019-07-23 16:51:57 +01002003 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002004 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Hanno Beckera3c2c172019-07-23 16:51:57 +01002005
2006 ssl->handshake->pmslen = NUM_ECC_BYTES;
2007 }
2008 else
2009#endif
Hanno Becker09d23642019-07-22 17:18:18 +01002010#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2011 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2012 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
2013 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002014 ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002015 ssl->handshake->premaster,
2016 MBEDTLS_PREMASTER_SIZE,
2017 &ssl->handshake->pmslen,
2018 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002019 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2020 if( ret == 0 )
2021 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002022 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002023 if( ret == 0 )
2024 {
2025 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2026 }
2027 else
2028 {
2029 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2030 return( ret );
2031 }
2032 }
2033 else
Hanno Becker09d23642019-07-22 17:18:18 +01002034 {
2035 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2036 return( ret );
2037 }
2038
2039 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2040 }
2041 else
2042#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01002043#if defined(MBEDTLS_ECDH_C) && \
2044 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2045 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2046 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2047 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002048 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2049 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2050 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2051 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2052 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2053 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2054 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2055 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2056 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002057 ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002058 &ssl->handshake->pmslen,
2059 ssl->handshake->premaster,
2060 MBEDTLS_MPI_MAX_SIZE,
2061 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002062 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2063 if( ret == 0 )
2064 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002065 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002066 if( ret == 0 )
2067 {
2068 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2069 }
2070 else
2071 {
2072 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002073 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002074 }
2075 }
2076 else
Hanno Becker09d23642019-07-22 17:18:18 +01002077 {
2078 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2079 return( ret );
2080 }
2081
2082 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2083 }
2084 else
2085#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2086 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2087 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2088 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2089#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2090 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2091 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002092 ret = mbedtls_ssl_psk_derive_premaster( ssl,
2093 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) );
2094 if( ret == 0 )
2095 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002096 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002097 if( ret == 0 )
2098 {
2099 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2100 }
2101 else
2102 {
2103 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002104 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002105 }
2106 }
2107 else
Hanno Becker09d23642019-07-22 17:18:18 +01002108 {
2109 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2110 return( ret );
2111 }
2112 }
2113 else
2114#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2115#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2116 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2117 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2118 {
2119 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2120 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2121 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002122 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002123 if( ret == 0 )
2124 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002125 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002126 if( ret == 0 )
2127 {
2128 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2129 }
2130 else
2131 {
2132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002133 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002134 }
2135 }
2136 else
Hanno Becker09d23642019-07-22 17:18:18 +01002137 {
2138 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2139 return( ret );
2140 }
2141 }
2142 else
2143#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2144#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2145 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2146 == MBEDTLS_KEY_EXCHANGE_RSA )
2147 {
2148 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002149 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002150 * ssl_rsa_generate_partial_pms(). Only the
2151 * PMS length needs to be set. */
2152 ssl->handshake->pmslen = 48;
2153 }
2154 else
2155#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2156 {
2157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2158 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2159 }
2160
2161 return( 0 );
2162}
2163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2165int 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 +02002166{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002167 unsigned char *p = ssl->handshake->premaster;
2168 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002169 const unsigned char *psk = ssl->conf->psk;
2170 size_t psk_len = ssl->conf->psk_len;
2171
2172 /* If the psk callback was called, use its result */
2173 if( ssl->handshake->psk != NULL )
2174 {
2175 psk = ssl->handshake->psk;
2176 psk_len = ssl->handshake->psk_len;
2177 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002178
2179 /*
2180 * PMS = struct {
2181 * opaque other_secret<0..2^16-1>;
2182 * opaque psk<0..2^16-1>;
2183 * };
2184 * with "other_secret" depending on the particular key exchange
2185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2187 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002188 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002189 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002191
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002192 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002193
2194 if( end < p || (size_t)( end - p ) < psk_len )
2195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2196
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002197 mbedtls_platform_memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002198 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002199 }
2200 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2202#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2203 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002204 {
2205 /*
2206 * other_secret already set by the ClientKeyExchange message,
2207 * and is 48 bytes long
2208 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002209 if( end - p < 2 )
2210 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2211
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002212 *p++ = 0;
2213 *p++ = 48;
2214 p += 48;
2215 }
2216 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2218#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2219 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002220 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002221 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002222 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002223
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002224 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002226 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002227 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002228 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002231 return( ret );
2232 }
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002233 p = mbedtls_platform_put_uint16_be( p, len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002234 p += len;
2235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002237 }
2238 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2240#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2241 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002242 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002243 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002244 size_t zlen;
2245
Hanno Becker982da7e2019-09-02 09:47:39 +01002246#if defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01002247 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2248 ssl->handshake->ecdh_privkey,
2249 p + 2 );
2250 if( ret == UECC_FAULT_DETECTED )
2251 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2252 if( ret != UECC_SUCCESS )
Hanno Becker982da7e2019-09-02 09:47:39 +01002253 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Hanno Becker982da7e2019-09-02 09:47:39 +01002254
2255 zlen = NUM_ECC_BYTES;
2256#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002258 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002259 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002260 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002263 return( ret );
2264 }
2265
Hanno Becker982da7e2019-09-02 09:47:39 +01002266 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2267 MBEDTLS_DEBUG_ECDH_Z );
2268#endif /* MBEDTLS_USE_TINYCRYPT */
2269
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002270 p = mbedtls_platform_put_uint16_be( p, zlen );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002271 p += zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002272 }
2273 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2277 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002278 }
2279
2280 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002281 if( end - p < 2 )
2282 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002283
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002284 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002285
2286 if( end < p || (size_t)( end - p ) < psk_len )
2287 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2288
Teppo Järvelin91d79382019-10-02 09:09:31 +03002289 mbedtls_platform_memcpy( p, psk, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002290 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002291
2292 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2293
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002294 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2295
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002296 return( 0 );
2297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002301/*
2302 * SSLv3.0 MAC functions
2303 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002304#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002305static void ssl_mac( mbedtls_md_context_t *md_ctx,
2306 const unsigned char *secret,
2307 const unsigned char *buf, size_t len,
2308 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002309 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002310{
2311 unsigned char header[11];
2312 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002313 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2315 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002316
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002317 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002319 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002320 else
Paul Bakker68884e32013-01-07 18:20:04 +01002321 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002322
Teppo Järvelin91d79382019-10-02 09:09:31 +03002323 mbedtls_platform_memcpy( header, ctr, 8 );
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03002324 header[8] = (unsigned char) type;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002325 (void)mbedtls_platform_put_uint16_be( &header[9], len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002326
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002327 mbedtls_platform_memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 mbedtls_md_starts( md_ctx );
2329 mbedtls_md_update( md_ctx, secret, md_size );
2330 mbedtls_md_update( md_ctx, padding, padlen );
2331 mbedtls_md_update( md_ctx, header, 11 );
2332 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002333 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002334
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002335 mbedtls_platform_memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 mbedtls_md_starts( md_ctx );
2337 mbedtls_md_update( md_ctx, secret, md_size );
2338 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002339 mbedtls_md_update( md_ctx, out, md_size );
2340 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002341}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002343
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002344/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002345 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002346#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002347 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2348 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2349 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2350/* This function makes sure every byte in the memory region is accessed
2351 * (in ascending addresses order) */
2352static void ssl_read_memory( unsigned char *p, size_t len )
2353{
2354 unsigned char acc = 0;
2355 volatile unsigned char force;
2356
2357 for( ; len != 0; p++, len-- )
2358 acc ^= *p;
2359
2360 force = acc;
2361 (void) force;
2362}
2363#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2364
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002365/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002366 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002367 */
Hanno Becker3307b532017-12-27 21:37:21 +00002368
Hanno Beckera5a2b082019-05-15 14:03:01 +01002369#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002370/* This functions transforms a DTLS plaintext fragment and a record content
2371 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002372 *
2373 * struct {
2374 * opaque content[DTLSPlaintext.length];
2375 * ContentType real_type;
2376 * uint8 zeros[length_of_padding];
2377 * } DTLSInnerPlaintext;
2378 *
2379 * Input:
2380 * - `content`: The beginning of the buffer holding the
2381 * plaintext to be wrapped.
2382 * - `*content_size`: The length of the plaintext in Bytes.
2383 * - `max_len`: The number of Bytes available starting from
2384 * `content`. This must be `>= *content_size`.
2385 * - `rec_type`: The desired record content type.
2386 *
2387 * Output:
2388 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2389 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2390 *
2391 * Returns:
2392 * - `0` on success.
2393 * - A negative error code if `max_len` didn't offer enough space
2394 * for the expansion.
2395 */
2396static int ssl_cid_build_inner_plaintext( unsigned char *content,
2397 size_t *content_size,
2398 size_t remaining,
2399 uint8_t rec_type )
2400{
2401 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002402 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2403 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2404 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002405
2406 /* Write real content type */
2407 if( remaining == 0 )
2408 return( -1 );
2409 content[ len ] = rec_type;
2410 len++;
2411 remaining--;
2412
2413 if( remaining < pad )
2414 return( -1 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002415 mbedtls_platform_memset( content + len, 0, pad );
Hanno Becker92c930f2019-04-29 17:31:37 +01002416 len += pad;
2417 remaining -= pad;
2418
2419 *content_size = len;
2420 return( 0 );
2421}
2422
Hanno Becker7dc25772019-05-20 15:08:01 +01002423/* This function parses a DTLSInnerPlaintext structure.
2424 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002425static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2426 size_t *content_size,
2427 uint8_t *rec_type )
2428{
2429 size_t remaining = *content_size;
2430
2431 /* Determine length of padding by skipping zeroes from the back. */
2432 do
2433 {
2434 if( remaining == 0 )
2435 return( -1 );
2436 remaining--;
2437 } while( content[ remaining ] == 0 );
2438
2439 *content_size = remaining;
2440 *rec_type = content[ remaining ];
2441
2442 return( 0 );
2443}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002444#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002445
Hanno Becker99abf512019-05-20 14:50:53 +01002446/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002447 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002448static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002449 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002450 mbedtls_record *rec )
2451{
Hanno Becker99abf512019-05-20 14:50:53 +01002452 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002453 *
2454 * additional_data = seq_num + TLSCompressed.type +
2455 * TLSCompressed.version + TLSCompressed.length;
2456 *
Hanno Becker99abf512019-05-20 14:50:53 +01002457 * For the CID extension, this is extended as follows
2458 * (quoting draft-ietf-tls-dtls-connection-id-05,
2459 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002460 *
2461 * additional_data = seq_num + DTLSPlaintext.type +
2462 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002463 * cid +
2464 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002465 * length_of_DTLSInnerPlaintext;
2466 */
2467
Teppo Järvelin91d79382019-10-02 09:09:31 +03002468 mbedtls_platform_memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002469 add_data[8] = rec->type;
Teppo Järvelin91d79382019-10-02 09:09:31 +03002470 mbedtls_platform_memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002471
Hanno Beckera5a2b082019-05-15 14:03:01 +01002472#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002473 if( rec->cid_len != 0 )
2474 {
Teppo Järvelin91d79382019-10-02 09:09:31 +03002475 mbedtls_platform_memcpy( add_data + 11, rec->cid, rec->cid_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002476 add_data[11 + rec->cid_len + 0] = rec->cid_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002477 (void)mbedtls_platform_put_uint16_be( &add_data[11 + rec->cid_len + 1],
2478 rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002479 *add_data_len = 13 + 1 + rec->cid_len;
2480 }
2481 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002482#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002483 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002484 (void)mbedtls_platform_put_uint16_be( &add_data[11], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002485 *add_data_len = 13;
2486 }
Hanno Becker3307b532017-12-27 21:37:21 +00002487}
2488
Hanno Becker611a83b2018-01-03 14:27:32 +00002489int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2490 mbedtls_ssl_transform *transform,
2491 mbedtls_record *rec,
2492 int (*f_rng)(void *, unsigned char *, size_t),
2493 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002494{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002496 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002497 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002498 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002499 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002500 size_t post_avail;
2501
2502 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002503#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002504 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002505 ((void) ssl);
2506#endif
2507
2508 /* The PRNG is used for dynamic IV generation that's used
2509 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2510#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2511 ( defined(MBEDTLS_AES_C) || \
2512 defined(MBEDTLS_ARIA_C) || \
2513 defined(MBEDTLS_CAMELLIA_C) ) && \
2514 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2515 ((void) f_rng);
2516 ((void) p_rng);
2517#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002520
Hanno Becker3307b532017-12-27 21:37:21 +00002521 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002522 {
Hanno Becker3307b532017-12-27 21:37:21 +00002523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2524 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2525 }
Hanno Becker505089d2019-05-01 09:45:57 +01002526 if( rec == NULL
2527 || rec->buf == NULL
2528 || rec->buf_len < rec->data_offset
2529 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002530#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002531 || rec->cid_len != 0
2532#endif
2533 )
Hanno Becker3307b532017-12-27 21:37:21 +00002534 {
2535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002537 }
2538
Hanno Becker3307b532017-12-27 21:37:21 +00002539 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002540 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002542 data, rec->data_len );
2543
2544 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2545
2546 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2547 {
2548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2549 (unsigned) rec->data_len,
2550 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2551 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2552 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002553
Hanno Beckera5a2b082019-05-15 14:03:01 +01002554#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002555 /*
2556 * Add CID information
2557 */
2558 rec->cid_len = transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03002559 /* Not using more secure mbedtls_platform_memcpy as cid is public */
2560 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
Hanno Beckere83efe62019-04-29 13:52:53 +01002561 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002562
2563 if( rec->cid_len != 0 )
2564 {
2565 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002566 * Wrap plaintext into DTLSInnerPlaintext structure.
2567 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002568 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002569 * Note that this changes `rec->data_len`, and hence
2570 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002571 */
2572 if( ssl_cid_build_inner_plaintext( data,
2573 &rec->data_len,
2574 post_avail,
2575 rec->type ) != 0 )
2576 {
2577 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2578 }
2579
2580 rec->type = MBEDTLS_SSL_MSG_CID;
2581 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002582#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002583
Hanno Becker92c930f2019-04-29 17:31:37 +01002584 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2585
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002587 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002589#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590 if( mode == MBEDTLS_MODE_STREAM ||
2591 ( mode == MBEDTLS_MODE_CBC
2592#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002593 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002594#endif
2595 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002596 {
Hanno Becker3307b532017-12-27 21:37:21 +00002597 if( post_avail < transform->maclen )
2598 {
2599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2600 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2601 }
2602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002604 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2605 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002606 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002607 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002608 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2609 data, rec->data_len, rec->ctr, rec->type, mac );
Teppo Järvelin91d79382019-10-02 09:09:31 +03002610 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002611 }
2612 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002613#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2615 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002616 if( mbedtls_ssl_ver_geq(
2617 mbedtls_ssl_transform_get_minor_ver( transform ),
2618 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002619 {
Hanno Becker992b6872017-11-09 18:57:39 +00002620 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2621
Hanno Beckere83efe62019-04-29 13:52:53 +01002622 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002623
Hanno Becker3307b532017-12-27 21:37:21 +00002624 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002625 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002626 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2627 data, rec->data_len );
2628 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2629 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2630
Teppo Järvelin91d79382019-10-02 09:09:31 +03002631 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002632 }
2633 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002634#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2637 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002638 }
2639
Hanno Becker3307b532017-12-27 21:37:21 +00002640 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2641 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002642
Hanno Becker3307b532017-12-27 21:37:21 +00002643 rec->data_len += transform->maclen;
2644 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002645 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002646 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002647#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002649 /*
2650 * Encrypt
2651 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2653 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002655 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002656 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002658 "including %d bytes of padding",
2659 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
Hanno Becker3307b532017-12-27 21:37:21 +00002661 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2662 transform->iv_enc, transform->ivlen,
2663 data, rec->data_len,
2664 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002667 return( ret );
2668 }
2669
Hanno Becker3307b532017-12-27 21:37:21 +00002670 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2673 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002674 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002675 }
Paul Bakker68884e32013-01-07 18:20:04 +01002676 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002678
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002679#if defined(MBEDTLS_GCM_C) || \
2680 defined(MBEDTLS_CCM_C) || \
2681 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002683 mode == MBEDTLS_MODE_CCM ||
2684 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002685 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002686 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002687 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002688 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002689
Hanno Becker3307b532017-12-27 21:37:21 +00002690 /* Check that there's space for both the authentication tag
2691 * and the explicit IV before and after the record content. */
2692 if( post_avail < transform->taglen ||
2693 rec->data_offset < explicit_iv_len )
2694 {
2695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2696 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2697 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002698
Paul Bakker68884e32013-01-07 18:20:04 +01002699 /*
2700 * Generate IV
2701 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002702 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2703 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002704 /* GCM and CCM: fixed || explicit (=seqnum) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002705 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2706 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, rec->ctr,
Hanno Becker3307b532017-12-27 21:37:21 +00002707 explicit_iv_len );
2708 /* Prefix record content with explicit IV. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002709 mbedtls_platform_memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002710 }
2711 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2712 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002713 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002714 unsigned char i;
2715
Teppo Järvelin91d79382019-10-02 09:09:31 +03002716 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002717
2718 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002719 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002720 }
2721 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002722 {
2723 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2725 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002726 }
2727
Hanno Beckere83efe62019-04-29 13:52:53 +01002728 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002729
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002730 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2731 iv, transform->ivlen );
2732 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002733 data - explicit_iv_len, explicit_iv_len );
2734 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002735 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002737 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002738 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002739
Paul Bakker68884e32013-01-07 18:20:04 +01002740 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002741 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002742 */
Hanno Becker3307b532017-12-27 21:37:21 +00002743
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002744 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002745 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002746 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002747 data, rec->data_len, /* source */
2748 data, &rec->data_len, /* destination */
2749 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002752 return( ret );
2753 }
2754
Hanno Becker3307b532017-12-27 21:37:21 +00002755 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2756 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002757
Hanno Becker3307b532017-12-27 21:37:21 +00002758 rec->data_len += transform->taglen + explicit_iv_len;
2759 rec->data_offset -= explicit_iv_len;
2760 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002761 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002762 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002763 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2765#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002766 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002768 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002769 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002770 size_t padlen, i;
2771 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002772
Hanno Becker3307b532017-12-27 21:37:21 +00002773 /* Currently we're always using minimal padding
2774 * (up to 255 bytes would be allowed). */
2775 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2776 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002777 padlen = 0;
2778
Hanno Becker3307b532017-12-27 21:37:21 +00002779 /* Check there's enough space in the buffer for the padding. */
2780 if( post_avail < padlen + 1 )
2781 {
2782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2783 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2784 }
2785
Paul Bakker5121ce52009-01-03 21:22:43 +00002786 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002787 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002788
Hanno Becker3307b532017-12-27 21:37:21 +00002789 rec->data_len += padlen + 1;
2790 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002793 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002794 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2795 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002796 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002797 if( mbedtls_ssl_ver_geq(
2798 mbedtls_ssl_transform_get_minor_ver( transform ),
2799 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002800 {
Hanno Becker3307b532017-12-27 21:37:21 +00002801 if( f_rng == NULL )
2802 {
2803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2804 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2805 }
2806
2807 if( rec->data_offset < transform->ivlen )
2808 {
2809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2810 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2811 }
2812
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002813 /*
2814 * Generate IV
2815 */
Hanno Becker3307b532017-12-27 21:37:21 +00002816 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002817 if( ret != 0 )
2818 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002819
Teppo Järvelin91d79382019-10-02 09:09:31 +03002820 mbedtls_platform_memcpy( data - transform->ivlen, transform->iv_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002821 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002822
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002823 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002827 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002828 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002829 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002830
Hanno Becker3307b532017-12-27 21:37:21 +00002831 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2832 transform->iv_enc,
2833 transform->ivlen,
2834 data, rec->data_len,
2835 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002838 return( ret );
2839 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002840
Hanno Becker3307b532017-12-27 21:37:21 +00002841 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2844 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002845 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002848 if( mbedtls_ssl_ver_lt(
2849 mbedtls_ssl_transform_get_minor_ver( transform ),
2850 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02002851 {
2852 /*
2853 * Save IV in SSL3 and TLS1
2854 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002855 mbedtls_platform_memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
Hanno Becker3307b532017-12-27 21:37:21 +00002856 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002857 }
Hanno Becker3307b532017-12-27 21:37:21 +00002858 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002859#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002860 {
2861 data -= transform->ivlen;
2862 rec->data_offset -= transform->ivlen;
2863 rec->data_len += transform->ivlen;
2864 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002867 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002868 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002869 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2870
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002871 /*
2872 * MAC(MAC_write_key, seq_num +
2873 * TLSCipherText.type +
2874 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002875 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002876 * IV + // except for TLS 1.0
2877 * ENC(content + padding + padding_length));
2878 */
Hanno Becker3307b532017-12-27 21:37:21 +00002879
2880 if( post_avail < transform->maclen)
2881 {
2882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2883 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2884 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002885
Hanno Beckere83efe62019-04-29 13:52:53 +01002886 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002889 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002890 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002891
Hanno Becker3307b532017-12-27 21:37:21 +00002892 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002893 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002894 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2895 data, rec->data_len );
2896 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2897 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002898
Teppo Järvelin91d79382019-10-02 09:09:31 +03002899 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002900
Hanno Becker3307b532017-12-27 21:37:21 +00002901 rec->data_len += transform->maclen;
2902 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002903 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002904 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002906 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002907 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002909 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2912 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002913 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002914
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002915 /* Make extra sure authentication was performed, exactly once */
2916 if( auth_done != 1 )
2917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2919 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002920 }
2921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002923
2924 return( 0 );
2925}
2926
Hanno Becker40478be2019-07-12 08:23:59 +01002927int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002928 mbedtls_ssl_transform *transform,
2929 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002930{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002931 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002933 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002934#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002935 size_t padlen = 0, correct = 1;
2936#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002937 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002938 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002939 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002940
Hanno Becker611a83b2018-01-03 14:27:32 +00002941#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002942 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002943 ((void) ssl);
2944#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002947 if( rec == NULL ||
2948 rec->buf == NULL ||
2949 rec->buf_len < rec->data_offset ||
2950 rec->buf_len - rec->data_offset < rec->data_len )
2951 {
2952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002954 }
2955
Hanno Becker4c6876b2017-12-27 21:28:58 +00002956 data = rec->buf + rec->data_offset;
2957 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002958
Hanno Beckera5a2b082019-05-15 14:03:01 +01002959#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002960 /*
2961 * Match record's CID with incoming CID.
2962 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002963 if( rec->cid_len != transform->in_cid_len ||
Teppo Järvelin0efac532019-10-04 13:21:08 +03002964 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) // use regular memcmp as CID is public
Hanno Beckerabd7c892019-05-08 13:02:22 +01002965 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002966 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002967 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002968#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2971 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002972 {
2973 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002974 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2975 transform->iv_dec,
2976 transform->ivlen,
2977 data, rec->data_len,
2978 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002981 return( ret );
2982 }
2983
Hanno Becker4c6876b2017-12-27 21:28:58 +00002984 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2987 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002988 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002989 }
Paul Bakker68884e32013-01-07 18:20:04 +01002990 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002992#if defined(MBEDTLS_GCM_C) || \
2993 defined(MBEDTLS_CCM_C) || \
2994 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002996 mode == MBEDTLS_MODE_CCM ||
2997 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002998 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002999 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003000 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003001
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003002 /*
3003 * Prepare IV from explicit and implicit data.
3004 */
3005
3006 /* Check that there's enough space for the explicit IV
3007 * (at the beginning of the record) and the MAC (at the
3008 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003009 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003012 "+ taglen (%d)", rec->data_len,
3013 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003015 }
Paul Bakker68884e32013-01-07 18:20:04 +01003016
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003017#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003018 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
3019 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003020 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01003021
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003022 /* Fixed */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003023 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003024 /* Explicit */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003025 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003026 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003027 else
3028#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3029#if defined(MBEDTLS_CHACHAPOLY_C)
3030 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003031 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02003032 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003033 unsigned char i;
3034
Teppo Järvelin91d79382019-10-02 09:09:31 +03003035 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003036
3037 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003038 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003039 }
3040 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003041#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003042 {
3043 /* Reminder if we ever add an AEAD mode with a different size */
3044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3045 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3046 }
3047
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003048 /* Group changes to data, data_len, and add_data, because
3049 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003050 data += explicit_iv_len;
3051 rec->data_offset += explicit_iv_len;
3052 rec->data_len -= explicit_iv_len + transform->taglen;
3053
Hanno Beckere83efe62019-04-29 13:52:53 +01003054 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003055 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01003056 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003057
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003058 /* Because of the check above, we know that there are
3059 * explicit_iv_len Bytes preceeding data, and taglen
3060 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01003061 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003062 * mbedtls_cipher_auth_decrypt() below. */
3063
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003064 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003065 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00003066 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01003067
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003068 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003069 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003070 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003071 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
3072 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01003073 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003074 data, rec->data_len,
3075 data, &olen,
3076 data + rec->data_len,
3077 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
3082 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003083
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003084 return( ret );
3085 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003086 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003087
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003088 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003089 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3092 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003093 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003094 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003095 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3097#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003098 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003100 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003101 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003102
Paul Bakker5121ce52009-01-03 21:22:43 +00003103 /*
Paul Bakker45829992013-01-03 14:52:21 +01003104 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003107 if( mbedtls_ssl_ver_geq(
3108 mbedtls_ssl_transform_get_minor_ver( transform ),
3109 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003110 {
3111 /* The ciphertext is prefixed with the CBC IV. */
3112 minlen += transform->ivlen;
3113 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003114#endif
Paul Bakker45829992013-01-03 14:52:21 +01003115
Hanno Becker4c6876b2017-12-27 21:28:58 +00003116 /* Size considerations:
3117 *
3118 * - The CBC cipher text must not be empty and hence
3119 * at least of size transform->ivlen.
3120 *
3121 * Together with the potential IV-prefix, this explains
3122 * the first of the two checks below.
3123 *
3124 * - The record must contain a MAC, either in plain or
3125 * encrypted, depending on whether Encrypt-then-MAC
3126 * is used or not.
3127 * - If it is, the message contains the IV-prefix,
3128 * the CBC ciphertext, and the MAC.
3129 * - If it is not, the padded plaintext, and hence
3130 * the CBC ciphertext, has at least length maclen + 1
3131 * because there is at least the padding length byte.
3132 *
3133 * As the CBC ciphertext is not empty, both cases give the
3134 * lower bound minlen + maclen + 1 on the record size, which
3135 * we test for in the second check below.
3136 */
3137 if( rec->data_len < minlen + transform->ivlen ||
3138 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003141 "+ 1 ) ( + expl IV )", rec->data_len,
3142 transform->ivlen,
3143 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003145 }
3146
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003147 /*
3148 * Authenticate before decrypt if enabled
3149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003151 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003152 {
Hanno Becker992b6872017-11-09 18:57:39 +00003153 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003156
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003157 /* Update data_len in tandem with add_data.
3158 *
3159 * The subtraction is safe because of the previous check
3160 * data_len >= minlen + maclen + 1.
3161 *
3162 * Afterwards, we know that data + data_len is followed by at
3163 * least maclen Bytes, which justifies the call to
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003164 * mbedtls_platform_memcmp() below.
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003165 *
3166 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003167 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003168 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003169
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003170 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003171 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3172 add_data_len );
3173 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3174 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003175 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3176 data, rec->data_len );
3177 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3178 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003179
Hanno Becker4c6876b2017-12-27 21:28:58 +00003180 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3181 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003182 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003183 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003184
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003185 /* Compare expected MAC with MAC at the end of the record. */
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003186 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003187 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003191 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003192 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003193 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003195
3196 /*
3197 * Check length sanity
3198 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003199
3200 /* We know from above that data_len > minlen >= 0,
3201 * so the following check in particular implies that
3202 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003203 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003206 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003208 }
3209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003211 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003212 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003213 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003214 if( mbedtls_ssl_ver_geq(
3215 mbedtls_ssl_transform_get_minor_ver( transform ),
3216 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003217 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003218 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003219 mbedtls_platform_memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003220
Hanno Becker4c6876b2017-12-27 21:28:58 +00003221 data += transform->ivlen;
3222 rec->data_offset += transform->ivlen;
3223 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003224 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003226
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003227 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3228
Hanno Becker4c6876b2017-12-27 21:28:58 +00003229 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3230 transform->iv_dec, transform->ivlen,
3231 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003234 return( ret );
3235 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003236
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003237 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003238 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3241 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003242 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003245 if( mbedtls_ssl_ver_lt(
3246 mbedtls_ssl_transform_get_minor_ver( transform ),
3247 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02003248 {
3249 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003250 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3251 * records is equivalent to CBC decryption of the concatenation
3252 * of the records; in other words, IVs are maintained across
3253 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003254 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003255 mbedtls_platform_memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003256 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003257 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003258#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003259
Hanno Becker4c6876b2017-12-27 21:28:58 +00003260 /* Safe since data_len >= minlen + maclen + 1, so after having
3261 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003262 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3263 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003264 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003265
Hanno Becker4c6876b2017-12-27 21:28:58 +00003266 if( auth_done == 1 )
3267 {
3268 correct *= ( rec->data_len >= padlen + 1 );
3269 padlen *= ( rec->data_len >= padlen + 1 );
3270 }
3271 else
Paul Bakker45829992013-01-03 14:52:21 +01003272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003273#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003274 if( rec->data_len < transform->maclen + padlen + 1 )
3275 {
3276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3277 rec->data_len,
3278 transform->maclen,
3279 padlen + 1 ) );
3280 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003281#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003282
3283 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3284 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003285 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003286
Hanno Becker4c6876b2017-12-27 21:28:58 +00003287 padlen++;
3288
3289 /* Regardless of the validity of the padding,
3290 * we have data_len >= padlen here. */
3291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003293 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3294 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003295 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003296 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003298#if defined(MBEDTLS_SSL_DEBUG_ALL)
3299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003300 "should be no more than %d",
3301 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003302#endif
Paul Bakker45829992013-01-03 14:52:21 +01003303 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003304 }
3305 }
3306 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003307#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3308#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3309 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003310 if( mbedtls_ssl_ver_gt(
3311 mbedtls_ssl_transform_get_minor_ver( transform ),
3312 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003313 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003314 /* The padding check involves a series of up to 256
3315 * consecutive memory reads at the end of the record
3316 * plaintext buffer. In order to hide the length and
3317 * validity of the padding, always perform exactly
3318 * `min(256,plaintext_len)` reads (but take into account
3319 * only the last `padlen` bytes for the padding check). */
3320 size_t pad_count = 0;
3321 size_t real_count = 0;
3322 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003323
Hanno Becker4c6876b2017-12-27 21:28:58 +00003324 /* Index of first padding byte; it has been ensured above
3325 * that the subtraction is safe. */
3326 size_t const padding_idx = rec->data_len - padlen;
3327 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3328 size_t const start_idx = rec->data_len - num_checks;
3329 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003330
Hanno Becker4c6876b2017-12-27 21:28:58 +00003331 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003332 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003333 real_count |= ( idx >= padding_idx );
3334 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003335 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003336 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003339 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003341#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003342 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003343 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003344 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3346 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3349 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003350 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003351
Hanno Becker4c6876b2017-12-27 21:28:58 +00003352 /* If the padding was found to be invalid, padlen == 0
3353 * and the subtraction is safe. If the padding was found valid,
3354 * padlen hasn't been changed and the previous assertion
3355 * data_len >= padlen still holds. */
3356 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003357 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003358 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003360 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3363 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003364 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003365
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003366#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003368 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003369#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003370
3371 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003372 * Authenticate if not done yet.
3373 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003374 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003375#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003376 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003377 {
Hanno Becker992b6872017-11-09 18:57:39 +00003378 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003379
Hanno Becker4c6876b2017-12-27 21:28:58 +00003380 /* If the initial value of padlen was such that
3381 * data_len < maclen + padlen + 1, then padlen
3382 * got reset to 1, and the initial check
3383 * data_len >= minlen + maclen + 1
3384 * guarantees that at this point we still
3385 * have at least data_len >= maclen.
3386 *
3387 * If the initial value of padlen was such that
3388 * data_len >= maclen + padlen + 1, then we have
3389 * subtracted either padlen + 1 (if the padding was correct)
3390 * or 0 (if the padding was incorrect) since then,
3391 * hence data_len >= maclen in any case.
3392 */
3393 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003394 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003397 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3398 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003399 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003400 ssl_mac( &transform->md_ctx_dec,
3401 transform->mac_dec,
3402 data, rec->data_len,
3403 rec->ctr, rec->type,
3404 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003405 }
3406 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3408#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3409 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003410 if( mbedtls_ssl_ver_gt(
3411 mbedtls_ssl_transform_get_minor_ver( transform ),
3412 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003413 {
3414 /*
3415 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003416 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003417 *
3418 * Known timing attacks:
3419 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3420 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003421 * To compensate for different timings for the MAC calculation
3422 * depending on how much padding was removed (which is determined
3423 * by padlen), process extra_run more blocks through the hash
3424 * function.
3425 *
3426 * The formula in the paper is
3427 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3428 * where L1 is the size of the header plus the decrypted message
3429 * plus CBC padding and L2 is the size of the header plus the
3430 * decrypted message. This is for an underlying hash function
3431 * with 64-byte blocks.
3432 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3433 * correctly. We round down instead of up, so -56 is the correct
3434 * value for our calculations instead of -55.
3435 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003436 * Repeat the formula rather than defining a block_size variable.
3437 * This avoids requiring division by a variable at runtime
3438 * (which would be marginally less efficient and would require
3439 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003440 */
3441 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003442 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003443
3444 /*
3445 * The next two sizes are the minimum and maximum values of
3446 * in_msglen over all padlen values.
3447 *
3448 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003449 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003450 *
3451 * Note that max_len + maclen is never more than the buffer
3452 * length, as we previously did in_msglen -= maclen too.
3453 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003454 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003455 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3456
Hanno Becker4c6876b2017-12-27 21:28:58 +00003457 memset( tmp, 0, sizeof( tmp ) );
3458
Hanno Beckera5cedbc2019-07-17 11:21:02 +01003459 switch( mbedtls_md_get_type(
3460 mbedtls_md_get_handle( &transform->md_ctx_dec ) ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003461 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003462#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3463 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003464 case MBEDTLS_MD_MD5:
3465 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003466 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003467 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003468 extra_run =
3469 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3470 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003471 break;
3472#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003473#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003474 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003475 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003476 extra_run =
3477 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3478 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003479 break;
3480#endif
3481 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003483 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3484 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003485
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003486 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003487
Hanno Beckere83efe62019-04-29 13:52:53 +01003488 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3489 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003490 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3491 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003492 /* Make sure we access everything even when padlen > 0. This
3493 * makes the synchronisation requirements for just-in-time
3494 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003495 ssl_read_memory( data + rec->data_len, padlen );
3496 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003497
3498 /* Call mbedtls_md_process at least once due to cache attacks
3499 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003500 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003501 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003502
Hanno Becker4c6876b2017-12-27 21:28:58 +00003503 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003504
3505 /* Make sure we access all the memory that could contain the MAC,
3506 * before we check it in the next code block. This makes the
3507 * synchronisation requirements for just-in-time Prime+Probe
3508 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003509 ssl_read_memory( data + min_len,
3510 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003511 }
3512 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3514 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3517 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003518 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003519
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003520#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003521 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3522 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003523#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003524
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003525 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003526 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528#if defined(MBEDTLS_SSL_DEBUG_ALL)
3529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003530#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003531 correct = 0;
3532 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003533 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003534 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003535
3536 /*
3537 * Finally check the correct flag
3538 */
3539 if( correct == 0 )
3540 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003541#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003542
3543 /* Make extra sure authentication was performed, exactly once */
3544 if( auth_done != 1 )
3545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3547 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003548 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003549
Hanno Beckera5a2b082019-05-15 14:03:01 +01003550#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003551 if( rec->cid_len != 0 )
3552 {
3553 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3554 &rec->type );
3555 if( ret != 0 )
3556 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3557 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003558#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003561
3562 return( 0 );
3563}
3564
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003565#undef MAC_NONE
3566#undef MAC_PLAINTEXT
3567#undef MAC_CIPHERTEXT
3568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003570/*
3571 * Compression/decompression functions
3572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003574{
3575 int ret;
3576 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003577 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003578 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003579 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003582
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003583 if( len_pre == 0 )
3584 return( 0 );
3585
Teppo Järvelin91d79382019-10-02 09:09:31 +03003586 mbedtls_platform_memcpy( msg_pre, ssl->out_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003589 ssl->out_msglen ) );
3590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003591 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003592 ssl->out_msg, ssl->out_msglen );
3593
Paul Bakker48916f92012-09-16 19:57:18 +00003594 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3595 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3596 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003597 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003598
Paul Bakker48916f92012-09-16 19:57:18 +00003599 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003600 if( ret != Z_OK )
3601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3603 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003604 }
3605
Angus Grattond8213d02016-05-25 20:56:48 +10003606 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003607 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003609 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003610 ssl->out_msglen ) );
3611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003613 ssl->out_msg, ssl->out_msglen );
3614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003616
3617 return( 0 );
3618}
3619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003621{
3622 int ret;
3623 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003624 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003625 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003626 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003629
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003630 if( len_pre == 0 )
3631 return( 0 );
3632
Teppo Järvelin91d79382019-10-02 09:09:31 +03003633 mbedtls_platform_memcpy( msg_pre, ssl->in_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003636 ssl->in_msglen ) );
3637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003639 ssl->in_msg, ssl->in_msglen );
3640
Paul Bakker48916f92012-09-16 19:57:18 +00003641 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3642 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3643 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003644 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003645 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003646
Paul Bakker48916f92012-09-16 19:57:18 +00003647 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003648 if( ret != Z_OK )
3649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3651 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003652 }
3653
Angus Grattond8213d02016-05-25 20:56:48 +10003654 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003655 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003658 ssl->in_msglen ) );
3659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003661 ssl->in_msg, ssl->in_msglen );
3662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003664
3665 return( 0 );
3666}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3670static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672#if defined(MBEDTLS_SSL_PROTO_DTLS)
3673static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003674{
3675 /* If renegotiation is not enforced, retransmit until we would reach max
3676 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003677 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003678 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003679 uint32_t ratio =
3680 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3681 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003682 unsigned char doublings = 1;
3683
3684 while( ratio != 0 )
3685 {
3686 ++doublings;
3687 ratio >>= 1;
3688 }
3689
3690 if( ++ssl->renego_records_seen > doublings )
3691 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003693 return( 0 );
3694 }
3695 }
3696
3697 return( ssl_write_hello_request( ssl ) );
3698}
3699#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003700#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003701
Paul Bakker5121ce52009-01-03 21:22:43 +00003702/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003703 * Fill the input message buffer by appending data to it.
3704 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003705 *
3706 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3707 * available (from this read and/or a previous one). Otherwise, an error code
3708 * is returned (possibly EOF or WANT_READ).
3709 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003710 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3711 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3712 * since we always read a whole datagram at once.
3713 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003714 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003715 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003717int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003718{
Paul Bakker23986e52011-04-24 08:57:21 +00003719 int ret;
3720 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003723
Hanno Beckera58a8962019-06-13 16:11:15 +01003724 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3725 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003728 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003729 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003730 }
3731
Angus Grattond8213d02016-05-25 20:56:48 +10003732 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3735 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003736 }
3737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003738#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003739 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003740 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003741 uint32_t timeout;
3742
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003743 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003744 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3745 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003746 {
3747 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3748 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3749 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3750 }
3751
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003752 /*
3753 * The point is, we need to always read a full datagram at once, so we
3754 * sometimes read more then requested, and handle the additional data.
3755 * It could be the rest of the current record (while fetching the
3756 * header) and/or some other records in the same datagram.
3757 */
3758
3759 /*
3760 * Move to the next record in the already read datagram if applicable
3761 */
3762 if( ssl->next_record_offset != 0 )
3763 {
3764 if( ssl->in_left < ssl->next_record_offset )
3765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3767 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003768 }
3769
3770 ssl->in_left -= ssl->next_record_offset;
3771
3772 if( ssl->in_left != 0 )
3773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003775 ssl->next_record_offset ) );
3776 memmove( ssl->in_hdr,
3777 ssl->in_hdr + ssl->next_record_offset,
3778 ssl->in_left );
3779 }
3780
3781 ssl->next_record_offset = 0;
3782 }
3783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003785 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003786
3787 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003788 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003789 */
3790 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003793 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003794 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003795
3796 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003797 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003798 * are not at the beginning of a new record, the caller did something
3799 * wrong.
3800 */
3801 if( ssl->in_left != 0 )
3802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3804 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003805 }
3806
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003807 /*
3808 * Don't even try to read if time's out already.
3809 * This avoids by-passing the timer when repeatedly receiving messages
3810 * that will end up being dropped.
3811 */
3812 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003813 {
3814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003815 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003816 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003817 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003818 {
Angus Grattond8213d02016-05-25 20:56:48 +10003819 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003821 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003822 timeout = ssl->handshake->retransmit_timeout;
3823 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003824 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003827
Hanno Beckera58a8962019-06-13 16:11:15 +01003828 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3829 {
3830 ret = mbedtls_ssl_get_recv_timeout( ssl )
3831 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3832 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003833 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003834 {
3835 ret = mbedtls_ssl_get_recv( ssl )
3836 ( ssl->p_bio, ssl->in_hdr, len );
3837 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003839 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003840
3841 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003843 }
3844
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003845 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003848 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003851 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003852 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003855 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003856 }
3857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003861 return( ret );
3862 }
3863
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003864 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003865 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003867 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3868 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003869 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003870 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003871 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003873 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003874 return( ret );
3875 }
3876
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003877 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003878 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003880 }
3881
Paul Bakker5121ce52009-01-03 21:22:43 +00003882 if( ret < 0 )
3883 return( ret );
3884
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003885 ssl->in_left = ret;
3886 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003887 MBEDTLS_SSL_TRANSPORT_ELSE
3888#endif /* MBEDTLS_SSL_PROTO_DTLS */
3889#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003892 ssl->in_left, nb_want ) );
3893
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003894 while( ssl->in_left < nb_want )
3895 {
3896 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003897
3898 if( ssl_check_timer( ssl ) != 0 )
3899 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3900 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003901 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003902 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003903 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003904 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3905 ssl->in_hdr + ssl->in_left, len,
3906 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003907 }
3908 else
3909 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003910 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003911 ssl->in_hdr + ssl->in_left, len );
3912 }
3913 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003916 ssl->in_left, nb_want ) );
3917 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003918
3919 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003920 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003921
3922 if( ret < 0 )
3923 return( ret );
3924
mohammad160352aecb92018-03-28 23:41:40 -07003925 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003926 {
Darryl Green11999bb2018-03-13 15:22:58 +00003927 MBEDTLS_SSL_DEBUG_MSG( 1,
3928 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003929 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003930 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3931 }
3932
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003933 ssl->in_left += ret;
3934 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003935 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003936#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003939
3940 return( 0 );
3941}
3942
3943/*
3944 * Flush any data not yet written
3945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003946int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003947{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003948 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003949 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003952
Hanno Beckera58a8962019-06-13 16:11:15 +01003953 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003956 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003957 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003958 }
3959
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003960 /* Avoid incrementing counter if data is flushed */
3961 if( ssl->out_left == 0 )
3962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003964 return( 0 );
3965 }
3966
Paul Bakker5121ce52009-01-03 21:22:43 +00003967 while( ssl->out_left > 0 )
3968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003970 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003971
Hanno Becker2b1e3542018-08-06 11:19:13 +01003972 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003973 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003975 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003976
3977 if( ret <= 0 )
3978 return( ret );
3979
mohammad160352aecb92018-03-28 23:41:40 -07003980 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003981 {
Darryl Green11999bb2018-03-13 15:22:58 +00003982 MBEDTLS_SSL_DEBUG_MSG( 1,
3983 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003984 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003985 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3986 }
3987
Paul Bakker5121ce52009-01-03 21:22:43 +00003988 ssl->out_left -= ret;
3989 }
3990
Hanno Becker2b1e3542018-08-06 11:19:13 +01003991#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003992 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003993 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003994 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003995 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003996 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003997#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003998#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003999 {
4000 ssl->out_hdr = ssl->out_buf + 8;
4001 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004002#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01004003 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004006
4007 return( 0 );
4008}
4009
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004010/*
4011 * Functions to handle the DTLS retransmission state machine
4012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004013#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004014/*
4015 * Append current handshake message to current outgoing flight
4016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004017static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004018{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004019 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01004020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
4021 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
4022 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004023
4024 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004025 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004026 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004028 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004029 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004030 }
4031
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004032 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004033 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004035 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004036 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004037 }
4038
4039 /* Copy current handshake message with headers */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004040 mbedtls_platform_memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004041 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004042 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004043 msg->next = NULL;
4044
4045 /* Append to the current flight */
4046 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004047 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004048 else
4049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004050 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004051 while( cur->next != NULL )
4052 cur = cur->next;
4053 cur->next = msg;
4054 }
4055
Hanno Becker3b235902018-08-06 09:54:53 +01004056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004057 return( 0 );
4058}
4059
4060/*
4061 * Free the current flight of handshake messages
4062 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004063static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004064{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004065 mbedtls_ssl_flight_item *cur = flight;
4066 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004067
4068 while( cur != NULL )
4069 {
4070 next = cur->next;
4071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004072 mbedtls_free( cur->p );
4073 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004074
4075 cur = next;
4076 }
4077}
4078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004079#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4080static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004081#endif
4082
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004083/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004084 * Swap transform_out and out_ctr with the alternative ones
4085 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004086static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004087{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004088 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004089 unsigned char tmp_out_ctr[8];
4090
4091 if( ssl->transform_out == ssl->handshake->alt_transform_out )
4092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004093 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004094 return;
4095 }
4096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004097 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004098
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004099 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004100 tmp_transform = ssl->transform_out;
4101 ssl->transform_out = ssl->handshake->alt_transform_out;
4102 ssl->handshake->alt_transform_out = tmp_transform;
4103
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004104 /* Swap epoch + sequence_number */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004105 mbedtls_platform_memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4106 mbedtls_platform_memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
4107 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004108
4109 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004110 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004112#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4113 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004115 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004117 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4118 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004119 }
4120 }
4121#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004122}
4123
4124/*
4125 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004126 */
4127int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4128{
4129 int ret = 0;
4130
4131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4132
4133 ret = mbedtls_ssl_flight_transmit( ssl );
4134
4135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4136
4137 return( ret );
4138}
4139
4140/*
4141 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004142 *
4143 * Need to remember the current message in case flush_output returns
4144 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004145 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004146 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004147int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004148{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004149 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004152 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004153 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004155
4156 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004157 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004158 ssl_swap_epochs( ssl );
4159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004160 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004161 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004162
4163 while( ssl->handshake->cur_msg != NULL )
4164 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004165 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004166 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004167
Hanno Beckere1dcb032018-08-17 16:47:58 +01004168 int const is_finished =
4169 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4170 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4171
Hanno Becker04da1892018-08-14 13:22:10 +01004172 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4173 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4174
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004175 /* Swap epochs before sending Finished: we can't do it after
4176 * sending ChangeCipherSpec, in case write returns WANT_READ.
4177 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004178 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004179 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004181 ssl_swap_epochs( ssl );
4182 }
4183
Hanno Becker67bc7c32018-08-06 11:33:50 +01004184 ret = ssl_get_remaining_payload_in_datagram( ssl );
4185 if( ret < 0 )
4186 return( ret );
4187 max_frag_len = (size_t) ret;
4188
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004189 /* CCS is copied as is, while HS messages may need fragmentation */
4190 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4191 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004192 if( max_frag_len == 0 )
4193 {
4194 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4195 return( ret );
4196
4197 continue;
4198 }
4199
Teppo Järvelin91d79382019-10-02 09:09:31 +03004200 mbedtls_platform_memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004201 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004202 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004203
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004204 /* Update position inside current message */
4205 ssl->handshake->cur_msg_p += cur->len;
4206 }
4207 else
4208 {
4209 const unsigned char * const p = ssl->handshake->cur_msg_p;
4210 const size_t hs_len = cur->len - 12;
4211 const size_t frag_off = p - ( cur->p + 12 );
4212 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004213 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004214
Hanno Beckere1dcb032018-08-17 16:47:58 +01004215 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004216 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004217 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004218 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004219
Hanno Becker67bc7c32018-08-06 11:33:50 +01004220 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4221 return( ret );
4222
4223 continue;
4224 }
4225 max_hs_frag_len = max_frag_len - 12;
4226
4227 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4228 max_hs_frag_len : rem_len;
4229
4230 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004231 {
4232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004233 (unsigned) cur_hs_frag_len,
4234 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004235 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004236
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004237 /* Messages are stored with handshake headers as if not fragmented,
4238 * copy beginning of headers then fill fragmentation fields.
4239 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004240 mbedtls_platform_memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004241
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004242 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[6], frag_off );
4243 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[9],
4244 cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004245
4246 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4247
Hanno Becker3f7b9732018-08-28 09:53:25 +01004248 /* Copy the handshake message content and set records fields */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004249 mbedtls_platform_memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004250 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004251 ssl->out_msgtype = cur->type;
4252
4253 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004254 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004255 }
4256
4257 /* If done with the current message move to the next one if any */
4258 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4259 {
4260 if( cur->next != NULL )
4261 {
4262 ssl->handshake->cur_msg = cur->next;
4263 ssl->handshake->cur_msg_p = cur->next->p + 12;
4264 }
4265 else
4266 {
4267 ssl->handshake->cur_msg = NULL;
4268 ssl->handshake->cur_msg_p = NULL;
4269 }
4270 }
4271
4272 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004273 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004275 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004276 return( ret );
4277 }
4278 }
4279
Hanno Becker67bc7c32018-08-06 11:33:50 +01004280 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4281 return( ret );
4282
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004283 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4285 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004286 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004288 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004289 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4290 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004291
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004293
4294 return( 0 );
4295}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004296
4297/*
4298 * To be called when the last message of an incoming flight is received.
4299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004300void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004301{
4302 /* We won't need to resend that one any more */
4303 ssl_flight_free( ssl->handshake->flight );
4304 ssl->handshake->flight = NULL;
4305 ssl->handshake->cur_msg = NULL;
4306
4307 /* The next incoming flight will start with this msg_seq */
4308 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4309
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004310 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004311 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004312
Hanno Becker0271f962018-08-16 13:23:47 +01004313 /* Clear future message buffering structure. */
4314 ssl_buffering_free( ssl );
4315
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004316 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004317 ssl_set_timer( ssl, 0 );
4318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4320 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004322 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004323 }
4324 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004325 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004326}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004327
4328/*
4329 * To be called when the last message of an outgoing flight is send.
4330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004332{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004333 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004334 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004336 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4337 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004339 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004340 }
4341 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004342 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004343}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004344#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004345
Paul Bakker5121ce52009-01-03 21:22:43 +00004346/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004347 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004348 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004349
4350/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004351 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004352 *
4353 * - fill in handshake headers
4354 * - update handshake checksum
4355 * - DTLS: save message for resending
4356 * - then pass to the record layer
4357 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004358 * DTLS: except for HelloRequest, messages are only queued, and will only be
4359 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004360 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004361 * Inputs:
4362 * - ssl->out_msglen: 4 + actual handshake message len
4363 * (4 is the size of handshake headers for TLS)
4364 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4365 * - ssl->out_msg + 4: the handshake message body
4366 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004367 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004368 * - ssl->out_msglen: the length of the record contents
4369 * (including handshake headers but excluding record headers)
4370 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004371 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004372int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004373{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004374 int ret;
4375 const size_t hs_len = ssl->out_msglen - 4;
4376 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004377
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4379
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004380 /*
4381 * Sanity checks
4382 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004383 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004384 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4385 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004386 /* In SSLv3, the client might send a NoCertificate alert. */
4387#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004388 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004389 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004390 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4391 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004392#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4393 {
4394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4395 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4396 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004397 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004398
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004399 /* Whenever we send anything different from a
4400 * HelloRequest we should be in a handshake - double check. */
4401 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4402 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004403 ssl->handshake == NULL )
4404 {
4405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4406 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4407 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004409#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004410 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004411 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004412 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004413 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4415 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004416 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004417#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004418
Hanno Beckerb50a2532018-08-06 11:52:54 +01004419 /* Double-check that we did not exceed the bounds
4420 * of the outgoing record buffer.
4421 * This should never fail as the various message
4422 * writing functions must obey the bounds of the
4423 * outgoing record buffer, but better be safe.
4424 *
4425 * Note: We deliberately do not check for the MTU or MFL here.
4426 */
4427 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4428 {
4429 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4430 "size %u, maximum %u",
4431 (unsigned) ssl->out_msglen,
4432 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4433 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4434 }
4435
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004436 /*
4437 * Fill handshake headers
4438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004439 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004440 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004441 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[1], hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004442
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004443 /*
4444 * DTLS has additional fields in the Handshake layer,
4445 * between the length field and the actual payload:
4446 * uint16 message_seq;
4447 * uint24 fragment_offset;
4448 * uint24 fragment_length;
4449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004450#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004451 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004452 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004453 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004454 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004455 {
4456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4457 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004458 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004459 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004460 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4461 }
4462
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004463 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004464 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004465
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004466 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004467 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004468 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004469 (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[4],
4470 ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004471 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004472 }
4473 else
4474 {
4475 ssl->out_msg[4] = 0;
4476 ssl->out_msg[5] = 0;
4477 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004478
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004479 /* Handshake hashes are computed without fragmentation,
4480 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004481 mbedtls_platform_memset( ssl->out_msg + 6, 0x00, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03004482 mbedtls_platform_memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004483 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004484#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004485
Hanno Becker0207e532018-08-28 10:28:28 +01004486 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004487 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004488 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004489 }
4490
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004491 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004492#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004493 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004494 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4495 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004496 {
4497 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004499 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004500 return( ret );
4501 }
4502 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004503 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004504#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004505 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004506 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004507 {
4508 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4509 return( ret );
4510 }
4511 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004512
4513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4514
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004515 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004516}
4517
4518/*
4519 * Record layer functions
4520 */
4521
4522/*
4523 * Write current record.
4524 *
4525 * Uses:
4526 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4527 * - ssl->out_msglen: length of the record content (excl headers)
4528 * - ssl->out_msg: record content
4529 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004530int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004531{
4532 int ret, done = 0;
4533 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004534 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004535
4536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004538#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004539 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004540 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004541 {
4542 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004544 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004545 return( ret );
4546 }
4547
4548 len = ssl->out_msglen;
4549 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004550#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4553 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004557 ret = mbedtls_ssl_hw_record_write( ssl );
4558 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004560 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4561 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004562 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004563
4564 if( ret == 0 )
4565 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004566 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004567#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004568 if( !done )
4569 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004570 unsigned i;
4571 size_t protected_record_size;
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004572 volatile int encrypted_fi = 0;
Hanno Becker2b1e3542018-08-06 11:19:13 +01004573
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004574 /* Skip writing the record content type to after the encryption,
4575 * as it may change when using the CID extension. */
4576
Hanno Becker2881d802019-05-22 14:44:53 +01004577 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4578 mbedtls_ssl_get_minor_ver( ssl ),
4579 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004580
Teppo Järvelin91d79382019-10-02 09:09:31 +03004581 mbedtls_platform_memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004582 (void)mbedtls_platform_put_uint16_be( ssl->out_len, len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004583
Paul Bakker48916f92012-09-16 19:57:18 +00004584 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004585 {
Hanno Becker3307b532017-12-27 21:37:21 +00004586 mbedtls_record rec;
4587
4588 rec.buf = ssl->out_iv;
4589 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4590 ( ssl->out_iv - ssl->out_buf );
4591 rec.data_len = ssl->out_msglen;
4592 rec.data_offset = ssl->out_msg - rec.buf;
4593
Teppo Järvelin91d79382019-10-02 09:09:31 +03004594 mbedtls_platform_memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004595 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4596 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004597 ssl->conf->transport, rec.ver );
4598 rec.type = ssl->out_msgtype;
4599
Hanno Beckera5a2b082019-05-15 14:03:01 +01004600#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004601 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004602 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004603#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004604
Hanno Becker611a83b2018-01-03 14:27:32 +00004605 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004606 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004607 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004609 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004610 return( ret );
4611 }
4612
Hanno Becker3307b532017-12-27 21:37:21 +00004613 if( rec.data_offset != 0 )
4614 {
4615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4616 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4617 }
4618
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004619 /* Update the record content type and CID. */
4620 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004621#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03004622 /* Not using more secure mbedtls_platform_memcpy as cid is public */
4623 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004624#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004625 ssl->out_msglen = len = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004626 (void)mbedtls_platform_put_uint16_be( ssl->out_len, rec.data_len );
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004627 encrypted_fi = 1;
4628 }
4629
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004630 /* Double check to ensure the encryption has been done */
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004631 if( ssl->transform_out != NULL && encrypted_fi == 0 )
4632 {
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004633 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004634 }
4635
Hanno Becker43395762019-05-03 14:46:38 +01004636 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004637
4638#if defined(MBEDTLS_SSL_PROTO_DTLS)
4639 /* In case of DTLS, double-check that we don't exceed
4640 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004641 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004642 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004643 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004644 if( ret < 0 )
4645 return( ret );
4646
4647 if( protected_record_size > (size_t) ret )
4648 {
4649 /* Should never happen */
4650 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4651 }
4652 }
4653#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004654
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004655 /* Now write the potentially updated record content type. */
4656 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004659 "version = [%d:%d], msglen = %d",
4660 ssl->out_hdr[0], ssl->out_hdr[1],
4661 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004663 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004664 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004665
4666 ssl->out_left += protected_record_size;
4667 ssl->out_hdr += protected_record_size;
4668 ssl_update_out_pointers( ssl, ssl->transform_out );
4669
Hanno Becker04484622018-08-06 09:49:38 +01004670 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4671 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4672 break;
4673
4674 /* The loop goes to its end iff the counter is wrapping */
4675 if( i == ssl_ep_len( ssl ) )
4676 {
4677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4678 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4679 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004680 }
4681
Hanno Becker67bc7c32018-08-06 11:33:50 +01004682#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004683 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004684 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004685 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004686 size_t remaining;
4687 ret = ssl_get_remaining_payload_in_datagram( ssl );
4688 if( ret < 0 )
4689 {
4690 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4691 ret );
4692 return( ret );
4693 }
4694
4695 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004696 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004697 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004698 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004699 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004700 else
4701 {
Hanno Becker513815a2018-08-20 11:56:09 +01004702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004703 }
4704 }
4705#endif /* MBEDTLS_SSL_PROTO_DTLS */
4706
4707 if( ( flush == SSL_FORCE_FLUSH ) &&
4708 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004710 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004711 return( ret );
4712 }
4713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004715
4716 return( 0 );
4717}
4718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004719#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004720
4721static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4722{
4723 if( ssl->in_msglen < ssl->in_hslen ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +03004724 mbedtls_platform_memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4725 mbedtls_platform_memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
Hanno Beckere25e3b72018-08-16 09:30:53 +01004726 {
4727 return( 1 );
4728 }
4729 return( 0 );
4730}
Hanno Becker44650b72018-08-16 12:51:11 +01004731
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004732static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004733{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004734 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[9] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004735}
4736
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004737static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004738{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004739 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[6] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004740}
4741
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004742static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004743{
4744 uint32_t msg_len, frag_off, frag_len;
4745
4746 msg_len = ssl_get_hs_total_len( ssl );
4747 frag_off = ssl_get_hs_frag_off( ssl );
4748 frag_len = ssl_get_hs_frag_len( ssl );
4749
4750 if( frag_off > msg_len )
4751 return( -1 );
4752
4753 if( frag_len > msg_len - frag_off )
4754 return( -1 );
4755
4756 if( frag_len + 12 > ssl->in_msglen )
4757 return( -1 );
4758
4759 return( 0 );
4760}
4761
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004762/*
4763 * Mark bits in bitmask (used for DTLS HS reassembly)
4764 */
4765static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4766{
4767 unsigned int start_bits, end_bits;
4768
4769 start_bits = 8 - ( offset % 8 );
4770 if( start_bits != 8 )
4771 {
4772 size_t first_byte_idx = offset / 8;
4773
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004774 /* Special case */
4775 if( len <= start_bits )
4776 {
4777 for( ; len != 0; len-- )
4778 mask[first_byte_idx] |= 1 << ( start_bits - len );
4779
4780 /* Avoid potential issues with offset or len becoming invalid */
4781 return;
4782 }
4783
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004784 offset += start_bits; /* Now offset % 8 == 0 */
4785 len -= start_bits;
4786
4787 for( ; start_bits != 0; start_bits-- )
4788 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4789 }
4790
4791 end_bits = len % 8;
4792 if( end_bits != 0 )
4793 {
4794 size_t last_byte_idx = ( offset + len ) / 8;
4795
4796 len -= end_bits; /* Now len % 8 == 0 */
4797
4798 for( ; end_bits != 0; end_bits-- )
4799 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4800 }
4801
4802 memset( mask + offset / 8, 0xFF, len / 8 );
4803}
4804
4805/*
4806 * Check that bitmask is full
4807 */
4808static int ssl_bitmask_check( unsigned char *mask, size_t len )
4809{
4810 size_t i;
4811
4812 for( i = 0; i < len / 8; i++ )
4813 if( mask[i] != 0xFF )
4814 return( -1 );
4815
4816 for( i = 0; i < len % 8; i++ )
4817 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4818 return( -1 );
4819
4820 return( 0 );
4821}
4822
Hanno Becker56e205e2018-08-16 09:06:12 +01004823/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004824static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004825 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004826{
Hanno Becker56e205e2018-08-16 09:06:12 +01004827 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004828
Hanno Becker56e205e2018-08-16 09:06:12 +01004829 alloc_len = 12; /* Handshake header */
4830 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004831
Hanno Beckerd07df862018-08-16 09:14:58 +01004832 if( add_bitmap )
4833 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004834
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004835 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004836}
Hanno Becker56e205e2018-08-16 09:06:12 +01004837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004838#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004839
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004840static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004841{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004842 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[1] ) );
Hanno Becker12555c62018-08-16 12:47:53 +01004843}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004844
Simon Butcher99000142016-10-13 17:21:01 +01004845int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004846{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004847 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004850 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004851 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004852 }
4853
Hanno Becker12555c62018-08-16 12:47:53 +01004854 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004856 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004857 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004858 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004860#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004861 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004862 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004863 int ret;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004864 unsigned int recv_msg_seq = (unsigned int)
4865 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004866
Hanno Becker44650b72018-08-16 12:51:11 +01004867 if( ssl_check_hs_header( ssl ) != 0 )
4868 {
4869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4870 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4871 }
4872
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004873 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004874 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4875 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4876 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4877 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004878 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004879 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4880 {
4881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4882 recv_msg_seq,
4883 ssl->handshake->in_msg_seq ) );
4884 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4885 }
4886
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004887 /* Retransmit only on last message from previous flight, to avoid
4888 * too many retransmissions.
4889 * Besides, No sane server ever retransmits HelloVerifyRequest */
4890 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004891 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004894 "message_seq = %d, start_of_flight = %d",
4895 recv_msg_seq,
4896 ssl->handshake->in_flight_start_seq ) );
4897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004898 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004900 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004901 return( ret );
4902 }
4903 }
4904 else
4905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004907 "message_seq = %d, expected = %d",
4908 recv_msg_seq,
4909 ssl->handshake->in_msg_seq ) );
4910 }
4911
Hanno Becker90333da2017-10-10 11:27:13 +01004912 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004913 }
4914 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004915
Hanno Becker6d97ef52018-08-16 13:09:04 +01004916 /* Message reassembly is handled alongside buffering of future
4917 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004918 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004919 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004920 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004923 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004924 }
4925 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004926 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004927#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004928#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004929 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004930 /* With TLS we don't handle fragmentation (for now) */
4931 if( ssl->in_msglen < ssl->in_hslen )
4932 {
4933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4934 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4935 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004936 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004937#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004938
Simon Butcher99000142016-10-13 17:21:01 +01004939 return( 0 );
4940}
4941
4942void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4943{
Hanno Becker0271f962018-08-16 13:23:47 +01004944 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004945
Hanno Becker0271f962018-08-16 13:23:47 +01004946 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004947 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004948
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004949 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004950#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004951 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004952 ssl->handshake != NULL )
4953 {
Hanno Becker0271f962018-08-16 13:23:47 +01004954 unsigned offset;
4955 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004956
Hanno Becker0271f962018-08-16 13:23:47 +01004957 /* Increment handshake sequence number */
4958 hs->in_msg_seq++;
4959
4960 /*
4961 * Clear up handshake buffering and reassembly structure.
4962 */
4963
4964 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004965 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004966
4967 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004968 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4969 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004970 offset++, hs_buf++ )
4971 {
4972 *hs_buf = *(hs_buf + 1);
4973 }
4974
4975 /* Create a fresh last entry */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004976 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004977 }
4978#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004979}
4980
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004981/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004982 * DTLS anti-replay: RFC 6347 4.1.2.6
4983 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004984 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4985 * Bit n is set iff record number in_window_top - n has been seen.
4986 *
4987 * Usually, in_window_top is the last record number seen and the lsb of
4988 * in_window is set. The only exception is the initial state (record number 0
4989 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004990 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004991#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4992static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004993{
4994 ssl->in_window_top = 0;
4995 ssl->in_window = 0;
4996}
4997
4998static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4999{
5000 return( ( (uint64_t) buf[0] << 40 ) |
5001 ( (uint64_t) buf[1] << 32 ) |
5002 ( (uint64_t) buf[2] << 24 ) |
5003 ( (uint64_t) buf[3] << 16 ) |
5004 ( (uint64_t) buf[4] << 8 ) |
5005 ( (uint64_t) buf[5] ) );
5006}
5007
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005008static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
5009{
5010 int ret;
5011 unsigned char *original_in_ctr;
5012
5013 // save original in_ctr
5014 original_in_ctr = ssl->in_ctr;
5015
5016 // use counter from record
5017 ssl->in_ctr = record_in_ctr;
5018
5019 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
5020
5021 // restore the counter
5022 ssl->in_ctr = original_in_ctr;
5023
5024 return ret;
5025}
5026
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005027/*
5028 * Return 0 if sequence number is acceptable, -1 otherwise
5029 */
Hanno Beckerfc551722019-07-12 08:50:37 +01005030int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005031{
5032 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5033 uint64_t bit;
5034
Hanno Becker7f376f42019-06-12 16:20:48 +01005035 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5036 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5037 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005038 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01005039 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005040
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005041 if( rec_seqnum > ssl->in_window_top )
5042 return( 0 );
5043
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005044 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005045
5046 if( bit >= 64 )
5047 return( -1 );
5048
5049 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
5050 return( -1 );
5051
5052 return( 0 );
5053}
5054
5055/*
5056 * Update replay window on new validated record
5057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005058void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005059{
5060 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5061
Hanno Becker7f376f42019-06-12 16:20:48 +01005062 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5063 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5064 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005065 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01005066 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005067
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005068 if( rec_seqnum > ssl->in_window_top )
5069 {
5070 /* Update window_top and the contents of the window */
5071 uint64_t shift = rec_seqnum - ssl->in_window_top;
5072
5073 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005074 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005075 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005076 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005077 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005078 ssl->in_window |= 1;
5079 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005080
5081 ssl->in_window_top = rec_seqnum;
5082 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005083 else
5084 {
5085 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005086 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005087
5088 if( bit < 64 ) /* Always true, but be extra sure */
5089 ssl->in_window |= (uint64_t) 1 << bit;
5090 }
5091}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005092#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005093
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005094#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005095/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005096static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
5097
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005098/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005099 * Without any SSL context, check if a datagram looks like a ClientHello with
5100 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01005101 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005102 *
5103 * - if cookie is valid, return 0
5104 * - if ClientHello looks superficially valid but cookie is not,
5105 * fill obuf and set olen, then
5106 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
5107 * - otherwise return a specific error code
5108 */
5109static int ssl_check_dtls_clihlo_cookie(
5110 mbedtls_ssl_cookie_write_t *f_cookie_write,
5111 mbedtls_ssl_cookie_check_t *f_cookie_check,
5112 void *p_cookie,
5113 const unsigned char *cli_id, size_t cli_id_len,
5114 const unsigned char *in, size_t in_len,
5115 unsigned char *obuf, size_t buf_len, size_t *olen )
5116{
5117 size_t sid_len, cookie_len;
5118 unsigned char *p;
5119
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005120 /*
5121 * Structure of ClientHello with record and handshake headers,
5122 * and expected values. We don't need to check a lot, more checks will be
5123 * done when actually parsing the ClientHello - skipping those checks
5124 * avoids code duplication and does not make cookie forging any easier.
5125 *
5126 * 0-0 ContentType type; copied, must be handshake
5127 * 1-2 ProtocolVersion version; copied
5128 * 3-4 uint16 epoch; copied, must be 0
5129 * 5-10 uint48 sequence_number; copied
5130 * 11-12 uint16 length; (ignored)
5131 *
5132 * 13-13 HandshakeType msg_type; (ignored)
5133 * 14-16 uint24 length; (ignored)
5134 * 17-18 uint16 message_seq; copied
5135 * 19-21 uint24 fragment_offset; copied, must be 0
5136 * 22-24 uint24 fragment_length; (ignored)
5137 *
5138 * 25-26 ProtocolVersion client_version; (ignored)
5139 * 27-58 Random random; (ignored)
5140 * 59-xx SessionID session_id; 1 byte len + sid_len content
5141 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5142 * ...
5143 *
5144 * Minimum length is 61 bytes.
5145 */
5146 if( in_len < 61 ||
5147 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5148 in[3] != 0 || in[4] != 0 ||
5149 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5150 {
5151 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5152 }
5153
5154 sid_len = in[59];
5155 if( sid_len > in_len - 61 )
5156 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5157
5158 cookie_len = in[60 + sid_len];
5159 if( cookie_len > in_len - 60 )
5160 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5161
5162 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5163 cli_id, cli_id_len ) == 0 )
5164 {
5165 /* Valid cookie */
5166 return( 0 );
5167 }
5168
5169 /*
5170 * If we get here, we've got an invalid cookie, let's prepare HVR.
5171 *
5172 * 0-0 ContentType type; copied
5173 * 1-2 ProtocolVersion version; copied
5174 * 3-4 uint16 epoch; copied
5175 * 5-10 uint48 sequence_number; copied
5176 * 11-12 uint16 length; olen - 13
5177 *
5178 * 13-13 HandshakeType msg_type; hello_verify_request
5179 * 14-16 uint24 length; olen - 25
5180 * 17-18 uint16 message_seq; copied
5181 * 19-21 uint24 fragment_offset; copied
5182 * 22-24 uint24 fragment_length; olen - 25
5183 *
5184 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5185 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5186 *
5187 * Minimum length is 28.
5188 */
5189 if( buf_len < 28 )
5190 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5191
5192 /* Copy most fields and adapt others */
Teppo Järvelin91d79382019-10-02 09:09:31 +03005193 mbedtls_platform_memcpy( obuf, in, 25 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005194 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5195 obuf[25] = 0xfe;
5196 obuf[26] = 0xff;
5197
5198 /* Generate and write actual cookie */
5199 p = obuf + 28;
5200 if( f_cookie_write( p_cookie,
5201 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5202 {
5203 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5204 }
5205
5206 *olen = p - obuf;
5207
5208 /* Go back and fill length fields */
5209 obuf[27] = (unsigned char)( *olen - 28 );
5210
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005211 (void)mbedtls_platform_put_uint24_be( &obuf[14], ( *olen - 25 ) );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005212 obuf[22] = obuf[14];
5213 obuf[23] = obuf[15];
5214 obuf[24] = obuf[16];
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005215
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005216 (void)mbedtls_platform_put_uint16_be( &obuf[11], ( *olen - 13 ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005217
5218 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5219}
5220
5221/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005222 * Handle possible client reconnect with the same UDP quadruplet
5223 * (RFC 6347 Section 4.2.8).
5224 *
5225 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5226 * that looks like a ClientHello.
5227 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005228 * - if the input looks like a ClientHello without cookies,
5229 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005230 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005231 * - if the input looks like a ClientHello with a valid cookie,
5232 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005233 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005234 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005235 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005236 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005237 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5238 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005239 */
5240static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5241{
5242 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005243 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005244
Hanno Becker87b56262019-07-10 14:37:41 +01005245 if( ssl->conf->f_cookie_write == NULL ||
5246 ssl->conf->f_cookie_check == NULL )
5247 {
5248 /* If we can't use cookies to verify reachability of the peer,
5249 * drop the record. */
5250 return( 0 );
5251 }
5252
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005253 ret = ssl_check_dtls_clihlo_cookie(
5254 ssl->conf->f_cookie_write,
5255 ssl->conf->f_cookie_check,
5256 ssl->conf->p_cookie,
5257 ssl->cli_id, ssl->cli_id_len,
5258 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005259 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005260
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005261 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5262
5263 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005264 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005265 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005266 * If the error is permanent we'll catch it later,
5267 * if it's not, then hopefully it'll work next time. */
Hanno Beckera58a8962019-06-13 16:11:15 +01005268 (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
Hanno Becker87b56262019-07-10 14:37:41 +01005269 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005270 }
5271
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005272 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005273 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005274 /* Got a valid cookie, partially reset context */
5275 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5276 {
5277 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5278 return( ret );
5279 }
5280
5281 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005282 }
5283
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005284 return( ret );
5285}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005286#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005287
Hanno Becker46483f12019-05-03 13:25:54 +01005288static int ssl_check_record_type( uint8_t record_type )
5289{
5290 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5291 record_type != MBEDTLS_SSL_MSG_ALERT &&
5292 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5293 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5294 {
5295 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5296 }
5297
5298 return( 0 );
5299}
5300
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005301/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005302 * ContentType type;
5303 * ProtocolVersion version;
5304 * uint16 epoch; // DTLS only
5305 * uint48 sequence_number; // DTLS only
5306 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005307 *
5308 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005309 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005310 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5311 *
5312 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005313 * 1. proceed with the record if this function returns 0
5314 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5315 * 3. return CLIENT_RECONNECT if this function return that value
5316 * 4. drop the whole datagram if this function returns anything else.
5317 * Point 2 is needed when the peer is resending, and we have already received
5318 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005319 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005320static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005321 unsigned char *buf,
5322 size_t len,
5323 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005324{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005325 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005326
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005327 size_t const rec_hdr_type_offset = 0;
5328 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005329
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005330 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5331 rec_hdr_type_len;
5332 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005333
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005334 size_t const rec_hdr_ctr_len = 8;
5335#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005336 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005337 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5338 rec_hdr_version_len;
5339
Hanno Beckera5a2b082019-05-15 14:03:01 +01005340#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005341 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5342 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005343 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005344#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5345#endif /* MBEDTLS_SSL_PROTO_DTLS */
5346
5347 size_t rec_hdr_len_offset; /* To be determined */
5348 size_t const rec_hdr_len_len = 2;
5349
5350 /*
5351 * Check minimum lengths for record header.
5352 */
5353
5354#if defined(MBEDTLS_SSL_PROTO_DTLS)
5355 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5356 {
5357 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5358 }
5359 MBEDTLS_SSL_TRANSPORT_ELSE
5360#endif /* MBEDTLS_SSL_PROTO_DTLS */
5361#if defined(MBEDTLS_SSL_PROTO_TLS)
5362 {
5363 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5364 }
5365#endif /* MBEDTLS_SSL_PROTO_DTLS */
5366
5367 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5368 {
5369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5370 (unsigned) len,
5371 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5372 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5373 }
5374
5375 /*
5376 * Parse and validate record content type
5377 */
5378
5379 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005380
5381 /* Check record content type */
5382#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5383 rec->cid_len = 0;
5384
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005385 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005386 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5387 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005388 {
5389 /* Shift pointers to account for record header including CID
5390 * struct {
5391 * ContentType special_type = tls12_cid;
5392 * ProtocolVersion version;
5393 * uint16 epoch;
5394 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005395 * opaque cid[cid_length]; // Additional field compared to
5396 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005397 * uint16 length;
5398 * opaque enc_content[DTLSCiphertext.length];
5399 * } DTLSCiphertext;
5400 */
5401
5402 /* So far, we only support static CID lengths
5403 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005404 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5405 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005406
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005407 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005408 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5410 (unsigned) len,
5411 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005412 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005413 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005414
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005415 /* configured CID len is guaranteed at most 255, see
5416 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5417 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005418 /* Not using more secure mbedtls_platform_memcpy as cid is public */
5419 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005420 }
5421 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005422#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005423 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005424 if( ssl_check_record_type( rec->type ) )
5425 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5427 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005428 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5429 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005430 }
5431
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005432 /*
5433 * Parse and validate record version
5434 */
5435
Hanno Becker8061c6e2019-07-26 08:07:03 +01005436 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5437 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005438 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5439 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005440 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005441
Hanno Becker2881d802019-05-22 14:44:53 +01005442 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5445 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005446 }
5447
Hanno Becker7bcf2b52019-07-26 09:02:40 +01005448 if( mbedtls_ssl_ver_gt( minor_ver,
5449 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5452 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005453 }
5454
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005455 /*
5456 * Parse/Copy record sequence number.
5457 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005458
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005459#if defined(MBEDTLS_SSL_PROTO_DTLS)
5460 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5461 {
5462 /* Copy explicit record sequence number from input buffer. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005463 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5464 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005465 rec_hdr_ctr_len );
5466 }
5467 MBEDTLS_SSL_TRANSPORT_ELSE
5468#endif /* MBEDTLS_SSL_PROTO_DTLS */
5469#if defined(MBEDTLS_SSL_PROTO_TLS)
5470 {
5471 /* Copy implicit record sequence number from SSL context structure. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005472 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5473 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005474 }
5475#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005476
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005477 /*
5478 * Parse record length.
5479 */
5480
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005481 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005482 rec->data_len = mbedtls_platform_get_uint16_be( &buf[rec_hdr_len_offset] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005483 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5484
Hanno Becker8b09b732019-05-08 12:03:28 +01005485 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005486 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005487 rec->type,
5488 major_ver, minor_ver, rec->data_len ) );
5489
5490 rec->buf = buf;
5491 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005492
Hanno Beckerec014082019-07-26 08:20:27 +01005493 if( rec->data_len == 0 )
5494 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5495
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005496 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005497 * DTLS-related tests.
5498 * Check epoch before checking length constraint because
5499 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5500 * message gets duplicated before the corresponding Finished message,
5501 * the second ChangeCipherSpec should be discarded because it belongs
5502 * to an old epoch, but not because its length is shorter than
5503 * the minimum record length for packets using the new record transform.
5504 * Note that these two kinds of failures are handled differently,
5505 * as an unexpected record is silently skipped but an invalid
5506 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005507 */
5508#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005509 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005510 {
Arto Kinnunena3fa06e2019-09-09 12:22:51 +03005511 rec_epoch = (uint32_t)mbedtls_platform_get_uint16_be( rec->ctr );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005512
Hanno Beckere0452772019-07-10 17:12:07 +01005513 /* Check that the datagram is large enough to contain a record
5514 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005515 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005516 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5518 (unsigned) len,
5519 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005520 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5521 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005522
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005523 /* Records from other, non-matching epochs are silently discarded.
5524 * (The case of same-port Client reconnects must be considered in
5525 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005526 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005527 {
5528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5529 "expected %d, received %d",
5530 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005531
5532 /* Records from the next epoch are considered for buffering
5533 * (concretely: early Finished messages). */
5534 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5535 {
5536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5537 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5538 }
5539
Hanno Becker87b56262019-07-10 14:37:41 +01005540 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005541 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005542#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005543 /* For records from the correct epoch, check whether their
5544 * sequence number has been seen before. */
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005545 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
5546 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005547 {
5548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5549 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5550 }
5551#endif
5552 }
5553#endif /* MBEDTLS_SSL_PROTO_DTLS */
5554
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005555 return( 0 );
5556}
Paul Bakker5121ce52009-01-03 21:22:43 +00005557
Hanno Becker87b56262019-07-10 14:37:41 +01005558
5559#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5560static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5561{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005562 unsigned int rec_epoch = (unsigned int)
5563 mbedtls_platform_get_uint16_be( &ssl->in_ctr[0] );
Hanno Becker87b56262019-07-10 14:37:41 +01005564
5565 /*
5566 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5567 * access the first byte of record content (handshake type), as we
5568 * have an active transform (possibly iv_len != 0), so use the
5569 * fact that the record header len is 13 instead.
5570 */
5571 if( rec_epoch == 0 &&
5572 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5573 MBEDTLS_SSL_IS_SERVER &&
5574 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5575 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5576 ssl->in_left > 13 &&
5577 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5578 {
5579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5580 "from the same port" ) );
5581 return( ssl_handle_possible_reconnect( ssl ) );
5582 }
5583
5584 return( 0 );
5585}
5586#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5587
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005588/*
Manuel Pégourié-Gonnardae48d862020-01-03 12:18:49 +01005589 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005590 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005591static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5592 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005593{
5594 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005596 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005597 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5600 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604 ret = mbedtls_ssl_hw_record_read( ssl );
5605 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5608 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005609 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005610
5611 if( ret == 0 )
5612 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005613 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005615 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005616 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005617 unsigned char const old_msg_type = rec->type;
5618
Hanno Becker611a83b2018-01-03 14:27:32 +00005619 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005620 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005623
Hanno Beckera5a2b082019-05-15 14:03:01 +01005624#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005625 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005626 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5627 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005628 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005629 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005630 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005631 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005632#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005633
Paul Bakker5121ce52009-01-03 21:22:43 +00005634 return( ret );
5635 }
5636
Hanno Becker106f3da2019-07-12 09:35:58 +01005637 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005638 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005639 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005640 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005641 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005642
Paul Bakker5121ce52009-01-03 21:22:43 +00005643 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005644 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005645
Hanno Beckera5a2b082019-05-15 14:03:01 +01005646#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005647 /* We have already checked the record content type
5648 * in ssl_parse_record_header(), failing or silently
5649 * dropping the record in the case of an unknown type.
5650 *
5651 * Since with the use of CIDs, the record content type
5652 * might change during decryption, re-check the record
5653 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005654 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005655 {
5656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5657 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5658 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005659#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005660
Hanno Becker106f3da2019-07-12 09:35:58 +01005661 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005662 {
5663#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005664 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005665 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005666 {
5667 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5669 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5670 }
5671#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5672
5673 ssl->nb_zero++;
5674
5675 /*
5676 * Three or more empty messages may be a DoS attack
5677 * (excessive CPU consumption).
5678 */
5679 if( ssl->nb_zero > 3 )
5680 {
5681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005682 "messages, possible DoS attack" ) );
5683 /* Treat the records as if they were not properly authenticated,
5684 * thereby failing the connection if we see more than allowed
5685 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005686 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5687 }
5688 }
5689 else
5690 ssl->nb_zero = 0;
5691
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005692 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5693#if defined(MBEDTLS_SSL_PROTO_TLS)
5694 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005695 {
5696 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005697 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005698 if( ++ssl->in_ctr[i - 1] != 0 )
5699 break;
5700
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005701 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005702 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005703 {
5704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5705 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5706 }
5707 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005708#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005709
Paul Bakker5121ce52009-01-03 21:22:43 +00005710 }
5711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005713 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005716 }
5717#endif
5718
Hanno Beckerf0242852019-07-09 17:30:02 +01005719 /* Check actual (decrypted) record content length against
5720 * configured maximum. */
5721 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5722 {
5723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5724 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5725 }
5726
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005727 return( 0 );
5728}
5729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005730static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005731
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005732/*
5733 * Read a record.
5734 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005735 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5736 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5737 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005738 */
Hanno Becker1097b342018-08-15 14:09:41 +01005739
5740/* Helper functions for mbedtls_ssl_read_record(). */
5741static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005742static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5743static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005744
Hanno Becker327c93b2018-08-15 13:56:18 +01005745int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005746 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005747{
5748 int ret;
5749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005751
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005752 if( ssl->keep_current_message == 0 )
5753 {
5754 do {
Simon Butcher99000142016-10-13 17:21:01 +01005755
Hanno Becker26994592018-08-15 14:14:59 +01005756 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005757 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005758 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005759
Hanno Beckere74d5562018-08-15 14:26:08 +01005760 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005761 {
Hanno Becker40f50842018-08-15 14:48:01 +01005762#if defined(MBEDTLS_SSL_PROTO_DTLS)
5763 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005764
Hanno Becker40f50842018-08-15 14:48:01 +01005765 /* We only check for buffered messages if the
5766 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005767 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005768 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005769 {
Hanno Becker40f50842018-08-15 14:48:01 +01005770 if( ssl_load_buffered_message( ssl ) == 0 )
5771 have_buffered = 1;
5772 }
5773
5774 if( have_buffered == 0 )
5775#endif /* MBEDTLS_SSL_PROTO_DTLS */
5776 {
5777 ret = ssl_get_next_record( ssl );
5778 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5779 continue;
5780
5781 if( ret != 0 )
5782 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005783 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005784 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005785 return( ret );
5786 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005787 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005788 }
5789
5790 ret = mbedtls_ssl_handle_message_type( ssl );
5791
Hanno Becker40f50842018-08-15 14:48:01 +01005792#if defined(MBEDTLS_SSL_PROTO_DTLS)
5793 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5794 {
5795 /* Buffer future message */
5796 ret = ssl_buffer_message( ssl );
5797 if( ret != 0 )
5798 return( ret );
5799
5800 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5801 }
5802#endif /* MBEDTLS_SSL_PROTO_DTLS */
5803
Hanno Becker90333da2017-10-10 11:27:13 +01005804 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5805 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005806
5807 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005808 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005809 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005810 return( ret );
5811 }
5812
Hanno Becker327c93b2018-08-15 13:56:18 +01005813 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005814 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005815 {
5816 mbedtls_ssl_update_handshake_status( ssl );
5817 }
Simon Butcher99000142016-10-13 17:21:01 +01005818 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005819 else
Simon Butcher99000142016-10-13 17:21:01 +01005820 {
Hanno Becker02f59072018-08-15 14:00:24 +01005821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005822 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005823 }
5824
5825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5826
5827 return( 0 );
5828}
5829
Hanno Becker40f50842018-08-15 14:48:01 +01005830#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005831static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005832{
Hanno Becker40f50842018-08-15 14:48:01 +01005833 if( ssl->in_left > ssl->next_record_offset )
5834 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005835
Hanno Becker40f50842018-08-15 14:48:01 +01005836 return( 0 );
5837}
5838
5839static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5840{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005841 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005842 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005843 int ret = 0;
5844
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005845 if( hs == NULL )
5846 return( -1 );
5847
Hanno Beckere00ae372018-08-20 09:39:42 +01005848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5849
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005850 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5851 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5852 {
5853 /* Check if we have seen a ChangeCipherSpec before.
5854 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005855 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005856 {
5857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5858 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005859 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005860 }
5861
Hanno Becker39b8bc92018-08-28 17:17:13 +01005862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005863 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5864 ssl->in_msglen = 1;
5865 ssl->in_msg[0] = 1;
5866
5867 /* As long as they are equal, the exact value doesn't matter. */
5868 ssl->in_left = 0;
5869 ssl->next_record_offset = 0;
5870
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005871 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005872 goto exit;
5873 }
Hanno Becker37f95322018-08-16 13:55:32 +01005874
Hanno Beckerb8f50142018-08-28 10:01:34 +01005875#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005876 /* Debug only */
5877 {
5878 unsigned offset;
5879 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5880 {
5881 hs_buf = &hs->buffering.hs[offset];
5882 if( hs_buf->is_valid == 1 )
5883 {
5884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5885 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005886 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005887 }
5888 }
5889 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005890#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005891
5892 /* Check if we have buffered and/or fully reassembled the
5893 * next handshake message. */
5894 hs_buf = &hs->buffering.hs[0];
5895 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5896 {
5897 /* Synthesize a record containing the buffered HS message. */
Arto Kinnunen84eeb4f2019-09-10 10:32:30 +03005898 size_t msg_len = mbedtls_platform_get_uint24_be( &hs_buf->data[1] );
Hanno Becker37f95322018-08-16 13:55:32 +01005899
5900 /* Double-check that we haven't accidentally buffered
5901 * a message that doesn't fit into the input buffer. */
5902 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5903 {
5904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5905 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5906 }
5907
5908 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5909 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5910 hs_buf->data, msg_len + 12 );
5911
5912 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5913 ssl->in_hslen = msg_len + 12;
5914 ssl->in_msglen = msg_len + 12;
Teppo Järvelin91d79382019-10-02 09:09:31 +03005915 mbedtls_platform_memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
Hanno Becker37f95322018-08-16 13:55:32 +01005916
5917 ret = 0;
5918 goto exit;
5919 }
5920 else
5921 {
5922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5923 hs->in_msg_seq ) );
5924 }
5925
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005926 ret = -1;
5927
5928exit:
5929
5930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5931 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005932}
5933
Hanno Beckera02b0b42018-08-21 17:20:27 +01005934static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5935 size_t desired )
5936{
5937 int offset;
5938 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5940 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005941
Hanno Becker01315ea2018-08-21 17:22:17 +01005942 /* Get rid of future records epoch first, if such exist. */
5943 ssl_free_buffered_record( ssl );
5944
5945 /* Check if we have enough space available now. */
5946 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5947 hs->buffering.total_bytes_buffered ) )
5948 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005950 return( 0 );
5951 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005952
Hanno Becker4f432ad2018-08-28 10:02:32 +01005953 /* We don't have enough space to buffer the next expected handshake
5954 * message. Remove buffers used for future messages to gain space,
5955 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005956 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5957 offset >= 0; offset-- )
5958 {
5959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5960 offset ) );
5961
Hanno Beckerb309b922018-08-23 13:18:05 +01005962 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005963
5964 /* Check if we have enough space available now. */
5965 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5966 hs->buffering.total_bytes_buffered ) )
5967 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005969 return( 0 );
5970 }
5971 }
5972
5973 return( -1 );
5974}
5975
Hanno Becker40f50842018-08-15 14:48:01 +01005976static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5977{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005978 int ret = 0;
5979 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5980
5981 if( hs == NULL )
5982 return( 0 );
5983
5984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5985
5986 switch( ssl->in_msgtype )
5987 {
5988 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005990
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005991 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005992 break;
5993
5994 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005995 {
5996 unsigned recv_msg_seq_offset;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005997 unsigned recv_msg_seq = (unsigned)
5998 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005999
Hanno Becker37f95322018-08-16 13:55:32 +01006000 mbedtls_ssl_hs_buffer *hs_buf;
6001 size_t msg_len = ssl->in_hslen - 12;
6002
6003 /* We should never receive an old handshake
6004 * message - double-check nonetheless. */
6005 if( recv_msg_seq < ssl->handshake->in_msg_seq )
6006 {
6007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6008 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6009 }
6010
6011 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
6012 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
6013 {
6014 /* Silently ignore -- message too far in the future */
6015 MBEDTLS_SSL_DEBUG_MSG( 2,
6016 ( "Ignore future HS message with sequence number %u, "
6017 "buffering window %u - %u",
6018 recv_msg_seq, ssl->handshake->in_msg_seq,
6019 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
6020
6021 goto exit;
6022 }
6023
6024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
6025 recv_msg_seq, recv_msg_seq_offset ) );
6026
6027 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
6028
6029 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01006030 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01006031 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006032 size_t reassembly_buf_sz;
6033
Hanno Becker37f95322018-08-16 13:55:32 +01006034 hs_buf->is_fragmented =
6035 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
6036
6037 /* We copy the message back into the input buffer
6038 * after reassembly, so check that it's not too large.
6039 * This is an implementation-specific limitation
6040 * and not one from the standard, hence it is not
6041 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01006042 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01006043 {
6044 /* Ignore message */
6045 goto exit;
6046 }
6047
Hanno Beckere0b150f2018-08-21 15:51:03 +01006048 /* Check if we have enough space to buffer the message. */
6049 if( hs->buffering.total_bytes_buffered >
6050 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
6051 {
6052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6054 }
6055
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006056 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
6057 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01006058
6059 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
6060 hs->buffering.total_bytes_buffered ) )
6061 {
6062 if( recv_msg_seq_offset > 0 )
6063 {
6064 /* If we can't buffer a future message because
6065 * of space limitations -- ignore. */
6066 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",
6067 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6068 (unsigned) hs->buffering.total_bytes_buffered ) );
6069 goto exit;
6070 }
Hanno Beckere1801392018-08-21 16:51:05 +01006071 else
6072 {
6073 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",
6074 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6075 (unsigned) hs->buffering.total_bytes_buffered ) );
6076 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006077
Hanno Beckera02b0b42018-08-21 17:20:27 +01006078 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006079 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01006080 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",
6081 (unsigned) msg_len,
6082 (unsigned) reassembly_buf_sz,
6083 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01006084 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006085 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6086 goto exit;
6087 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006088 }
6089
6090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
6091 msg_len ) );
6092
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006093 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
6094 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01006095 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01006096 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01006097 goto exit;
6098 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006099 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006100
6101 /* Prepare final header: copy msg_type, length and message_seq,
6102 * then add standardised fragment_offset and fragment_length */
Teppo Järvelin91d79382019-10-02 09:09:31 +03006103 mbedtls_platform_memcpy( hs_buf->data, ssl->in_msg, 6 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02006104 mbedtls_platform_memset( hs_buf->data + 6, 0, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006105 mbedtls_platform_memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
Hanno Becker37f95322018-08-16 13:55:32 +01006106
6107 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01006108
6109 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006110 }
6111 else
6112 {
6113 /* Make sure msg_type and length are consistent */
Teppo Järvelin0efac532019-10-04 13:21:08 +03006114 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 +01006115 {
6116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6117 /* Ignore */
6118 goto exit;
6119 }
6120 }
6121
Hanno Becker4422bbb2018-08-20 09:40:19 +01006122 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006123 {
6124 size_t frag_len, frag_off;
6125 unsigned char * const msg = hs_buf->data + 12;
6126
6127 /*
6128 * Check and copy current fragment
6129 */
6130
6131 /* Validation of header fields already done in
6132 * mbedtls_ssl_prepare_handshake_record(). */
6133 frag_off = ssl_get_hs_frag_off( ssl );
6134 frag_len = ssl_get_hs_frag_len( ssl );
6135
6136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6137 frag_off, frag_len ) );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006138 mbedtls_platform_memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
Hanno Becker37f95322018-08-16 13:55:32 +01006139
6140 if( hs_buf->is_fragmented )
6141 {
6142 unsigned char * const bitmask = msg + msg_len;
6143 ssl_bitmask_set( bitmask, frag_off, frag_len );
6144 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6145 msg_len ) == 0 );
6146 }
6147 else
6148 {
6149 hs_buf->is_complete = 1;
6150 }
6151
6152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6153 hs_buf->is_complete ? "" : "not yet " ) );
6154 }
6155
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006156 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006157 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006158
6159 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006160 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006161 break;
6162 }
6163
6164exit:
6165
6166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6167 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006168}
6169#endif /* MBEDTLS_SSL_PROTO_DTLS */
6170
Hanno Becker1097b342018-08-15 14:09:41 +01006171static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006172{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006173 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006174 * Consume last content-layer message and potentially
6175 * update in_msglen which keeps track of the contents'
6176 * consumption state.
6177 *
6178 * (1) Handshake messages:
6179 * Remove last handshake message, move content
6180 * and adapt in_msglen.
6181 *
6182 * (2) Alert messages:
6183 * Consume whole record content, in_msglen = 0.
6184 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006185 * (3) Change cipher spec:
6186 * Consume whole record content, in_msglen = 0.
6187 *
6188 * (4) Application data:
6189 * Don't do anything - the record layer provides
6190 * the application data as a stream transport
6191 * and consumes through mbedtls_ssl_read only.
6192 *
6193 */
6194
6195 /* Case (1): Handshake messages */
6196 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006197 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006198 /* Hard assertion to be sure that no application data
6199 * is in flight, as corrupting ssl->in_msglen during
6200 * ssl->in_offt != NULL is fatal. */
6201 if( ssl->in_offt != NULL )
6202 {
6203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6204 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6205 }
6206
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006207 /*
6208 * Get next Handshake message in the current record
6209 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006210
Hanno Becker4a810fb2017-05-24 16:27:30 +01006211 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006212 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006213 * current handshake content: If DTLS handshake
6214 * fragmentation is used, that's the fragment
6215 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006216 * size here is faulty and should be changed at
6217 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006218 * (2) While it doesn't seem to cause problems, one
6219 * has to be very careful not to assume that in_hslen
6220 * is always <= in_msglen in a sensible communication.
6221 * Again, it's wrong for DTLS handshake fragmentation.
6222 * The following check is therefore mandatory, and
6223 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006224 * Additionally, ssl->in_hslen might be arbitrarily out of
6225 * bounds after handling a DTLS message with an unexpected
6226 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006227 */
6228 if( ssl->in_hslen < ssl->in_msglen )
6229 {
6230 ssl->in_msglen -= ssl->in_hslen;
6231 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6232 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006233
Hanno Becker4a810fb2017-05-24 16:27:30 +01006234 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6235 ssl->in_msg, ssl->in_msglen );
6236 }
6237 else
6238 {
6239 ssl->in_msglen = 0;
6240 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006241
Hanno Becker4a810fb2017-05-24 16:27:30 +01006242 ssl->in_hslen = 0;
6243 }
6244 /* Case (4): Application data */
6245 else if( ssl->in_offt != NULL )
6246 {
6247 return( 0 );
6248 }
6249 /* Everything else (CCS & Alerts) */
6250 else
6251 {
6252 ssl->in_msglen = 0;
6253 }
6254
Hanno Becker1097b342018-08-15 14:09:41 +01006255 return( 0 );
6256}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006257
Hanno Beckere74d5562018-08-15 14:26:08 +01006258static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6259{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006260 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006261 return( 1 );
6262
6263 return( 0 );
6264}
6265
Hanno Becker5f066e72018-08-16 14:56:31 +01006266#if defined(MBEDTLS_SSL_PROTO_DTLS)
6267
6268static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6269{
6270 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6271 if( hs == NULL )
6272 return;
6273
Hanno Becker01315ea2018-08-21 17:22:17 +01006274 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006275 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006276 hs->buffering.total_bytes_buffered -=
6277 hs->buffering.future_record.len;
6278
6279 mbedtls_free( hs->buffering.future_record.data );
6280 hs->buffering.future_record.data = NULL;
6281 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006282}
6283
6284static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6285{
6286 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6287 unsigned char * rec;
6288 size_t rec_len;
6289 unsigned rec_epoch;
6290
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006291 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006292 return( 0 );
6293
6294 if( hs == NULL )
6295 return( 0 );
6296
Hanno Becker5f066e72018-08-16 14:56:31 +01006297 rec = hs->buffering.future_record.data;
6298 rec_len = hs->buffering.future_record.len;
6299 rec_epoch = hs->buffering.future_record.epoch;
6300
6301 if( rec == NULL )
6302 return( 0 );
6303
Hanno Becker4cb782d2018-08-20 11:19:05 +01006304 /* Only consider loading future records if the
6305 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006306 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006307 return( 0 );
6308
Hanno Becker5f066e72018-08-16 14:56:31 +01006309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6310
6311 if( rec_epoch != ssl->in_epoch )
6312 {
6313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6314 goto exit;
6315 }
6316
6317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6318
6319 /* Double-check that the record is not too large */
6320 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6321 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6322 {
6323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6324 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6325 }
6326
Teppo Järvelin91d79382019-10-02 09:09:31 +03006327 mbedtls_platform_memcpy( ssl->in_hdr, rec, rec_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006328 ssl->in_left = rec_len;
6329 ssl->next_record_offset = 0;
6330
6331 ssl_free_buffered_record( ssl );
6332
6333exit:
6334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6335 return( 0 );
6336}
6337
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006338static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6339 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006340{
6341 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006342
6343 /* Don't buffer future records outside handshakes. */
6344 if( hs == NULL )
6345 return( 0 );
6346
6347 /* Only buffer handshake records (we are only interested
6348 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006349 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006350 return( 0 );
6351
6352 /* Don't buffer more than one future epoch record. */
6353 if( hs->buffering.future_record.data != NULL )
6354 return( 0 );
6355
Hanno Becker01315ea2018-08-21 17:22:17 +01006356 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006357 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006358 hs->buffering.total_bytes_buffered ) )
6359 {
6360 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 +01006361 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006362 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006363 return( 0 );
6364 }
6365
Hanno Becker5f066e72018-08-16 14:56:31 +01006366 /* Buffer record */
6367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6368 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006369 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006370
6371 /* ssl_parse_record_header() only considers records
6372 * of the next epoch as candidates for buffering. */
6373 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006374 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006375
6376 hs->buffering.future_record.data =
6377 mbedtls_calloc( 1, hs->buffering.future_record.len );
6378 if( hs->buffering.future_record.data == NULL )
6379 {
6380 /* If we run out of RAM trying to buffer a
6381 * record from the next epoch, just ignore. */
6382 return( 0 );
6383 }
6384
Teppo Järvelin91d79382019-10-02 09:09:31 +03006385 mbedtls_platform_memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006386
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006387 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006388 return( 0 );
6389}
6390
6391#endif /* MBEDTLS_SSL_PROTO_DTLS */
6392
Hanno Beckere74d5562018-08-15 14:26:08 +01006393static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006394{
6395 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006396 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006397
Hanno Becker5f066e72018-08-16 14:56:31 +01006398#if defined(MBEDTLS_SSL_PROTO_DTLS)
6399 /* We might have buffered a future record; if so,
6400 * and if the epoch matches now, load it.
6401 * On success, this call will set ssl->in_left to
6402 * the length of the buffered record, so that
6403 * the calls to ssl_fetch_input() below will
6404 * essentially be no-ops. */
6405 ret = ssl_load_buffered_record( ssl );
6406 if( ret != 0 )
6407 return( ret );
6408#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006409
Hanno Becker8b09b732019-05-08 12:03:28 +01006410 /* Ensure that we have enough space available for the default form
6411 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6412 * with no space for CIDs counted in). */
6413 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6414 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006417 return( ret );
6418 }
6419
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006420 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6421 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006423#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006424 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006425 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006426 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6427 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006428 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006429 if( ret != 0 )
6430 return( ret );
6431
6432 /* Fall through to handling of unexpected records */
6433 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6434 }
6435
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006436 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6437 {
Hanno Becker87b56262019-07-10 14:37:41 +01006438#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006439 /* Reset in pointers to default state for TLS/DTLS records,
6440 * assuming no CID and no offset between record content and
6441 * record plaintext. */
6442 ssl_update_in_pointers( ssl );
6443
Hanno Becker69412452019-07-12 08:33:49 +01006444 /* Setup internal message pointers from record structure. */
6445 ssl->in_msgtype = rec.type;
6446#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6447 ssl->in_len = ssl->in_cid + rec.cid_len;
6448#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006449 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006450 ssl->in_msglen = rec.data_len;
6451
Hanno Becker87b56262019-07-10 14:37:41 +01006452 ret = ssl_check_client_reconnect( ssl );
6453 if( ret != 0 )
6454 return( ret );
6455#endif
6456
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006457 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006458 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006459
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6461 "(header)" ) );
6462 }
6463 else
6464 {
6465 /* Skip invalid record and the rest of the datagram */
6466 ssl->next_record_offset = 0;
6467 ssl->in_left = 0;
6468
6469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6470 "(header)" ) );
6471 }
6472
6473 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006474 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006475 }
Hanno Becker87b56262019-07-10 14:37:41 +01006476 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006477#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006478 {
6479 return( ret );
6480 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006481 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006484 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006485 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006486 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006487 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006488 if( ssl->next_record_offset < ssl->in_left )
6489 {
6490 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6491 }
6492 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006493 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006494#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006495#if defined(MBEDTLS_SSL_PROTO_TLS)
6496 {
Hanno Beckere0452772019-07-10 17:12:07 +01006497 /*
6498 * Fetch record contents from underlying transport.
6499 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006500 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006501 if( ret != 0 )
6502 {
6503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6504 return( ret );
6505 }
6506
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006507 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006508 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006509#endif /* MBEDTLS_SSL_PROTO_TLS */
6510
6511 /*
6512 * Decrypt record contents.
6513 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006514
Hanno Beckera89610a2019-07-11 13:07:45 +01006515 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006517#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006518 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006519 {
6520 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006521 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006522 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006523 /* Except when waiting for Finished as a bad mac here
6524 * probably means something went wrong in the handshake
6525 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6526 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6527 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6528 {
6529#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6530 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6531 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006532 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006533 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6534 }
6535#endif
6536 return( ret );
6537 }
6538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006539#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006540 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6541 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6544 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006545 }
6546#endif
6547
Hanno Becker4a810fb2017-05-24 16:27:30 +01006548 /* As above, invalid records cause
6549 * dismissal of the whole datagram. */
6550
6551 ssl->next_record_offset = 0;
6552 ssl->in_left = 0;
6553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006555 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006556 }
6557
6558 return( ret );
6559 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006560 MBEDTLS_SSL_TRANSPORT_ELSE
6561#endif /* MBEDTLS_SSL_PROTO_DTLS */
6562#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006563 {
6564 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006565#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6566 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006567 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006568 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006570 }
6571#endif
6572 return( ret );
6573 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006574#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006575 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006576
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006577
6578 /* Reset in pointers to default state for TLS/DTLS records,
6579 * assuming no CID and no offset between record content and
6580 * record plaintext. */
6581 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006582#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6583 ssl->in_len = ssl->in_cid + rec.cid_len;
6584#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006585 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006586
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006587 /* The record content type may change during decryption,
6588 * so re-read it. */
6589 ssl->in_msgtype = rec.type;
6590 /* Also update the input buffer, because unfortunately
6591 * the server-side ssl_parse_client_hello() reparses the
6592 * record header when receiving a ClientHello initiating
6593 * a renegotiation. */
6594 ssl->in_hdr[0] = rec.type;
6595 ssl->in_msg = rec.buf + rec.data_offset;
6596 ssl->in_msglen = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006597 (void)mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len );
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006598
Manuel Pégourié-Gonnardae48d862020-01-03 12:18:49 +01006599#if defined(MBEDTLS_ZLIB_SUPPORT)
6600 if( ssl->transform_in != NULL &&
6601 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
6602 {
6603 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
6604 {
6605 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
6606 return( ret );
6607 }
6608
6609 /* Check actual (decompress) record content length against
6610 * configured maximum. */
6611 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
6612 {
6613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
6614 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6615 }
6616 }
6617#endif /* MBEDTLS_ZLIB_SUPPORT */
6618
Simon Butcher99000142016-10-13 17:21:01 +01006619 return( 0 );
6620}
6621
6622int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6623{
6624 int ret;
6625
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006626 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006627 * Handle particular types of records
6628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006629 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006630 {
Simon Butcher99000142016-10-13 17:21:01 +01006631 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6632 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006633 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006634 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006635 }
6636
Hanno Beckere678eaa2018-08-21 14:57:46 +01006637 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006638 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006639 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006640 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6642 ssl->in_msglen ) );
6643 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006644 }
6645
Hanno Beckere678eaa2018-08-21 14:57:46 +01006646 if( ssl->in_msg[0] != 1 )
6647 {
6648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6649 ssl->in_msg[0] ) );
6650 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6651 }
6652
6653#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006654 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006655 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6656 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6657 {
6658 if( ssl->handshake == NULL )
6659 {
6660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6661 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6662 }
6663
6664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6665 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6666 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006667#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006668 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006670 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006671 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006672 if( ssl->in_msglen != 2 )
6673 {
6674 /* Note: Standard allows for more than one 2 byte alert
6675 to be packed in a single message, but Mbed TLS doesn't
6676 currently support this. */
6677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6678 ssl->in_msglen ) );
6679 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6680 }
6681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006683 ssl->in_msg[0], ssl->in_msg[1] ) );
6684
6685 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006686 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006687 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006691 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006693 }
6694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006695 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6696 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6699 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006700 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006701
6702#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6703 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6704 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6705 {
Hanno Becker90333da2017-10-10 11:27:13 +01006706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006707 /* Will be handled when trying to parse ServerHello */
6708 return( 0 );
6709 }
6710#endif
6711
6712#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006713 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006714 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6715 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006716 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6717 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6718 {
6719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6720 /* Will be handled in mbedtls_ssl_parse_certificate() */
6721 return( 0 );
6722 }
6723#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6724
6725 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006726 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006727 }
6728
Hanno Beckerc76c6192017-06-06 10:03:17 +01006729#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006730 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006731 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006732 /* Drop unexpected ApplicationData records,
6733 * except at the beginning of renegotiations */
6734 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6735 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6736#if defined(MBEDTLS_SSL_RENEGOTIATION)
6737 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6738 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006739#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006740 )
6741 {
6742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6743 return( MBEDTLS_ERR_SSL_NON_FATAL );
6744 }
6745
6746 if( ssl->handshake != NULL &&
6747 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6748 {
6749 ssl_handshake_wrapup_free_hs_transform( ssl );
6750 }
6751 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006752#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006753
Paul Bakker5121ce52009-01-03 21:22:43 +00006754 return( 0 );
6755}
6756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006757int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006758{
6759 int ret;
6760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006761 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6762 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6763 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006764 {
6765 return( ret );
6766 }
6767
6768 return( 0 );
6769}
6770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006771int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006772 unsigned char level,
6773 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006774{
6775 int ret;
6776
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006777 if( ssl == NULL || ssl->conf == NULL )
6778 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006781 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006784 ssl->out_msglen = 2;
6785 ssl->out_msg[0] = level;
6786 ssl->out_msg[1] = message;
6787
Hanno Becker67bc7c32018-08-06 11:33:50 +01006788 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006791 return( ret );
6792 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006794
6795 return( 0 );
6796}
6797
Hanno Becker17572472019-02-08 07:19:04 +00006798#if defined(MBEDTLS_X509_CRT_PARSE_C)
6799static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6800{
6801#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6802 if( session->peer_cert != NULL )
6803 {
6804 mbedtls_x509_crt_free( session->peer_cert );
6805 mbedtls_free( session->peer_cert );
6806 session->peer_cert = NULL;
6807 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006808#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006809 if( session->peer_cert_digest != NULL )
6810 {
6811 /* Zeroization is not necessary. */
6812 mbedtls_free( session->peer_cert_digest );
6813 session->peer_cert_digest = NULL;
6814 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6815 session->peer_cert_digest_len = 0;
6816 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006817#else
6818 ((void) session);
6819#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006820}
6821#endif /* MBEDTLS_X509_CRT_PARSE_C */
6822
Paul Bakker5121ce52009-01-03 21:22:43 +00006823/*
6824 * Handshake functions
6825 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006826#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006827/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006829{
Hanno Beckerdf645962019-06-26 13:02:22 +01006830 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6831 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006834
Hanno Becker5097cba2019-02-05 13:36:46 +00006835 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006838 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6839 {
6840 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6841 }
6842 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6843 {
6844 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6845 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006846 else
6847 {
6848 ssl->state = MBEDTLS_SSL_INVALID;
6849 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006850 return( 0 );
6851 }
6852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6854 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006855}
6856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006857int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006858{
Hanno Beckerdf645962019-06-26 13:02:22 +01006859 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6860 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006863
Hanno Becker5097cba2019-02-05 13:36:46 +00006864 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006867 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6868 {
6869 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6870 }
6871 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6872 {
6873 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6874 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006875 else
6876 {
6877 ssl->state = MBEDTLS_SSL_INVALID;
6878 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006879 return( 0 );
6880 }
6881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6883 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006884}
Gilles Peskinef9828522017-05-03 12:28:43 +02006885
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006886#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006887/* Some certificate support -> implement write and parse */
6888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006889int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006890{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006891 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006892 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006894 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6895 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006898
Hanno Becker5097cba2019-02-05 13:36:46 +00006899 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006902 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6903 {
6904 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6905 }
6906 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6907 {
6908 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6909 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006910 else
6911 {
6912 ssl->state = MBEDTLS_SSL_INVALID;
6913 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006914 return( 0 );
6915 }
6916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006917#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006918 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6919 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006920 {
6921 if( ssl->client_auth == 0 )
6922 {
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 Bakker5121ce52009-01-03 21:22:43 +00006936 return( 0 );
6937 }
6938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006940 /*
6941 * If using SSLv3 and got no cert, send an Alert message
6942 * (otherwise an empty Certificate message will be sent).
6943 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006944 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006945 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006946 {
6947 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6949 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6950 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006953 goto write_msg;
6954 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006956 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006957#endif /* MBEDTLS_SSL_CLI_C */
6958#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006959 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6964 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006965 }
6966 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006967#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006969 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006970
6971 /*
6972 * 0 . 0 handshake type
6973 * 1 . 3 handshake length
6974 * 4 . 6 length of all certs
6975 * 7 . 9 length of cert. 1
6976 * 10 . n-1 peer certificate
6977 * n . n+2 length of cert. 2
6978 * n+3 . ... upper level cert, etc.
6979 */
6980 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006981 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006982
Paul Bakker29087132010-03-21 21:03:34 +00006983 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006984 {
6985 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006986 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006989 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006990 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006991 }
6992
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006993 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006994
Teppo Järvelin91d79382019-10-02 09:09:31 +03006995 i += 3; mbedtls_platform_memcpy( ssl->out_msg + i, crt->raw.p, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006996 i += n; crt = crt->next;
6997 }
6998
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006999 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007000
7001 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7003 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007004
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007005#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007006write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007007#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007008
Jarno Lamsa2b205162019-11-12 15:36:21 +02007009 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7010 {
7011 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7012 }
7013 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7014 {
7015 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7016 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007017 else
7018 {
7019 ssl->state = MBEDTLS_SSL_INVALID;
7020 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007021
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007022 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007023 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007024 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007025 return( ret );
7026 }
7027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007029
Paul Bakkered27a042013-04-18 22:46:23 +02007030 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007031}
7032
Hanno Becker285ff0c2019-01-31 07:44:03 +00007033#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00007034
7035#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00007036static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7037 unsigned char *crt_buf,
7038 size_t crt_buf_len )
7039{
7040 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7041
7042 if( peer_crt == NULL )
7043 return( -1 );
7044
7045 if( peer_crt->raw.len != crt_buf_len )
7046 return( -1 );
7047
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007048 return( mbedtls_platform_memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007049}
Hanno Becker5882dd02019-06-06 16:25:57 +01007050#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00007051static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7052 unsigned char *crt_buf,
7053 size_t crt_buf_len )
7054{
7055 int ret;
7056 unsigned char const * const peer_cert_digest =
7057 ssl->session->peer_cert_digest;
7058 mbedtls_md_type_t const peer_cert_digest_type =
7059 ssl->session->peer_cert_digest_type;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007060 mbedtls_md_handle_t digest_info =
Hanno Beckerdf759382019-02-05 17:02:46 +00007061 mbedtls_md_info_from_type( peer_cert_digest_type );
7062 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7063 size_t digest_len;
7064
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007065 if( peer_cert_digest == NULL ||
7066 digest_info == MBEDTLS_MD_INVALID_HANDLE )
7067 {
Hanno Beckerdf759382019-02-05 17:02:46 +00007068 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007069 }
Hanno Beckerdf759382019-02-05 17:02:46 +00007070
7071 digest_len = mbedtls_md_get_size( digest_info );
7072 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
7073 return( -1 );
7074
7075 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
7076 if( ret != 0 )
7077 return( -1 );
7078
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007079 return( mbedtls_platform_memcmp( tmp_digest, peer_cert_digest, digest_len ) );
Hanno Beckerdf759382019-02-05 17:02:46 +00007080}
Hanno Becker5882dd02019-06-06 16:25:57 +01007081#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00007082#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00007083
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007084/*
7085 * Once the certificate message is read, parse it into a cert chain and
7086 * perform basic checks, but leave actual verification to the caller
7087 */
Hanno Becker35e41772019-02-05 15:37:23 +00007088static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
7089 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00007090{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007091 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00007092#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7093 int crt_cnt=0;
7094#endif
Paul Bakker23986e52011-04-24 08:57:21 +00007095 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02007096 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00007097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007101 mbedtls_ssl_pend_fatal_alert( ssl,
7102 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007103 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007104 }
7105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007106 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
7107 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007110 mbedtls_ssl_pend_fatal_alert( ssl,
7111 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007113 }
7114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007116
Paul Bakker5121ce52009-01-03 21:22:43 +00007117 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00007119 */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007120 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007121
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007122 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007123 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007126 mbedtls_ssl_pend_fatal_alert( ssl,
7127 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007128 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007129 }
7130
Hanno Becker33c3dc82019-01-30 14:46:46 +00007131 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7132 i += 3;
7133
Hanno Becker33c3dc82019-01-30 14:46:46 +00007134 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007135 while( i < ssl->in_hslen )
7136 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007137 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02007138 if ( i + 3 > ssl->in_hslen ) {
7139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007140 mbedtls_ssl_pend_fatal_alert( ssl,
7141 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02007142 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
7143 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007144 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7145 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007146 if( ssl->in_msg[i] != 0 )
7147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007149 mbedtls_ssl_pend_fatal_alert( ssl,
7150 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007151 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007152 }
7153
Hanno Becker33c3dc82019-01-30 14:46:46 +00007154 /* Read length of the next CRT in the chain. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007155 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007156 i += 3;
7157
7158 if( n < 128 || i + n > ssl->in_hslen )
7159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007161 mbedtls_ssl_pend_fatal_alert( ssl,
7162 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007164 }
7165
Hanno Becker33c3dc82019-01-30 14:46:46 +00007166 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00007167#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7168 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01007169 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7170 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00007171 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007172 {
Hanno Becker68b856d2019-02-08 14:00:04 +00007173 /* During client-side renegotiation, check that the server's
7174 * end-CRTs hasn't changed compared to the initial handshake,
7175 * mitigating the triple handshake attack. On success, reuse
7176 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00007177 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7178 if( ssl_check_peer_crt_unchanged( ssl,
7179 &ssl->in_msg[i],
7180 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007181 {
Hanno Becker35e41772019-02-05 15:37:23 +00007182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007183 mbedtls_ssl_pend_fatal_alert( ssl,
7184 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007185 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007186 }
Hanno Becker35e41772019-02-05 15:37:23 +00007187
7188 /* Now we can safely free the original chain. */
7189 ssl_clear_peer_cert( ssl->session );
7190 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007191#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7192
Hanno Becker33c3dc82019-01-30 14:46:46 +00007193 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007194#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007195 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007196#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007197 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007198 * it in-place from the input buffer instead of making a copy. */
7199 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7200#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007201 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007202 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007203 case 0: /*ok*/
7204 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7205 /* Ignore certificate with an unknown algorithm: maybe a
7206 prior certificate was already trusted. */
7207 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007208
Hanno Becker33c3dc82019-01-30 14:46:46 +00007209 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7210 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7211 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007212
Hanno Becker33c3dc82019-01-30 14:46:46 +00007213 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7214 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7215 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007216
Hanno Becker33c3dc82019-01-30 14:46:46 +00007217 default:
7218 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7219 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007220 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007221 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7222 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007223 }
7224
7225 i += n;
7226 }
7227
Hanno Becker35e41772019-02-05 15:37:23 +00007228 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007229 return( 0 );
7230}
7231
Hanno Beckerb8a08572019-02-05 12:49:06 +00007232#if defined(MBEDTLS_SSL_SRV_C)
7233static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7234{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007235 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007236 return( -1 );
7237
7238#if defined(MBEDTLS_SSL_PROTO_SSL3)
7239 /*
7240 * Check if the client sent an empty certificate
7241 */
Hanno Becker2881d802019-05-22 14:44:53 +01007242 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007243 {
7244 if( ssl->in_msglen == 2 &&
7245 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7246 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7247 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7248 {
7249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7250 return( 0 );
7251 }
7252
7253 return( -1 );
7254 }
7255#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7256
7257#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7258 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7259 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7260 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7261 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Teppo Järvelin0efac532019-10-04 13:21:08 +03007262 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 +00007263 {
7264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7265 return( 0 );
7266 }
7267
Hanno Beckerb8a08572019-02-05 12:49:06 +00007268#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7269 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007270
7271 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007272}
7273#endif /* MBEDTLS_SSL_SRV_C */
7274
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007275/* Check if a certificate message is expected.
7276 * Return either
7277 * - SSL_CERTIFICATE_EXPECTED, or
7278 * - SSL_CERTIFICATE_SKIP
7279 * indicating whether a Certificate message is expected or not.
7280 */
7281#define SSL_CERTIFICATE_EXPECTED 0
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007282#define SSL_CERTIFICATE_SKIP 0xff
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007283static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7284 int authmode )
7285{
Hanno Becker473f98f2019-06-26 10:27:32 +01007286 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007287 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007288
7289 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7290 return( SSL_CERTIFICATE_SKIP );
7291
7292#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007293 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007294 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007295 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7296 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7297 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007298 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007299 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007300
7301 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7302 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007303 ssl->session_negotiate->verify_result =
7304 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7305 return( SSL_CERTIFICATE_SKIP );
7306 }
7307 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007308#else
7309 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007310#endif /* MBEDTLS_SSL_SRV_C */
7311
7312 return( SSL_CERTIFICATE_EXPECTED );
7313}
7314
Hanno Becker3cf50612019-02-05 14:36:34 +00007315static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007316 volatile int authmode,
Hanno Becker3cf50612019-02-05 14:36:34 +00007317 mbedtls_x509_crt *chain,
7318 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007319{
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007320 volatile int verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Andrzej Kurekfd56f402020-05-25 11:52:05 -04007321 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007322 volatile int flow_counter = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01007323 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007324 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007325 mbedtls_x509_crt *ca_chain;
7326 mbedtls_x509_crl *ca_crl;
7327
7328 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
Jarno Lamsae1621d42019-12-19 08:58:56 +02007329 {
Hanno Becker3cf50612019-02-05 14:36:34 +00007330 return( 0 );
Jarno Lamsae1621d42019-12-19 08:58:56 +02007331 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007332
Jarno Lamsae1621d42019-12-19 08:58:56 +02007333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Hanno Becker3cf50612019-02-05 14:36:34 +00007334#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7335 if( ssl->handshake->sni_ca_chain != NULL )
7336 {
7337 ca_chain = ssl->handshake->sni_ca_chain;
7338 ca_crl = ssl->handshake->sni_ca_crl;
7339 }
7340 else
7341#endif
7342 {
7343 ca_chain = ssl->conf->ca_chain;
7344 ca_crl = ssl->conf->ca_crl;
7345 }
7346
7347 /*
7348 * Main check: verify certificate
7349 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007350 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007351 chain,
7352 ca_chain, ca_crl,
7353 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007354#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007355 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007356#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007357 &ssl->session_negotiate->verify_result,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01007358#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
7359 ssl->conf->f_vrfy, ssl->conf->p_vrfy,
7360#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
7361 rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007362
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007363 if( verify_ret == 0 )
7364 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007365 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007366 if( verify_ret == 0 )
7367 {
7368 flow_counter++;
7369 }
7370 else
7371 {
7372 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7373 }
7374 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007375 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007376 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007377 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007378 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007379 }
7380
7381#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007382 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007383 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7384#endif
7385
7386 /*
7387 * Secondary checks: always done, but change 'ret' only if it was 0
7388 */
7389
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007390#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007391 {
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007392#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007393 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007394#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007395 mbedtls_pk_context *pk;
7396 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7397 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007398 {
7399 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007400 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007401 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007402
7403 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007404 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007405 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007406 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007407 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007408
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007409 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007410#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007411
7412 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007413 {
7414 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7415
7416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007417 if( verify_ret == 0 )
7418 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007419 flow_counter++;
7420 }
7421 if( ret == 0 )
7422 {
7423 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007424 }
7425 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007426#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007427
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007428 ret = mbedtls_ssl_check_cert_usage( chain,
Hanno Becker3cf50612019-02-05 14:36:34 +00007429 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007430 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7431 MBEDTLS_SSL_IS_CLIENT ),
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007432 &ssl->session_negotiate->verify_result );
7433 if( ret == 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007434 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007435 flow_counter++;
7436 }
7437 else
7438 {
7439 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007441 if( verify_ret == 0 )
7442 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007443 }
7444
7445 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7446 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7447 * with details encoded in the verification flags. All other kinds
7448 * of error codes, including those from the user provided f_vrfy
7449 * functions, are treated as fatal and lead to a failure of
7450 * ssl_parse_certificate even if verification was optional. */
7451 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007452 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7453 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007454 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007455 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007456 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7457 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7458 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7459 {
7460 verify_ret = 0;
7461 flow_counter++;
7462 }
7463 else
7464 {
7465 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7466 }
7467 } else {
7468 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007469 }
7470
7471 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7472 {
7473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007474 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007475 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007476 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007477 else
7478 {
7479 flow_counter++;
7480 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007481
Hanno Becker8c13ee62019-02-26 16:48:17 +00007482 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007483 {
7484 uint8_t alert;
7485
7486 /* The certificate may have been rejected for several reasons.
7487 Pick one and send the corresponding alert. Which alert to send
7488 may be a subject of debate in some cases. */
7489 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7490 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7491 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7492 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7493 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7494 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7495 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7496 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7497 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7498 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7499 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7500 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7501 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7502 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7503 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7504 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7505 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7506 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7507 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7508 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7509 else
7510 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007511 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007512 }
7513
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007514 if( verify_ret == 0 &&
7515#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7516 flow_counter == 5 )
7517#else
7518 flow_counter == 4 )
7519#endif
7520 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007521 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007522 if( verify_ret == 0 &&
7523#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7524 flow_counter == 5 )
7525#else
7526 flow_counter == 4 )
7527#endif
7528 {
Jarno Lamsae1621d42019-12-19 08:58:56 +02007529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER AUTHENTICATED" ) );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007530 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
7531 }
7532 else
7533 {
7534 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7535 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007536 } else {
7537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER NOT AUTHENTICATED, %d", flow_counter));
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007538 }
7539
Hanno Becker3cf50612019-02-05 14:36:34 +00007540#if defined(MBEDTLS_DEBUG_C)
7541 if( ssl->session_negotiate->verify_result != 0 )
7542 {
7543 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7544 ssl->session_negotiate->verify_result ) );
7545 }
7546 else
7547 {
7548 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7549 }
7550#endif /* MBEDTLS_DEBUG_C */
7551
Hanno Becker8c13ee62019-02-26 16:48:17 +00007552 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007553}
7554
Hanno Becker34106f62019-02-08 14:59:05 +00007555#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007556
7557#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007558static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7559 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007560{
7561 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007562 /* Remember digest of the peer's end-CRT. */
7563 ssl->session_negotiate->peer_cert_digest =
7564 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7565 if( ssl->session_negotiate->peer_cert_digest == NULL )
7566 {
7567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7568 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007569 mbedtls_ssl_pend_fatal_alert( ssl,
7570 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007571
7572 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7573 }
7574
7575 ret = mbedtls_md( mbedtls_md_info_from_type(
7576 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7577 start, len,
7578 ssl->session_negotiate->peer_cert_digest );
7579
7580 ssl->session_negotiate->peer_cert_digest_type =
7581 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7582 ssl->session_negotiate->peer_cert_digest_len =
7583 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7584
7585 return( ret );
7586}
Hanno Becker5882dd02019-06-06 16:25:57 +01007587#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007588
7589static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7590 unsigned char *start, size_t len )
7591{
7592 unsigned char *end = start + len;
7593 int ret;
7594
7595 /* Make a copy of the peer's raw public key. */
7596 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7597 ret = mbedtls_pk_parse_subpubkey( &start, end,
7598 &ssl->handshake->peer_pubkey );
7599 if( ret != 0 )
7600 {
7601 /* We should have parsed the public key before. */
7602 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7603 }
7604
Manuel Pégourié-Gonnard2829bbf2019-09-19 10:45:14 +02007605 ssl->handshake->got_peer_pubkey = 1;
Hanno Becker34106f62019-02-08 14:59:05 +00007606 return( 0 );
7607}
7608#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7609
Hanno Becker3cf50612019-02-05 14:36:34 +00007610int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7611{
7612 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007613 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007614#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7615 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7616 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007617 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007618#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007619 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007620#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007621 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007622 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007623
7624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7625
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007626 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7627 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007628 {
7629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007630 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007631 }
7632
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007633#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7634 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007635 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007636 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007637 chain = ssl->handshake->ecrs_peer_cert;
7638 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007639 goto crt_verify;
7640 }
7641#endif
7642
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007643 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007644 {
7645 /* mbedtls_ssl_read_record may have sent an alert already. We
7646 let it decide whether to alert. */
7647 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007648 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007649 }
7650
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007651#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007652 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7653 {
7654 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007655
Hanno Beckerb8a08572019-02-05 12:49:06 +00007656 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007657 ret = 0;
7658 else
7659 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007660
Hanno Becker613d4902019-02-05 13:11:17 +00007661 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007662 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007663#endif /* MBEDTLS_SSL_SRV_C */
7664
Hanno Becker35e41772019-02-05 15:37:23 +00007665 /* Clear existing peer CRT structure in case we tried to
7666 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007667 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007668
7669 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7670 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007671 {
7672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7673 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007674 mbedtls_ssl_pend_fatal_alert( ssl,
7675 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007676
Hanno Beckere4aeb762019-02-05 17:19:52 +00007677 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7678 goto exit;
7679 }
7680 mbedtls_x509_crt_init( chain );
7681
7682 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007683 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007684 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007685
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007686#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7687 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007688 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007689
7690crt_verify:
7691 if( ssl->handshake->ecrs_enabled)
7692 rs_ctx = &ssl->handshake->ecrs_ctx;
7693#endif
7694
Hanno Becker3cf50612019-02-05 14:36:34 +00007695 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007696 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007697 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007698 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007699
Hanno Becker3008d282019-02-05 17:02:28 +00007700#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007701 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007702 size_t pk_len;
7703 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007704
Hanno Becker34106f62019-02-08 14:59:05 +00007705 /* We parse the CRT chain without copying, so
7706 * these pointers point into the input buffer,
7707 * and are hence still valid after freeing the
7708 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007709
Hanno Becker5882dd02019-06-06 16:25:57 +01007710#if defined(MBEDTLS_SSL_RENEGOTIATION)
7711 unsigned char *crt_start;
7712 size_t crt_len;
7713
Hanno Becker34106f62019-02-08 14:59:05 +00007714 crt_start = chain->raw.p;
7715 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007716#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007717
Hanno Becker8c13ee62019-02-26 16:48:17 +00007718 pk_start = chain->cache->pk_raw.p;
7719 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007720
7721 /* Free the CRT structures before computing
7722 * digest and copying the peer's public key. */
7723 mbedtls_x509_crt_free( chain );
7724 mbedtls_free( chain );
7725 chain = NULL;
7726
Hanno Becker5882dd02019-06-06 16:25:57 +01007727#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007728 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007729 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007730 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007731#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007732
Hanno Becker34106f62019-02-08 14:59:05 +00007733 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007734 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007735 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007736 }
Hanno Becker34106f62019-02-08 14:59:05 +00007737#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7738 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007739 ssl->session_negotiate->peer_cert = chain;
7740 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007741#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007744
Hanno Becker613d4902019-02-05 13:11:17 +00007745exit:
7746
Hanno Beckere4aeb762019-02-05 17:19:52 +00007747 if( ret == 0 )
Jarno Lamsa2b205162019-11-12 15:36:21 +02007748 {
7749 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7750 {
7751 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7752 }
7753 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7754 {
7755 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7756 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007757 else
7758 {
7759 ssl->state = MBEDTLS_SSL_INVALID;
7760 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02007761 }
Hanno Beckere4aeb762019-02-05 17:19:52 +00007762
7763#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7764 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7765 {
7766 ssl->handshake->ecrs_peer_cert = chain;
7767 chain = NULL;
7768 }
7769#endif
7770
7771 if( chain != NULL )
7772 {
7773 mbedtls_x509_crt_free( chain );
7774 mbedtls_free( chain );
7775 }
7776
Paul Bakker5121ce52009-01-03 21:22:43 +00007777 return( ret );
7778}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007779#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007781int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007782{
7783 int ret;
7784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007787 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007788 ssl->out_msglen = 1;
7789 ssl->out_msg[0] = 1;
7790
Jarno Lamsa2b205162019-11-12 15:36:21 +02007791 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7792 {
7793 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7794 }
7795 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7796 {
7797 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7798 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007799 else
7800 {
7801 ssl->state = MBEDTLS_SSL_INVALID;
7802 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007803
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007804 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007805 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007806 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007807 return( ret );
7808 }
7809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007811
7812 return( 0 );
7813}
7814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007816{
7817 int ret;
7818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007820
Hanno Becker327c93b2018-08-15 13:56:18 +01007821 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007824 return( ret );
7825 }
7826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007827 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007830 mbedtls_ssl_pend_fatal_alert( ssl,
7831 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007833 }
7834
Hanno Beckere678eaa2018-08-21 14:57:46 +01007835 /* CCS records are only accepted if they have length 1 and content '1',
7836 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007837
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007838 /*
7839 * Switch to our negotiated transform and session parameters for inbound
7840 * data.
7841 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007843 ssl->transform_in = ssl->transform_negotiate;
7844 ssl->session_in = ssl->session_negotiate;
7845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007847 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007850 ssl_dtls_replay_reset( ssl );
7851#endif
7852
7853 /* Increment epoch */
7854 if( ++ssl->in_epoch == 0 )
7855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007857 /* This is highly unlikely to happen for legitimate reasons, so
7858 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007859 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007860 }
7861 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007862 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007864#if defined(MBEDTLS_SSL_PROTO_TLS)
7865 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02007866 mbedtls_platform_memset( ssl->in_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007867 }
7868#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007869
Hanno Beckerf5970a02019-05-08 09:38:41 +01007870 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7873 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007878 mbedtls_ssl_pend_fatal_alert( ssl,
7879 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007880 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007881 }
7882 }
7883#endif
7884
Jarno Lamsa2b205162019-11-12 15:36:21 +02007885 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7886 {
7887 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7888 }
7889 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7890 {
7891 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7892 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007893 else
7894 {
7895 ssl->state = MBEDTLS_SSL_INVALID;
7896 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007899
7900 return( 0 );
7901}
7902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007904{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7906 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007907 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007908 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007909#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7911#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007912 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007913#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007914#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007915 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007916#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007917#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007918}
7919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007921{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007923
7924 /*
7925 * Free our handshake params
7926 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007927 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007928 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007929 ssl->handshake = NULL;
7930
7931 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007932 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007933 */
7934 if( ssl->transform )
7935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936 mbedtls_ssl_transform_free( ssl->transform );
7937 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007938 }
7939 ssl->transform = ssl->transform_negotiate;
7940 ssl->transform_negotiate = NULL;
7941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007942 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007943}
7944
Jarno Lamsae1621d42019-12-19 08:58:56 +02007945int mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007946{
Andrzej Kurekfd56f402020-05-25 11:52:05 -04007947 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007948
7949#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Jarno Lamsa015aa442019-12-20 12:09:37 +02007950 volatile const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007951 ? ssl->handshake->sni_authmode
7952 : mbedtls_ssl_conf_get_authmode( ssl->conf );
7953#else
Jarno Lamsa015aa442019-12-20 12:09:37 +02007954 volatile const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007955#endif
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007956#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7957 volatile int crt_expected = SSL_CERTIFICATE_EXPECTED;
7958 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7959#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007962#if defined(MBEDTLS_SSL_RENEGOTIATION)
7963 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007966 ssl->renego_records_seen = 0;
7967 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007968#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007969
7970 /*
7971 * Free the previous session and switch in the current one
7972 */
Paul Bakker0a597072012-09-25 21:55:46 +00007973 if( ssl->session )
7974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007976 /* RFC 7366 3.1: keep the EtM state */
7977 ssl->session_negotiate->encrypt_then_mac =
7978 ssl->session->encrypt_then_mac;
7979#endif
7980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981 mbedtls_ssl_session_free( ssl->session );
7982 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007983 }
7984 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007985 ssl->session_negotiate = NULL;
7986
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007987#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007988 /*
7989 * Add cache entry
7990 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007991 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007992 ssl->session->id_len != 0 &&
Jarno Lamsa8d09e572019-12-19 15:20:19 +02007993 ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_UNSET )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007994 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007995 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007997 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007998#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007999
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008000 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8001 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8002#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8003 crt_expected == SSL_CERTIFICATE_SKIP )
8004#else
8005 1 )
8006#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008007 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008008 mbedtls_platform_random_delay();
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008009 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8010 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8011#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8012 crt_expected == SSL_CERTIFICATE_SKIP )
8013#else
8014 1 )
8015#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008016 {
8017 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
8018 }
8019 else
8020 {
8021 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8022 goto cleanup;
8023 }
8024 }
8025
Jarno Lamsae1621d42019-12-19 08:58:56 +02008026#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008027 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008028 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008029 mbedtls_platform_random_delay();
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008030 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008031 {
Jarno Lamsa06164052019-12-19 14:40:36 +02008032 /* When doing session resume, no premaster or peer authentication */
Jarno Lamsae1621d42019-12-19 08:58:56 +02008033 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsa06164052019-12-19 14:40:36 +02008034 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008035 }
8036 else
8037 {
8038 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008039 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008040 }
8041 }
8042#endif
8043
8044 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8045 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008046 mbedtls_platform_random_delay();
Jarno Lamsae1621d42019-12-19 08:58:56 +02008047 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8048 {
8049 ret = 0;
8050 }
8051 else
8052 {
8053 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008054 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008055 }
8056 }
8057 else
8058 {
8059 ret = MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008060 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008061 }
8062
Jarno Lamsa06164052019-12-19 14:40:36 +02008063 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8064 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8065 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8066 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008067 mbedtls_platform_random_delay();
Jarno Lamsa06164052019-12-19 14:40:36 +02008068 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8069 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8070 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8071 {
8072 ret = 0;
8073 }
8074 else
8075 {
8076 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8077 goto cleanup;
8078 }
8079 }
8080 else
8081 {
8082 MBEDTLS_SSL_DEBUG_MSG( 3, ( "hello random %d", ssl->handshake->hello_random_set ) );
8083 MBEDTLS_SSL_DEBUG_MSG( 3, ( "key_derivation_done %d", ssl->handshake->key_derivation_done ) );
8084 MBEDTLS_SSL_DEBUG_MSG( 3, ( "premaster_generated %d", ssl->handshake->premaster_generated ) );
8085 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8086 }
8087
8088cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008089#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008090 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008091 ssl->handshake->flight != NULL )
8092 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008093 /* Cancel handshake timer */
8094 ssl_set_timer( ssl, 0 );
8095
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008096 /* Keep last flight around in case we need to resend it:
8097 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008098 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008099 }
8100 else
8101#endif
8102 ssl_handshake_wrapup_free_hs_transform( ssl );
8103
Jarno Lamsa2b205162019-11-12 15:36:21 +02008104 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Paul Bakker48916f92012-09-16 19:57:18 +00008105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008106 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Jarno Lamsae1621d42019-12-19 08:58:56 +02008107 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00008108}
8109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008110int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00008111{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008112 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00008113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008115
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008116 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01008117
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008118 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8119 mbedtls_ssl_suite_get_mac(
8120 mbedtls_ssl_ciphersuite_from_id(
8121 mbedtls_ssl_session_get_ciphersuite(
8122 ssl->session_negotiate ) ) ),
8123 ssl, ssl->out_msg + 4,
8124 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008125
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01008126 /*
8127 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8128 * may define some other value. Currently (early 2016), no defined
8129 * ciphersuite does this (and this is unlikely to change as activity has
8130 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8131 */
Hanno Becker2881d802019-05-22 14:44:53 +01008132 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008134#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008135 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008136 mbedtls_platform_memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008137#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008138
Paul Bakker5121ce52009-01-03 21:22:43 +00008139 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008140 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8141 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008142
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008143#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00008144 /*
8145 * In case of session resuming, invert the client and server
8146 * ChangeCipherSpec messages order.
8147 */
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008148 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008150#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008151 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8152 MBEDTLS_SSL_IS_CLIENT )
8153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008155 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008156#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008157#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008158 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8159 MBEDTLS_SSL_IS_SERVER )
8160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008162 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008163#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008164 }
8165 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008166#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008167 {
8168 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8169 {
8170 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8171 }
8172 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8173 {
8174 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8175 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008176 else
8177 {
8178 ssl->state = MBEDTLS_SSL_INVALID;
8179 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008180 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008181
Paul Bakker48916f92012-09-16 19:57:18 +00008182 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008183 * Switch to our negotiated transform and session parameters for outbound
8184 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008188#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008189 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008190 {
8191 unsigned char i;
8192
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008193 /* Remember current epoch settings for resending */
8194 ssl->handshake->alt_transform_out = ssl->transform_out;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008195 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008196
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008197 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008198 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008199
8200 /* Increment epoch */
8201 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008202 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008203 break;
8204
8205 /* The loop goes to its end iff the counter is wrapping */
8206 if( i == 0 )
8207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8209 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008210 }
8211 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008212 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008213#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008214#if defined(MBEDTLS_SSL_PROTO_TLS)
8215 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008216 mbedtls_platform_memset( ssl->cur_out_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008217 }
8218#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008219
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008220 ssl->transform_out = ssl->transform_negotiate;
8221 ssl->session_out = ssl->session_negotiate;
8222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008223#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8224 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008226 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008228 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8229 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008230 }
8231 }
8232#endif
8233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008234#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008235 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008236 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008237#endif
8238
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008239 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008240 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008241 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008242 return( ret );
8243 }
8244
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008245#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008246 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008247 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8248 {
8249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8250 return( ret );
8251 }
8252#endif
8253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008255
8256 return( 0 );
8257}
8258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008259#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008260#define SSL_MAX_HASH_LEN 36
8261#else
8262#define SSL_MAX_HASH_LEN 12
8263#endif
8264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008265int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008266{
Paul Bakker23986e52011-04-24 08:57:21 +00008267 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008268 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008269 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008272
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008273 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8274 mbedtls_ssl_suite_get_mac(
8275 mbedtls_ssl_ciphersuite_from_id(
8276 mbedtls_ssl_session_get_ciphersuite(
8277 ssl->session_negotiate ) ) ),
8278 ssl, buf,
8279 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008280
Hanno Becker327c93b2018-08-15 13:56:18 +01008281 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008283 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008284 return( ret );
8285 }
8286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008290 mbedtls_ssl_pend_fatal_alert( ssl,
8291 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008292 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008293 }
8294
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008295 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008296#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01008297 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008298 hash_len = 36;
8299 else
8300#endif
8301 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008303 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8304 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008307 mbedtls_ssl_pend_fatal_alert( ssl,
8308 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008309 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008310 }
8311
Teppo Järvelin707ceb82019-10-04 07:49:39 +03008312 if( mbedtls_platform_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008313 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008316 mbedtls_ssl_pend_fatal_alert( ssl,
8317 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008318 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008319 }
8320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008321#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008322 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008323 mbedtls_platform_memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008324#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008325
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008326#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008327 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008329#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008330 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008332#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008333#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008334 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008335 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008336#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008337 }
8338 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008339#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008340 {
8341 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8342 {
8343 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8344 }
8345 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8346 {
8347 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8348 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008349 else
8350 {
8351 ssl->state = MBEDTLS_SSL_INVALID;
8352 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008353 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008356 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008357 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008358#endif
8359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008361
8362 return( 0 );
8363}
8364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008366{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008367 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008369#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8370 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8371 mbedtls_md5_init( &handshake->fin_md5 );
8372 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008373 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8374 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008375#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008376#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8377#if defined(MBEDTLS_SHA256_C)
8378 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008379 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008380#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008381#if defined(MBEDTLS_SHA512_C)
8382 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008383 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008384#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008385#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008386
Hanno Becker7e5437a2017-04-28 17:15:26 +01008387#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8388 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8389 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8390#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008392#if defined(MBEDTLS_DHM_C)
8393 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008394#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008395#if defined(MBEDTLS_ECDH_C)
8396 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008397#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008398#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008399 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008400#if defined(MBEDTLS_SSL_CLI_C)
8401 handshake->ecjpake_cache = NULL;
8402 handshake->ecjpake_cache_len = 0;
8403#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008404#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008405
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008406#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008407 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008408#endif
8409
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008410#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8411 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8412#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008413
8414#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8415 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8416 mbedtls_pk_init( &handshake->peer_pubkey );
8417#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008418}
8419
Hanno Becker611a83b2018-01-03 14:27:32 +00008420void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008421{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008422 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008424 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8425 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008426
Hanno Becker92231322018-01-03 15:32:51 +00008427#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008428 mbedtls_md_init( &transform->md_ctx_enc );
8429 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008430#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008431}
8432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008433void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008434{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008435 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008436}
8437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008438static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008439{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008440 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008441 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008442 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008443 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008444 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008445 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008446 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008447
8448 /*
8449 * Either the pointers are now NULL or cleared properly and can be freed.
8450 * Now allocate missing structures.
8451 */
8452 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008453 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008454 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008455 }
Paul Bakker48916f92012-09-16 19:57:18 +00008456
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008457 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008458 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008459 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008460 }
Paul Bakker48916f92012-09-16 19:57:18 +00008461
Paul Bakker82788fb2014-10-20 13:59:19 +02008462 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008463 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008464 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008465 }
Paul Bakker48916f92012-09-16 19:57:18 +00008466
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008467 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008468 if( ssl->handshake == NULL ||
8469 ssl->transform_negotiate == NULL ||
8470 ssl->session_negotiate == NULL )
8471 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008474 mbedtls_free( ssl->handshake );
8475 mbedtls_free( ssl->transform_negotiate );
8476 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008477
8478 ssl->handshake = NULL;
8479 ssl->transform_negotiate = NULL;
8480 ssl->session_negotiate = NULL;
8481
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008482 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008483 }
8484
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008485 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008486 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008487 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008488 ssl_handshake_params_init( ssl->handshake );
8489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008490#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008491 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008492 {
8493 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008494
Hanno Becker2d9623f2019-06-13 12:07:05 +01008495 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008496 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8497 else
8498 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8499 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008500#endif
8501
Paul Bakker48916f92012-09-16 19:57:18 +00008502 return( 0 );
8503}
8504
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008505#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008506/* Dummy cookie callbacks for defaults */
8507static int ssl_cookie_write_dummy( void *ctx,
8508 unsigned char **p, unsigned char *end,
8509 const unsigned char *cli_id, size_t cli_id_len )
8510{
8511 ((void) ctx);
8512 ((void) p);
8513 ((void) end);
8514 ((void) cli_id);
8515 ((void) cli_id_len);
8516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008517 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008518}
8519
8520static int ssl_cookie_check_dummy( void *ctx,
8521 const unsigned char *cookie, size_t cookie_len,
8522 const unsigned char *cli_id, size_t cli_id_len )
8523{
8524 ((void) ctx);
8525 ((void) cookie);
8526 ((void) cookie_len);
8527 ((void) cli_id);
8528 ((void) cli_id_len);
8529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008530 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008531}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008532#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008533
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008534/* Once ssl->out_hdr as the address of the beginning of the
8535 * next outgoing record is set, deduce the other pointers.
8536 *
8537 * Note: For TLS, we save the implicit record sequence number
8538 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8539 * and the caller has to make sure there's space for this.
8540 */
8541
8542static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8543 mbedtls_ssl_transform *transform )
8544{
8545#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008546 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008547 {
8548 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008549#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008550 ssl->out_cid = ssl->out_ctr + 8;
8551 ssl->out_len = ssl->out_cid;
8552 if( transform != NULL )
8553 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008554#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008555 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008556#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008557 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008558 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008559 MBEDTLS_SSL_TRANSPORT_ELSE
8560#endif /* MBEDTLS_SSL_PROTO_DTLS */
8561#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008562 {
8563 ssl->out_ctr = ssl->out_hdr - 8;
8564 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008565#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008566 ssl->out_cid = ssl->out_len;
8567#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008568 ssl->out_iv = ssl->out_hdr + 5;
8569 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008570#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008571
8572 /* Adjust out_msg to make space for explicit IV, if used. */
8573 if( transform != NULL &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008574 mbedtls_ssl_ver_geq(
8575 mbedtls_ssl_get_minor_ver( ssl ),
8576 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008577 {
8578 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8579 }
8580 else
8581 ssl->out_msg = ssl->out_iv;
8582}
8583
8584/* Once ssl->in_hdr as the address of the beginning of the
8585 * next incoming record is set, deduce the other pointers.
8586 *
8587 * Note: For TLS, we save the implicit record sequence number
8588 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8589 * and the caller has to make sure there's space for this.
8590 */
8591
Hanno Beckerf5970a02019-05-08 09:38:41 +01008592static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008593{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008594 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008595 * of unprotected TLS/DTLS records, with ssl->in_msg
8596 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008597 *
8598 * When decrypting a protected record, ssl->in_msg
8599 * will be shifted to point to the beginning of the
8600 * record plaintext.
8601 */
8602
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008603#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008604 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008605 {
Hanno Becker70e79282019-05-03 14:34:53 +01008606 /* This sets the header pointers to match records
8607 * without CID. When we receive a record containing
8608 * a CID, the fields are shifted accordingly in
8609 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008610 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008611#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008612 ssl->in_cid = ssl->in_ctr + 8;
8613 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008614#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008615 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008616#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008617 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008618 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008619 MBEDTLS_SSL_TRANSPORT_ELSE
8620#endif /* MBEDTLS_SSL_PROTO_DTLS */
8621#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008622 {
8623 ssl->in_ctr = ssl->in_hdr - 8;
8624 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008625#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008626 ssl->in_cid = ssl->in_len;
8627#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008628 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008629 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008630#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008631}
8632
Paul Bakker5121ce52009-01-03 21:22:43 +00008633/*
8634 * Initialize an SSL context
8635 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008636void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8637{
8638 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8639}
8640
8641/*
8642 * Setup an SSL context
8643 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008644
8645static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8646{
8647 /* Set the incoming and outgoing record pointers. */
8648#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008649 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008650 {
8651 ssl->out_hdr = ssl->out_buf;
8652 ssl->in_hdr = ssl->in_buf;
8653 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008654 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008655#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008656#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008657 {
8658 ssl->out_hdr = ssl->out_buf + 8;
8659 ssl->in_hdr = ssl->in_buf + 8;
8660 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008661#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008662
8663 /* Derive other internal pointers. */
8664 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008665 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008666}
8667
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008668int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008669 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008670{
Paul Bakker48916f92012-09-16 19:57:18 +00008671 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008672
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008673 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008674
Hanno Beckeref982d52019-07-23 15:56:18 +01008675#if defined(MBEDTLS_USE_TINYCRYPT)
8676 uECC_set_rng( &uecc_rng_wrapper );
8677#endif
8678
Paul Bakker62f2dee2012-09-28 07:31:51 +00008679 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008680 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008681 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008682
8683 /* Set to NULL in case of an error condition */
8684 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008685
Angus Grattond8213d02016-05-25 20:56:48 +10008686 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8687 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008688 {
Angus Grattond8213d02016-05-25 20:56:48 +10008689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008690 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008691 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008692 }
8693
8694 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8695 if( ssl->out_buf == NULL )
8696 {
8697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008698 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008699 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008700 }
8701
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008702 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008703
Paul Bakker48916f92012-09-16 19:57:18 +00008704 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008705 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008706
Hanno Beckerc8f52992019-07-25 11:15:08 +01008707 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008708
Paul Bakker5121ce52009-01-03 21:22:43 +00008709 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008710
8711error:
8712 mbedtls_free( ssl->in_buf );
8713 mbedtls_free( ssl->out_buf );
8714
8715 ssl->conf = NULL;
8716
8717 ssl->in_buf = NULL;
8718 ssl->out_buf = NULL;
8719
8720 ssl->in_hdr = NULL;
8721 ssl->in_ctr = NULL;
8722 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008723 ssl->in_msg = NULL;
8724
8725 ssl->out_hdr = NULL;
8726 ssl->out_ctr = NULL;
8727 ssl->out_len = NULL;
8728 ssl->out_iv = NULL;
8729 ssl->out_msg = NULL;
8730
k-stachowiak9f7798e2018-07-31 16:52:32 +02008731 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008732}
8733
8734/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008735 * Reset an initialized and used SSL context for re-use while retaining
8736 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008737 *
8738 * If partial is non-zero, keep data in the input buffer and client ID.
8739 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008740 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008741static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008742{
Paul Bakker48916f92012-09-16 19:57:18 +00008743 int ret;
8744
Hanno Becker7e772132018-08-10 12:38:21 +01008745#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8746 !defined(MBEDTLS_SSL_SRV_C)
8747 ((void) partial);
8748#endif
8749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008750 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008751
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008752 /* Cancel any possibly running timer */
8753 ssl_set_timer( ssl, 0 );
8754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008755#if defined(MBEDTLS_SSL_RENEGOTIATION)
8756 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008757 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008758
8759 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008760 mbedtls_platform_memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8761 mbedtls_platform_memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008762#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008763 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008764
Paul Bakker7eb013f2011-10-06 12:37:39 +00008765 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008766 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008767
8768 ssl->in_msgtype = 0;
8769 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008771 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008772 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008773#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008774#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008775 ssl_dtls_replay_reset( ssl );
8776#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008777
8778 ssl->in_hslen = 0;
8779 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008780
8781 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008782
8783 ssl->out_msgtype = 0;
8784 ssl->out_msglen = 0;
8785 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008786#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8787 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008788 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008789#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008790
Hanno Becker19859472018-08-06 09:40:20 +01008791 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8792
Paul Bakker48916f92012-09-16 19:57:18 +00008793 ssl->transform_in = NULL;
8794 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008795
Hanno Becker78640902018-08-13 16:35:15 +01008796 ssl->session_in = NULL;
8797 ssl->session_out = NULL;
8798
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008799 mbedtls_platform_memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008800
8801#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008802 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008803#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8804 {
8805 ssl->in_left = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008806 mbedtls_platform_memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008807 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008809#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8810 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8813 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8816 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008817 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008818 }
8819#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008820
Paul Bakker48916f92012-09-16 19:57:18 +00008821 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008823 mbedtls_ssl_transform_free( ssl->transform );
8824 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008825 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008826 }
Paul Bakker48916f92012-09-16 19:57:18 +00008827
Paul Bakkerc0463502013-02-14 11:19:38 +01008828 if( ssl->session )
8829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008830 mbedtls_ssl_session_free( ssl->session );
8831 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008832 ssl->session = NULL;
8833 }
8834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008835#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008836 ssl->alpn_chosen = NULL;
8837#endif
8838
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008839#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008840#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008841 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008842#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008843 {
8844 mbedtls_free( ssl->cli_id );
8845 ssl->cli_id = NULL;
8846 ssl->cli_id_len = 0;
8847 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008848#endif
8849
Paul Bakker48916f92012-09-16 19:57:18 +00008850 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8851 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008852
8853 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008854}
8855
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008856/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008857 * Reset an initialized and used SSL context for re-use while retaining
8858 * all application-set variables, function pointers and data.
8859 */
8860int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8861{
8862 return( ssl_session_reset_int( ssl, 0 ) );
8863}
8864
8865/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008866 * SSL set accessors
8867 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008868#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008869void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008870{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008871 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008872}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008873#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008874
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008875void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008876{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008877 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008878}
8879
Hanno Becker7f376f42019-06-12 16:20:48 +01008880#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8881 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008882void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008883{
Hanno Becker7f376f42019-06-12 16:20:48 +01008884 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008885}
Hanno Becker7f376f42019-06-12 16:20:48 +01008886#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008887
Hanno Beckerde671542019-06-12 16:30:46 +01008888#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8889 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8890void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8891 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008892{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008893 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008894}
Hanno Beckerde671542019-06-12 16:30:46 +01008895#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008897#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008898
Hanno Becker1841b0a2018-08-24 11:13:57 +01008899void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8900 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008901{
8902 ssl->disable_datagram_packing = !allow_packing;
8903}
8904
Hanno Becker1f835fa2019-06-13 10:14:59 +01008905#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8906 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008907void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8908 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008909{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008910 conf->hs_timeout_min = min;
8911 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008912}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008913#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8914 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8915void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8916 uint32_t min, uint32_t max )
8917{
8918 ((void) conf);
8919 ((void) min);
8920 ((void) max);
8921}
8922#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8923 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8924
8925#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008926
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008927void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008928{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008929#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8930 conf->authmode = authmode;
8931#else
8932 ((void) conf);
8933 ((void) authmode);
8934#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008935}
8936
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008937#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8938 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008939void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008940 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008941 void *p_vrfy )
8942{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008943 conf->f_vrfy = f_vrfy;
8944 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008945}
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008946#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008947
Hanno Beckerece325c2019-06-13 15:39:27 +01008948#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008949void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008950 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008951 void *p_rng )
8952{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008953 conf->f_rng = f_rng;
8954 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008955}
Hanno Beckerece325c2019-06-13 15:39:27 +01008956#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008957
Hanno Becker14a4a442019-07-02 17:00:34 +01008958#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008959void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008960 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008961 void *p_dbg )
8962{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008963 conf->f_dbg = f_dbg;
8964 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008965}
Hanno Becker14a4a442019-07-02 17:00:34 +01008966#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008967
Hanno Beckera58a8962019-06-13 16:11:15 +01008968#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8969 !defined(MBEDTLS_SSL_CONF_SEND) && \
8970 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008971void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008972 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008973 mbedtls_ssl_send_t *f_send,
8974 mbedtls_ssl_recv_t *f_recv,
8975 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008976{
Hanno Beckera58a8962019-06-13 16:11:15 +01008977 ssl->p_bio = p_bio;
8978 ssl->f_send = f_send;
8979 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008980 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008981}
Hanno Beckera58a8962019-06-13 16:11:15 +01008982#else
8983void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8984 void *p_bio )
8985{
8986 ssl->p_bio = p_bio;
8987}
8988#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008989
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008990#if defined(MBEDTLS_SSL_PROTO_DTLS)
8991void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8992{
8993 ssl->mtu = mtu;
8994}
8995#endif
8996
Hanno Becker1f835fa2019-06-13 10:14:59 +01008997#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008998void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008999{
9000 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009001}
Hanno Becker1f835fa2019-06-13 10:14:59 +01009002#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009003
Hanno Becker0ae6b242019-06-13 16:45:36 +01009004#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
9005 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009006void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
9007 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00009008 mbedtls_ssl_set_timer_t *f_set_timer,
9009 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009010{
9011 ssl->p_timer = p_timer;
9012 ssl->f_set_timer = f_set_timer;
9013 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009014 /* Make sure we start with no timer running */
9015 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009016}
Hanno Becker0ae6b242019-06-13 16:45:36 +01009017#else
9018void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
9019 void *p_timer )
9020{
9021 ssl->p_timer = p_timer;
9022 /* Make sure we start with no timer running */
9023 ssl_set_timer( ssl, 0 );
9024}
9025#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009026
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009027#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009028void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009029 void *p_cache,
9030 int (*f_get_cache)(void *, mbedtls_ssl_session *),
9031 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00009032{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009033 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009034 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009035 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00009036}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009037#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009038
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009039#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009040int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00009041{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009042 int ret;
9043
9044 if( ssl == NULL ||
9045 session == NULL ||
9046 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009047 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009049 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009050 }
9051
Hanno Becker58fccf22019-02-06 14:30:46 +00009052 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
9053 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009054 return( ret );
9055
Jarno Lamsa8d09e572019-12-19 15:20:19 +02009056 ssl->handshake->resume = MBEDTLS_SSL_FI_FLAG_SET;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009057
9058 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009059}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009060#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009061
Hanno Becker73f4cb12019-06-27 13:51:07 +01009062#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009063void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009064 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00009065{
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009066 conf->ciphersuite_list[0] = ciphersuites;
9067 conf->ciphersuite_list[1] = ciphersuites;
9068 conf->ciphersuite_list[2] = ciphersuites;
9069 conf->ciphersuite_list[3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009070}
9071
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009072void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02009073 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009074 int major, int minor )
9075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009076 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009077 return;
9078
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009079 if( mbedtls_ssl_ver_lt( minor, MBEDTLS_SSL_MINOR_VERSION_0 ) ||
9080 mbedtls_ssl_ver_gt( minor, MBEDTLS_SSL_MINOR_VERSION_3 ) )
9081 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009082 return;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009083 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009084
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009085 conf->ciphersuite_list[mbedtls_ssl_minor_ver_index( minor )] =
9086 ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00009087}
Hanno Becker73f4cb12019-06-27 13:51:07 +01009088#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009090#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009091void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01009092 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009093{
9094 conf->cert_profile = profile;
9095}
9096
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009097/* Append a new keycert entry to a (possibly empty) list */
9098static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
9099 mbedtls_x509_crt *cert,
9100 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009101{
niisato8ee24222018-06-25 19:05:48 +09009102 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009103
niisato8ee24222018-06-25 19:05:48 +09009104 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
9105 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009106 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009107
niisato8ee24222018-06-25 19:05:48 +09009108 new_cert->cert = cert;
9109 new_cert->key = key;
9110 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009111
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009112 /* Update head is the list was null, else add to the end */
9113 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01009114 {
niisato8ee24222018-06-25 19:05:48 +09009115 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01009116 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009117 else
9118 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009119 mbedtls_ssl_key_cert *cur = *head;
9120 while( cur->next != NULL )
9121 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09009122 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009123 }
9124
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009125 return( 0 );
9126}
9127
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009128int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009129 mbedtls_x509_crt *own_cert,
9130 mbedtls_pk_context *pk_key )
9131{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02009132 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009133}
9134
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009135void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009136 mbedtls_x509_crt *ca_chain,
9137 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009138{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009139 conf->ca_chain = ca_chain;
9140 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00009141}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009142#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00009143
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009144#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9145int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
9146 mbedtls_x509_crt *own_cert,
9147 mbedtls_pk_context *pk_key )
9148{
9149 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
9150 own_cert, pk_key ) );
9151}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02009152
9153void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
9154 mbedtls_x509_crt *ca_chain,
9155 mbedtls_x509_crl *ca_crl )
9156{
9157 ssl->handshake->sni_ca_chain = ca_chain;
9158 ssl->handshake->sni_ca_crl = ca_crl;
9159}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02009160
9161void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
9162 int authmode )
9163{
9164 ssl->handshake->sni_authmode = authmode;
9165}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009166#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9167
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009168#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009169/*
9170 * Set EC J-PAKE password for current handshake
9171 */
9172int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
9173 const unsigned char *pw,
9174 size_t pw_len )
9175{
9176 mbedtls_ecjpake_role role;
9177
Janos Follath8eb64132016-06-03 15:40:57 +01009178 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009179 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9180
Hanno Becker2d9623f2019-06-13 12:07:05 +01009181 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009182 role = MBEDTLS_ECJPAKE_SERVER;
9183 else
9184 role = MBEDTLS_ECJPAKE_CLIENT;
9185
9186 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
9187 role,
9188 MBEDTLS_MD_SHA256,
9189 MBEDTLS_ECP_DP_SECP256R1,
9190 pw, pw_len ) );
9191}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009192#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009194#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009195int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009196 const unsigned char *psk, size_t psk_len,
9197 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009198{
Paul Bakker6db455e2013-09-18 17:29:31 +02009199 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009200 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02009201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009202 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9203 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01009204
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009205 /* Identity len will be encoded on two bytes */
9206 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009207 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009208 {
9209 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9210 }
9211
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009212 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02009213 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009214 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009215
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009216 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009217 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009218 conf->psk_len = 0;
9219 }
9220 if( conf->psk_identity != NULL )
9221 {
9222 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009223 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009224 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02009225 }
9226
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009227 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
9228 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05009229 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009230 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009231 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009232 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009233 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009234 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05009235 }
Paul Bakker6db455e2013-09-18 17:29:31 +02009236
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009237 conf->psk_len = psk_len;
9238 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02009239
Teppo Järvelin91d79382019-10-02 09:09:31 +03009240 mbedtls_platform_memcpy( conf->psk, psk, conf->psk_len );
9241 mbedtls_platform_memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02009242
9243 return( 0 );
9244}
9245
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009246int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9247 const unsigned char *psk, size_t psk_len )
9248{
9249 if( psk == NULL || ssl->handshake == NULL )
9250 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9251
9252 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9253 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9254
9255 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009256 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009257 mbedtls_platform_zeroize( ssl->handshake->psk,
9258 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01009259 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009260 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009261 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009262
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009263 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009264 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009265
9266 ssl->handshake->psk_len = psk_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03009267 mbedtls_platform_memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009268
9269 return( 0 );
9270}
9271
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009272void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009273 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009274 size_t),
9275 void *p_psk )
9276{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009277 conf->f_psk = f_psk;
9278 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009279}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009280#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009281
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009282#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009283
9284#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009285int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009286{
9287 int ret;
9288
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009289 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9290 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9291 {
9292 mbedtls_mpi_free( &conf->dhm_P );
9293 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009294 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009295 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009296
9297 return( 0 );
9298}
Hanno Becker470a8c42017-10-04 15:28:46 +01009299#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009300
Hanno Beckera90658f2017-10-04 15:29:08 +01009301int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9302 const unsigned char *dhm_P, size_t P_len,
9303 const unsigned char *dhm_G, size_t G_len )
9304{
9305 int ret;
9306
9307 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9308 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9309 {
9310 mbedtls_mpi_free( &conf->dhm_P );
9311 mbedtls_mpi_free( &conf->dhm_G );
9312 return( ret );
9313 }
9314
9315 return( 0 );
9316}
Paul Bakker5121ce52009-01-03 21:22:43 +00009317
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009318int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009319{
9320 int ret;
9321
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009322 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9323 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9324 {
9325 mbedtls_mpi_free( &conf->dhm_P );
9326 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009327 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009328 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009329
9330 return( 0 );
9331}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009332#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009333
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009334#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9335/*
9336 * Set the minimum length for Diffie-Hellman parameters
9337 */
9338void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9339 unsigned int bitlen )
9340{
9341 conf->dhm_min_bitlen = bitlen;
9342}
9343#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9344
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009345#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009346/*
9347 * Set allowed/preferred hashes for handshake signatures
9348 */
9349void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9350 const int *hashes )
9351{
Hanno Becker56595f42019-06-19 16:31:38 +01009352#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009353 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01009354#else
9355 ((void) conf);
9356 ((void) hashes);
9357#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009358}
Hanno Becker947194e2017-04-07 13:25:49 +01009359#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009360
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009361#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01009362#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009363/*
9364 * Set the allowed elliptic curves
9365 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009366void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009367 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009368{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009369 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009370}
Hanno Beckerc1096e72019-06-19 12:30:41 +01009371#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01009372#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009373
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009374#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009375int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009376{
Hanno Becker947194e2017-04-07 13:25:49 +01009377 /* Initialize to suppress unnecessary compiler warning */
9378 size_t hostname_len = 0;
9379
9380 /* Check if new hostname is valid before
9381 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009382 if( hostname != NULL )
9383 {
9384 hostname_len = strlen( hostname );
9385
9386 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9387 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9388 }
9389
9390 /* Now it's clear that we will overwrite the old hostname,
9391 * so we can free it safely */
9392
9393 if( ssl->hostname != NULL )
9394 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009395 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009396 mbedtls_free( ssl->hostname );
9397 }
9398
9399 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009400
Paul Bakker5121ce52009-01-03 21:22:43 +00009401 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009402 {
9403 ssl->hostname = NULL;
9404 }
9405 else
9406 {
9407 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009408 if( ssl->hostname == NULL )
9409 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009410
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03009411 /* Not using more secure mbedtls_platform_memcpy as hostname is public in initial handshake */
9412 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009413
Hanno Becker947194e2017-04-07 13:25:49 +01009414 ssl->hostname[hostname_len] = '\0';
9415 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009416
9417 return( 0 );
9418}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009419#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009420
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009421#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009422void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009423 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009424 const unsigned char *, size_t),
9425 void *p_sni )
9426{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009427 conf->f_sni = f_sni;
9428 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009429}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009430#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009432#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009433int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009434{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009435 size_t cur_len, tot_len;
9436 const char **p;
9437
9438 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009439 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9440 * MUST NOT be truncated."
9441 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009442 */
9443 tot_len = 0;
9444 for( p = protos; *p != NULL; p++ )
9445 {
9446 cur_len = strlen( *p );
9447 tot_len += cur_len;
9448
9449 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009450 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009451 }
9452
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009453 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009454
9455 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009456}
9457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009458const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009459{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009460 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009461}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009462#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009463
Hanno Becker33b9b252019-07-05 11:23:25 +01009464#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9465 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9466void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9467 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009468{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009469 conf->max_major_ver = major;
9470 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009471}
Hanno Becker33b9b252019-07-05 11:23:25 +01009472#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9473 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009474
Hanno Becker33b9b252019-07-05 11:23:25 +01009475#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9476 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9477void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9478 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009479{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009480 conf->min_major_ver = major;
9481 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009482}
Hanno Becker33b9b252019-07-05 11:23:25 +01009483#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9484 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009487void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009488{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009489 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009490}
9491#endif
9492
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009493#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009494void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9495 char cert_req_ca_list )
9496{
9497 conf->cert_req_ca_list = cert_req_ca_list;
9498}
9499#endif
9500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009502void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009503{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009504 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009505}
9506#endif
9507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009508#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009509#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009510void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009511{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009512 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009513}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009514#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9515#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009516void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009517 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009518{
9519 conf->enforce_extended_master_secret = ems_enf;
9520}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009521#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009522#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009523
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009524#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009525void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009526{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009527 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009528}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009529#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009532int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009533{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009535 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009537 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009538 }
9539
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009540 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009541
9542 return( 0 );
9543}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009544#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009547void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009548{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009549 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009550}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009551#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009553#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009554void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009555{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009556 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009557}
9558#endif
9559
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009560#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009561void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009562{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009563 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009564}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009565#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009567#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009568void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009569{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009570 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009571}
9572
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009573void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009574{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009575 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009576}
9577
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009578void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009579 const unsigned char period[8] )
9580{
Teppo Järvelin91d79382019-10-02 09:09:31 +03009581 mbedtls_platform_memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009582}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009583#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009585#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009586#if defined(MBEDTLS_SSL_CLI_C)
9587void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009588{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009589 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009590}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009591#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009592
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009593#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009594void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9595 mbedtls_ssl_ticket_write_t *f_ticket_write,
9596 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9597 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009598{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009599 conf->f_ticket_write = f_ticket_write;
9600 conf->f_ticket_parse = f_ticket_parse;
9601 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009602}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009603#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009605
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009606#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9607void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9608 mbedtls_ssl_export_keys_t *f_export_keys,
9609 void *p_export_keys )
9610{
9611 conf->f_export_keys = f_export_keys;
9612 conf->p_export_keys = p_export_keys;
9613}
9614#endif
9615
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009616#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009617void mbedtls_ssl_conf_async_private_cb(
9618 mbedtls_ssl_config *conf,
9619 mbedtls_ssl_async_sign_t *f_async_sign,
9620 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9621 mbedtls_ssl_async_resume_t *f_async_resume,
9622 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009623 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009624{
9625 conf->f_async_sign_start = f_async_sign;
9626 conf->f_async_decrypt_start = f_async_decrypt;
9627 conf->f_async_resume = f_async_resume;
9628 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009629 conf->p_async_config_data = async_config_data;
9630}
9631
Gilles Peskine8f97af72018-04-26 11:46:10 +02009632void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9633{
9634 return( conf->p_async_config_data );
9635}
9636
Gilles Peskine1febfef2018-04-30 11:54:39 +02009637void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009638{
9639 if( ssl->handshake == NULL )
9640 return( NULL );
9641 else
9642 return( ssl->handshake->user_async_ctx );
9643}
9644
Gilles Peskine1febfef2018-04-30 11:54:39 +02009645void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009646 void *ctx )
9647{
9648 if( ssl->handshake != NULL )
9649 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009650}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009651#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009652
Paul Bakker5121ce52009-01-03 21:22:43 +00009653/*
9654 * SSL get accessors
9655 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009656size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009657{
9658 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9659}
9660
Hanno Becker8b170a02017-10-10 11:51:19 +01009661int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9662{
9663 /*
9664 * Case A: We're currently holding back
9665 * a message for further processing.
9666 */
9667
9668 if( ssl->keep_current_message == 1 )
9669 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009670 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009671 return( 1 );
9672 }
9673
9674 /*
9675 * Case B: Further records are pending in the current datagram.
9676 */
9677
9678#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009679 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009680 ssl->in_left > ssl->next_record_offset )
9681 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009682 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009683 return( 1 );
9684 }
9685#endif /* MBEDTLS_SSL_PROTO_DTLS */
9686
9687 /*
9688 * Case C: A handshake message is being processed.
9689 */
9690
Hanno Becker8b170a02017-10-10 11:51:19 +01009691 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9692 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009693 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009694 return( 1 );
9695 }
9696
9697 /*
9698 * Case D: An application data message is being processed
9699 */
9700 if( ssl->in_offt != NULL )
9701 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009702 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009703 return( 1 );
9704 }
9705
9706 /*
9707 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009708 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009709 * we implement support for multiple alerts in single records.
9710 */
9711
9712 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9713 return( 0 );
9714}
9715
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009716uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009717{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009718 if( ssl->session != NULL )
9719 return( ssl->session->verify_result );
9720
9721 if( ssl->session_negotiate != NULL )
9722 return( ssl->session_negotiate->verify_result );
9723
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009724 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009725}
9726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009727const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009728{
Hanno Beckere02758c2019-06-26 15:31:31 +01009729 int suite;
9730
Paul Bakker926c8e42013-03-06 10:23:34 +01009731 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009732 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009733
Hanno Beckere02758c2019-06-26 15:31:31 +01009734 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9735 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009736}
9737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009738const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009739{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009740#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009741 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009742 {
Hanno Becker2881d802019-05-22 14:44:53 +01009743 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009745 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009746 return( "DTLSv1.0" );
9747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009748 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009749 return( "DTLSv1.2" );
9750
9751 default:
9752 return( "unknown (DTLS)" );
9753 }
9754 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009755 MBEDTLS_SSL_TRANSPORT_ELSE
9756#endif /* MBEDTLS_SSL_PROTO_DTLS */
9757#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009758 {
Hanno Becker2881d802019-05-22 14:44:53 +01009759 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009760 {
9761 case MBEDTLS_SSL_MINOR_VERSION_0:
9762 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009763
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009764 case MBEDTLS_SSL_MINOR_VERSION_1:
9765 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009766
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009767 case MBEDTLS_SSL_MINOR_VERSION_2:
9768 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009769
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009770 case MBEDTLS_SSL_MINOR_VERSION_3:
9771 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009772
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009773 default:
9774 return( "unknown" );
9775 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009776 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009777#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009778}
9779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009780int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009781{
Hanno Becker3136ede2018-08-17 15:28:19 +01009782 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009783 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009784
Hanno Becker43395762019-05-03 14:46:38 +01009785 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9786
Hanno Becker78640902018-08-13 16:35:15 +01009787 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009788 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009790#if defined(MBEDTLS_ZLIB_SUPPORT)
9791 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9792 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009793#endif
9794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009795 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009796 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009797#if defined(MBEDTLS_GCM_C) || \
9798 defined(MBEDTLS_CCM_C) || \
9799 defined(MBEDTLS_CHACHAPOLY_C)
9800#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009802#endif
9803#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009804 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009805#endif
9806#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009807 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009808#endif
9809 transform_expansion =
9810 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009811 break;
9812
Hanno Beckera9d5c452019-07-25 16:47:12 +01009813#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9814 MBEDTLS_CHACHAPOLY_C */
9815
9816#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9817 case MBEDTLS_MODE_STREAM:
9818 transform_expansion = transform->maclen;
9819 break;
9820#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9821
9822#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009823 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009824 {
9825 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009826
9827 block_size = mbedtls_cipher_get_block_size(
9828 &transform->cipher_ctx_enc );
9829
Hanno Becker3136ede2018-08-17 15:28:19 +01009830 /* Expansion due to the addition of the MAC. */
9831 transform_expansion += transform->maclen;
9832
9833 /* Expansion due to the addition of CBC padding;
9834 * Theoretically up to 256 bytes, but we never use
9835 * more than the block size of the underlying cipher. */
9836 transform_expansion += block_size;
9837
9838 /* For TLS 1.1 or higher, an explicit IV is added
9839 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009840#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009841 if( mbedtls_ssl_ver_geq(
9842 mbedtls_ssl_get_minor_ver( ssl ),
9843 MBEDTLS_SSL_MINOR_VERSION_2 ) )
9844 {
Hanno Becker3136ede2018-08-17 15:28:19 +01009845 transform_expansion += block_size;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009846 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01009847#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009848
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009849 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009850 }
9851#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009852
9853 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009856 }
9857
Hanno Beckera5a2b082019-05-15 14:03:01 +01009858#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009859 if( transform->out_cid_len != 0 )
9860 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009861#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009862
Hanno Becker43395762019-05-03 14:46:38 +01009863 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009864}
9865
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009866#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9867size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9868{
9869 size_t max_len;
9870
9871 /*
9872 * Assume mfl_code is correct since it was checked when set
9873 */
Angus Grattond8213d02016-05-25 20:56:48 +10009874 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009875
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009876 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009877 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009878 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009879 {
Angus Grattond8213d02016-05-25 20:56:48 +10009880 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009881 }
9882
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009883 /* During a handshake, use the value being negotiated */
9884 if( ssl->session_negotiate != NULL &&
9885 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9886 {
9887 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9888 }
9889
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009890 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009891}
9892#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9893
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009894#if defined(MBEDTLS_SSL_PROTO_DTLS)
9895static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9896{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009897 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009898 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009899 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9900 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009901 return( 0 );
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009902
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009903 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9904 return( ssl->mtu );
9905
9906 if( ssl->mtu == 0 )
9907 return( ssl->handshake->mtu );
9908
9909 return( ssl->mtu < ssl->handshake->mtu ?
9910 ssl->mtu : ssl->handshake->mtu );
9911}
9912#endif /* MBEDTLS_SSL_PROTO_DTLS */
9913
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009914int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9915{
9916 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9917
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009918#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9919 !defined(MBEDTLS_SSL_PROTO_DTLS)
9920 (void) ssl;
9921#endif
9922
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009923#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9924 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9925
9926 if( max_len > mfl )
9927 max_len = mfl;
9928#endif
9929
9930#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009931 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009932 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009933 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009934 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9935 const size_t overhead = (size_t) ret;
9936
9937 if( ret < 0 )
9938 return( ret );
9939
9940 if( mtu <= overhead )
9941 {
9942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9943 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9944 }
9945
9946 if( max_len > mtu - overhead )
9947 max_len = mtu - overhead;
9948 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009949#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009950
Hanno Becker0defedb2018-08-10 12:35:02 +01009951#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9952 !defined(MBEDTLS_SSL_PROTO_DTLS)
9953 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009954#endif
9955
9956 return( (int) max_len );
9957}
9958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009959#if defined(MBEDTLS_X509_CRT_PARSE_C)
9960const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009961{
9962 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009963 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009964
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009965#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009966 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009967#else
9968 return( NULL );
9969#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009970}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009971#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009973#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009974int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9975 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009976{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009977 if( ssl == NULL ||
9978 dst == NULL ||
9979 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009980 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009982 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009983 }
9984
Hanno Becker58fccf22019-02-06 14:30:46 +00009985 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009986}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009987#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009988
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009989const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9990{
9991 if( ssl == NULL )
9992 return( NULL );
9993
9994 return( ssl->session );
9995}
9996
Paul Bakker5121ce52009-01-03 21:22:43 +00009997/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009998 * Define ticket header determining Mbed TLS version
9999 * and structure of the ticket.
10000 */
10001
Hanno Becker41527622019-05-16 12:50:45 +010010002/*
Hanno Becker26829e92019-05-28 14:30:45 +010010003 * Define bitflag determining compile-time settings influencing
10004 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +010010005 */
10006
Hanno Becker26829e92019-05-28 14:30:45 +010010007#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010008#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +010010009#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010010#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +010010011#endif /* MBEDTLS_HAVE_TIME */
10012
10013#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010014#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +010010015#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010016#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +010010017#endif /* MBEDTLS_X509_CRT_PARSE_C */
10018
10019#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010020#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +010010021#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010022#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +010010023#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
10024
10025#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010026#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +010010027#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010028#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +010010029#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
10030
10031#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010032#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +010010033#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010034#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +010010035#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
10036
10037#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010038#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +010010039#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010040#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +010010041#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
10042
Hanno Becker41527622019-05-16 12:50:45 +010010043#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10044#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
10045#else
10046#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
10047#endif /* MBEDTLS_SSL_SESSION_TICKETS */
10048
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010049#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10050#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
10051#else
10052#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
10053#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10054
Hanno Becker88440552019-07-03 14:16:13 +010010055#if defined(MBEDTLS_ZLIB_SUPPORT)
10056#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
10057#else
10058#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
10059#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10060
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010061#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
10062#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
10063#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
10064#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
10065#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
10066#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
10067#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010068#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +010010069#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010070
Hanno Becker26829e92019-05-28 14:30:45 +010010071#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010072 ( (uint16_t) ( \
10073 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
10074 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
10075 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
10076 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
10077 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
10078 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010079 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +010010080 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010081 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +010010082
Hanno Becker557fe9f2019-05-16 12:41:07 +010010083static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +010010084 MBEDTLS_VERSION_MAJOR,
10085 MBEDTLS_VERSION_MINOR,
10086 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +010010087 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
10088 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +010010089};
Hanno Beckerb5352f02019-05-16 12:39:07 +010010090
10091/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010092 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010093 * (in the presentation language of TLS, RFC 8446 section 3)
10094 *
Hanno Becker26829e92019-05-28 14:30:45 +010010095 * opaque mbedtls_version[3]; // major, minor, patch
10096 * opaque session_format[2]; // version-specific 16-bit field determining
10097 * // the format of the remaining
10098 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +010010099 *
10100 * Note: When updating the format, remember to keep
10101 * these version+format bytes.
10102 *
Hanno Becker7bf77102019-06-04 09:43:16 +010010103 * // In this version, `session_format` determines
10104 * // the setting of those compile-time
10105 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +010010106 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010107 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +010010108 * uint8 ciphersuite[2]; // defined by the standard
10109 * uint8 compression; // 0 or 1
10110 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010111 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +010010112 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010113 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +010010114 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
10115 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
10116 * case disabled: uint8_t peer_cert_digest_type;
10117 * opaque peer_cert_digest<0..2^8-1>;
10118 * }
Hanno Becker26829e92019-05-28 14:30:45 +010010119 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010120 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +010010121 * uint8 mfl_code; // up to 255 according to standard
10122 * uint8 trunc_hmac; // 0 or 1
10123 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010124 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010125 * The order is the same as in the definition of the structure, except
10126 * verify_result is put before peer_cert so that all mandatory fields come
10127 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010128 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010129static int ssl_session_save( const mbedtls_ssl_session *session,
10130 unsigned char omit_header,
10131 unsigned char *buf,
10132 size_t buf_len,
10133 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010134{
10135 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010136 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010137#if defined(MBEDTLS_HAVE_TIME)
10138 uint64_t start;
10139#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010140#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010141#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010142 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010143#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +000010144#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010145
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010146 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010147 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010148 /*
10149 * Add version identifier
10150 */
10151
10152 used += sizeof( ssl_serialized_session_header );
10153
10154 if( used <= buf_len )
10155 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010156 mbedtls_platform_memcpy( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010157 sizeof( ssl_serialized_session_header ) );
10158 p += sizeof( ssl_serialized_session_header );
10159 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010160 }
10161
10162 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010163 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010164 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010165#if defined(MBEDTLS_HAVE_TIME)
10166 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010167
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010168 if( used <= buf_len )
10169 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010170 start = (uint64_t) session->start;
10171
10172 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
10173 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
10174 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
10175 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
10176 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
10177 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
10178 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
10179 *p++ = (unsigned char)( ( start ) & 0xFF );
10180 }
10181#endif /* MBEDTLS_HAVE_TIME */
10182
10183 /*
10184 * Basic mandatory fields
10185 */
Hanno Becker88440552019-07-03 14:16:13 +010010186 {
10187 size_t const ciphersuite_len = 2;
10188#if defined(MBEDTLS_ZLIB_SUPPORT)
10189 size_t const compression_len = 1;
10190#else
10191 size_t const compression_len = 0;
10192#endif
10193 size_t const id_len_len = 1;
10194 size_t const id_len = 32;
10195 size_t const master_len = 48;
10196 size_t const verif_result_len = 4;
10197
10198 size_t const basic_len =
10199 ciphersuite_len +
10200 compression_len +
10201 id_len_len +
10202 id_len +
10203 master_len +
10204 verif_result_len;
10205
10206 used += basic_len;
10207 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010208
10209 if( used <= buf_len )
10210 {
Hanno Beckere02758c2019-06-26 15:31:31 +010010211 const int ciphersuite =
10212 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010213 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010214
Hanno Becker88440552019-07-03 14:16:13 +010010215#if defined(MBEDTLS_ZLIB_SUPPORT)
10216 *p++ = (unsigned char)(
10217 mbedtls_ssl_session_get_compression( session ) );
10218#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010219
10220 *p++ = (unsigned char)( session->id_len & 0xFF );
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010221 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10222 memcpy( p, session->id, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010223 p += 32;
10224
Teppo Järvelin91d79382019-10-02 09:09:31 +030010225 mbedtls_platform_memcpy( p, session->master, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010226 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010227 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010228 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010229
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010230 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010231 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010232 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010233#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010234#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010235 if( session->peer_cert == NULL )
10236 cert_len = 0;
10237 else
10238 cert_len = session->peer_cert->raw.len;
10239
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010240 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010241
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010242 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010243 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010244 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010245
10246 if( session->peer_cert != NULL )
10247 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010248 mbedtls_platform_memcpy( p, session->peer_cert->raw.p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010249 p += cert_len;
10250 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010251 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010252
Hanno Becker5882dd02019-06-06 16:25:57 +010010253#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010254 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010255 if( session->peer_cert_digest != NULL )
10256 {
10257 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
10258 if( used <= buf_len )
10259 {
10260 *p++ = (unsigned char) session->peer_cert_digest_type;
10261 *p++ = (unsigned char) session->peer_cert_digest_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +030010262 mbedtls_platform_memcpy( p, session->peer_cert_digest,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010263 session->peer_cert_digest_len );
10264 p += session->peer_cert_digest_len;
10265 }
10266 }
10267 else
10268 {
10269 used += 2;
10270 if( used <= buf_len )
10271 {
10272 *p++ = (unsigned char) MBEDTLS_MD_NONE;
10273 *p++ = 0;
10274 }
10275 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010276#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010277#endif /* MBEDTLS_X509_CRT_PARSE_C */
10278
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010279 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010280 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010281 */
10282#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010283 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010284
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010285 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010286 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010287 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010288
10289 if( session->ticket != NULL )
10290 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010291 mbedtls_platform_memcpy( p, session->ticket, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010292 p += session->ticket_len;
10293 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010294
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010295 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010296 }
10297#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10298
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010299 /*
10300 * Misc extension-related info
10301 */
10302#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10303 used += 1;
10304
10305 if( used <= buf_len )
10306 *p++ = session->mfl_code;
10307#endif
10308
10309#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10310 used += 1;
10311
10312 if( used <= buf_len )
10313 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
10314#endif
10315
10316#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10317 used += 1;
10318
10319 if( used <= buf_len )
10320 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
10321#endif
10322
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010323 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010324 *olen = used;
10325
10326 if( used > buf_len )
10327 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010328
10329 return( 0 );
10330}
10331
10332/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010333 * Public wrapper for ssl_session_save()
10334 */
10335int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
10336 unsigned char *buf,
10337 size_t buf_len,
10338 size_t *olen )
10339{
10340 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
10341}
10342
10343/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010344 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010345 *
10346 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010347 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010348 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010349static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010350 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010351 const unsigned char *buf,
10352 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010353{
10354 const unsigned char *p = buf;
10355 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +010010356 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010357#if defined(MBEDTLS_HAVE_TIME)
10358 uint64_t start;
10359#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010360#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010361#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010362 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +000010363#endif
10364#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010365
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010366 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010367 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010368 /*
10369 * Check version identifier
10370 */
10371
10372 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
10373 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10374
Teppo Järvelin0efac532019-10-04 13:21:08 +030010375 // use regular memcmp as session header is public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010376 if( memcmp( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010377 sizeof( ssl_serialized_session_header ) ) != 0 )
10378 {
10379 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10380 }
10381 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +010010382 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010383
10384 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010385 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010386 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010387#if defined(MBEDTLS_HAVE_TIME)
10388 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010389 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10390
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010391 start = ( (uint64_t) p[0] << 56 ) |
10392 ( (uint64_t) p[1] << 48 ) |
10393 ( (uint64_t) p[2] << 40 ) |
10394 ( (uint64_t) p[3] << 32 ) |
10395 ( (uint64_t) p[4] << 24 ) |
10396 ( (uint64_t) p[5] << 16 ) |
10397 ( (uint64_t) p[6] << 8 ) |
10398 ( (uint64_t) p[7] );
10399 p += 8;
10400
10401 session->start = (time_t) start;
10402#endif /* MBEDTLS_HAVE_TIME */
10403
10404 /*
10405 * Basic mandatory fields
10406 */
Hanno Becker88440552019-07-03 14:16:13 +010010407 {
10408 size_t const ciphersuite_len = 2;
10409#if defined(MBEDTLS_ZLIB_SUPPORT)
10410 size_t const compression_len = 1;
10411#else
10412 size_t const compression_len = 0;
10413#endif
10414 size_t const id_len_len = 1;
10415 size_t const id_len = 32;
10416 size_t const master_len = 48;
10417 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010418
Hanno Becker88440552019-07-03 14:16:13 +010010419 size_t const basic_len =
10420 ciphersuite_len +
10421 compression_len +
10422 id_len_len +
10423 id_len +
10424 master_len +
10425 verif_result_len;
10426
10427 if( basic_len > (size_t)( end - p ) )
10428 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10429 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010430
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010431 ciphersuite = (int)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010432 p += 2;
10433
Hanno Becker73f4cb12019-06-27 13:51:07 +010010434#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010435 session->ciphersuite = ciphersuite;
10436#else
10437 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010438 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010439 {
10440 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10441 }
10442#endif
10443
Hanno Becker88440552019-07-03 14:16:13 +010010444#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010445 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010446#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010447
10448 session->id_len = *p++;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010449 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10450 memcpy( session->id, p, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010451 p += 32;
10452
Teppo Järvelin91d79382019-10-02 09:09:31 +030010453 mbedtls_platform_memcpy( session->master, p, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010454 p += 48;
10455
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010456 session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010457 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010458
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010459 /* Immediately clear invalid pointer values that have been read, in case
10460 * we exit early before we replaced them with valid ones. */
10461#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010462#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010463 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010464#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010465 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010466#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010467#endif
10468#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10469 session->ticket = NULL;
10470#endif
10471
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010472 /*
10473 * Peer certificate
10474 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010475#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010476#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010477 if( 3 > (size_t)( end - p ) )
10478 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10479
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010480 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10481
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010482 p += 3;
10483
10484 if( cert_len == 0 )
10485 {
10486 session->peer_cert = NULL;
10487 }
10488 else
10489 {
10490 int ret;
10491
10492 if( cert_len > (size_t)( end - p ) )
10493 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10494
10495 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10496
10497 if( session->peer_cert == NULL )
10498 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10499
10500 mbedtls_x509_crt_init( session->peer_cert );
10501
10502 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10503 p, cert_len ) ) != 0 )
10504 {
10505 mbedtls_x509_crt_free( session->peer_cert );
10506 mbedtls_free( session->peer_cert );
10507 session->peer_cert = NULL;
10508 return( ret );
10509 }
10510
10511 p += cert_len;
10512 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010513#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010514 /* Deserialize CRT digest from the end of the ticket. */
10515 if( 2 > (size_t)( end - p ) )
10516 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10517
10518 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10519 session->peer_cert_digest_len = (size_t) *p++;
10520
10521 if( session->peer_cert_digest_len != 0 )
10522 {
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010523 mbedtls_md_handle_t md_info =
Hanno Becker2326d202019-06-06 14:54:55 +010010524 mbedtls_md_info_from_type( session->peer_cert_digest_type );
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010525 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Hanno Becker2326d202019-06-06 14:54:55 +010010526 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10527 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10529
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010530 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10531 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10532
10533 session->peer_cert_digest =
10534 mbedtls_calloc( 1, session->peer_cert_digest_len );
10535 if( session->peer_cert_digest == NULL )
10536 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10537
Teppo Järvelin91d79382019-10-02 09:09:31 +030010538 mbedtls_platform_memcpy( session->peer_cert_digest, p,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010539 session->peer_cert_digest_len );
10540 p += session->peer_cert_digest_len;
10541 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010542#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010543#endif /* MBEDTLS_X509_CRT_PARSE_C */
10544
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010545 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010546 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010547 */
10548#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10549 if( 3 > (size_t)( end - p ) )
10550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10551
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010552 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010553 p += 3;
10554
10555 if( session->ticket_len != 0 )
10556 {
10557 if( session->ticket_len > (size_t)( end - p ) )
10558 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10559
10560 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10561 if( session->ticket == NULL )
10562 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10563
Teppo Järvelin91d79382019-10-02 09:09:31 +030010564 mbedtls_platform_memcpy( session->ticket, p, session->ticket_len );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010565 p += session->ticket_len;
10566 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010567
10568 if( 4 > (size_t)( end - p ) )
10569 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10570
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010571 session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010572 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010573#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10574
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010575 /*
10576 * Misc extension-related info
10577 */
10578#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10579 if( 1 > (size_t)( end - p ) )
10580 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10581
10582 session->mfl_code = *p++;
10583#endif
10584
10585#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10586 if( 1 > (size_t)( end - p ) )
10587 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10588
10589 session->trunc_hmac = *p++;
10590#endif
10591
10592#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10593 if( 1 > (size_t)( end - p ) )
10594 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10595
10596 session->encrypt_then_mac = *p++;
10597#endif
10598
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010599 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010600 if( p != end )
10601 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10602
10603 return( 0 );
10604}
10605
10606/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010607 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010608 */
10609int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10610 const unsigned char *buf,
10611 size_t len )
10612{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010613 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010614
10615 if( ret != 0 )
10616 mbedtls_ssl_session_free( session );
10617
10618 return( ret );
10619}
10620
10621/*
Paul Bakker1961b702013-01-25 14:49:24 +010010622 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010623 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010624int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010625{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010626 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010627
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010628 if( ssl == NULL || ssl->conf == NULL )
10629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010631#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010632 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010633 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010634#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010635#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010636 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010637 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010638#endif
10639
Hanno Beckerb82350b2019-07-26 07:24:05 +010010640 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010641 return( ret );
10642}
10643
10644/*
10645 * Perform the SSL handshake
10646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010647int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010648{
10649 int ret = 0;
10650
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010651 if( ssl == NULL || ssl->conf == NULL )
10652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010656 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010658 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010659
10660 if( ret != 0 )
10661 break;
10662 }
10663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010665
10666 return( ret );
10667}
10668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010669#if defined(MBEDTLS_SSL_RENEGOTIATION)
10670#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010671/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010672 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010674static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010675{
10676 int ret;
10677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010679
10680 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010681 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10682 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010683
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010684 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010685 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010686 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010687 return( ret );
10688 }
10689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010691
10692 return( 0 );
10693}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010694#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010695
10696/*
10697 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010698 * - any side: calling mbedtls_ssl_renegotiate(),
10699 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10700 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010701 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010702 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010703 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010704 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010705static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010706{
10707 int ret;
10708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010710
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010711 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10712 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010713
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010714 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10715 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010716#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010717 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010718 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010719 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010720 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10721 MBEDTLS_SSL_IS_SERVER )
10722 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010723 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010724 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010725 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010726 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010727 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010728 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010729 }
10730#endif
10731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010732 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10733 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010735 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010737 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010738 return( ret );
10739 }
10740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010742
10743 return( 0 );
10744}
10745
10746/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010747 * Renegotiate current connection on client,
10748 * or request renegotiation on server
10749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010750int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010751{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010752 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010753
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010754 if( ssl == NULL || ssl->conf == NULL )
10755 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010757#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010758 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010759 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010761 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10762 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010764 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010765
10766 /* Did we already try/start sending HelloRequest? */
10767 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010768 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010769
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010770 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010771 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010772#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010774#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010775 /*
10776 * On client, either start the renegotiation process or,
10777 * if already in progress, continue the handshake
10778 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010779 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010781 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10782 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010783
10784 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010786 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010787 return( ret );
10788 }
10789 }
10790 else
10791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010792 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010794 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010795 return( ret );
10796 }
10797 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010798#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010799
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010800 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010801}
10802
10803/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010804 * Check record counters and renegotiate if they're above the limit.
10805 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010806static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010807{
Andres AG2196c7f2016-12-15 17:01:16 +000010808 size_t ep_len = ssl_ep_len( ssl );
10809 int in_ctr_cmp;
10810 int out_ctr_cmp;
10811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010812 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10813 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010814 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010815 {
10816 return( 0 );
10817 }
10818
Teppo Järvelin0efac532019-10-04 13:21:08 +030010819 // use regular memcmp as counters are public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010820 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010821 ssl->conf->renego_period + ep_len, 8 - ep_len );
Teppo Järvelin650343c2019-10-03 15:36:59 +030010822 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010823 ssl->conf->renego_period + ep_len, 8 - ep_len );
10824
10825 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010826 {
10827 return( 0 );
10828 }
10829
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010831 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010832}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010833#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010834
10835/*
10836 * Receive application data decrypted from the SSL layer
10837 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010838int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010839{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010840 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010841 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010842
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010843 if( ssl == NULL || ssl->conf == NULL )
10844 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010848#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010849 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010851 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010852 return( ret );
10853
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010854 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010855 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010856 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010857 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010858 return( ret );
10859 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010860 }
10861#endif
10862
Hanno Becker4a810fb2017-05-24 16:27:30 +010010863 /*
10864 * Check if renegotiation is necessary and/or handshake is
10865 * in process. If yes, perform/continue, and fall through
10866 * if an unexpected packet is received while the client
10867 * is waiting for the ServerHello.
10868 *
10869 * (There is no equivalent to the last condition on
10870 * the server-side as it is not treated as within
10871 * a handshake while waiting for the ClientHello
10872 * after a renegotiation request.)
10873 */
10874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010875#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010876 ret = ssl_check_ctr_renegotiate( ssl );
10877 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10878 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010880 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010881 return( ret );
10882 }
10883#endif
10884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010885 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010887 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010888 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10889 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010891 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010892 return( ret );
10893 }
10894 }
10895
Hanno Beckere41158b2017-10-23 13:30:32 +010010896 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010897 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010898 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010899 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010900 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10901 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010902 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010903 ssl_set_timer( ssl,
10904 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010905 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010906
Hanno Becker327c93b2018-08-15 13:56:18 +010010907 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010908 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010909 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10910 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010911
Hanno Becker4a810fb2017-05-24 16:27:30 +010010912 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10913 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010914 }
10915
10916 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010917 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010918 {
10919 /*
10920 * OpenSSL sends empty messages to randomize the IV
10921 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010922 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010924 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010925 return( 0 );
10926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010927 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010928 return( ret );
10929 }
10930 }
10931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010932 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010935
Hanno Becker4a810fb2017-05-24 16:27:30 +010010936 /*
10937 * - For client-side, expect SERVER_HELLO_REQUEST.
10938 * - For server-side, expect CLIENT_HELLO.
10939 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10940 */
10941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010942#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010943 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10944 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010945 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010946 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010949
10950 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010951#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010952 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010953 {
10954 continue;
10955 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010956 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010957#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010958#if defined(MBEDTLS_SSL_PROTO_TLS)
10959 {
10960 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10961 }
10962#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010963 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010964#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010965
Hanno Becker4a810fb2017-05-24 16:27:30 +010010966#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010967 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10968 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010969 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010972
10973 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010974#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010975 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010976 {
10977 continue;
10978 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010979 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010980#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010981#if defined(MBEDTLS_SSL_PROTO_TLS)
10982 {
10983 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10984 }
10985#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010986 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010987#endif /* MBEDTLS_SSL_SRV_C */
10988
Hanno Becker21df7f92017-10-17 11:03:26 +010010989#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010990 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010991 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10992 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010993 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010994 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10995 {
10996 /*
10997 * Accept renegotiation request
10998 */
Paul Bakker48916f92012-09-16 19:57:18 +000010999
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011000 /* DTLS clients need to know renego is server-initiated */
11001#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011002 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010011003 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11004 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011005 {
11006 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
11007 }
11008#endif
11009 ret = ssl_start_renegotiation( ssl );
11010 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
11011 ret != 0 )
11012 {
11013 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
11014 return( ret );
11015 }
11016 }
11017 else
Hanno Becker21df7f92017-10-17 11:03:26 +010011018#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000011019 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010011020 /*
11021 * Refuse renegotiation
11022 */
11023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011024 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011026#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010011027 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000011028 {
Gilles Peskine92e44262017-05-10 17:27:49 +020011029 /* SSLv3 does not have a "no_renegotiation" warning, so
11030 we send a fatal alert and abort the connection. */
11031 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11032 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
11033 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011034 }
11035 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011036#endif /* MBEDTLS_SSL_PROTO_SSL3 */
11037#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11038 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011039 if( mbedtls_ssl_ver_geq(
11040 mbedtls_ssl_get_minor_ver( ssl ),
11041 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011042 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010011043 ret = mbedtls_ssl_send_alert_message( ssl,
11044 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11045 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
11046 if( ret != 0 )
11047 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000011048 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020011049 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011050#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
11051 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020011052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
11054 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020011055 }
Paul Bakker48916f92012-09-16 19:57:18 +000011056 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011057
Hanno Becker90333da2017-10-10 11:27:13 +010011058 /* At this point, we don't know whether the renegotiation has been
11059 * completed or not. The cases to consider are the following:
11060 * 1) The renegotiation is complete. In this case, no new record
11061 * has been read yet.
11062 * 2) The renegotiation is incomplete because the client received
11063 * an application data record while awaiting the ServerHello.
11064 * 3) The renegotiation is incomplete because the client received
11065 * a non-handshake, non-application data message while awaiting
11066 * the ServerHello.
11067 * In each of these case, looping will be the proper action:
11068 * - For 1), the next iteration will read a new record and check
11069 * if it's application data.
11070 * - For 2), the loop condition isn't satisfied as application data
11071 * is present, hence continue is the same as break
11072 * - For 3), the loop condition is satisfied and read_record
11073 * will re-deliver the message that was held back by the client
11074 * when expecting the ServerHello.
11075 */
11076 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000011077 }
Hanno Becker21df7f92017-10-17 11:03:26 +010011078#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011079 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011080 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011081 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011082 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011083 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011086 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011087 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011088 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011089 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011090 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011091#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011093 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
11094 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010011097 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011098 }
11099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011100 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000011101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
11103 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000011104 }
11105
11106 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020011107
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011108 /* We're going to return something now, cancel timer,
11109 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011110 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011111 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011112
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020011113#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011114 /* If we requested renego but received AppData, resend HelloRequest.
11115 * Do it now, after setting in_offt, to avoid taking this branch
11116 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011117#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010011118 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11119 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011120 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011121 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011122 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011124 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011125 return( ret );
11126 }
11127 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011128#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010011129#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000011130 }
11131
11132 n = ( len < ssl->in_msglen )
11133 ? len : ssl->in_msglen;
11134
Teppo Järvelin91d79382019-10-02 09:09:31 +030011135 mbedtls_platform_memcpy( buf, ssl->in_offt, n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011136 ssl->in_msglen -= n;
11137
Teppo Järvelincafb6c92020-01-08 09:19:07 +020011138 // clear incoming data after it's copied to buffer
11139 mbedtls_platform_memset(ssl->in_offt, 0, n);
11140
Paul Bakker5121ce52009-01-03 21:22:43 +000011141 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010011142 {
11143 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000011144 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010011145 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011146 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011147 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010011148 {
Paul Bakker5121ce52009-01-03 21:22:43 +000011149 /* more data available */
11150 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011151 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011154
Paul Bakker23986e52011-04-24 08:57:21 +000011155 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011156}
11157
11158/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011159 * Send application data to be encrypted by the SSL layer, taking care of max
11160 * fragment length and buffer size.
11161 *
11162 * According to RFC 5246 Section 6.2.1:
11163 *
11164 * Zero-length fragments of Application data MAY be sent as they are
11165 * potentially useful as a traffic analysis countermeasure.
11166 *
11167 * Therefore, it is possible that the input message length is 0 and the
11168 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000011169 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011170static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011171 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000011172{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020011173 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
11174 const size_t max_len = (size_t) ret;
11175
11176 if( ret < 0 )
11177 {
11178 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
11179 return( ret );
11180 }
11181
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011182 if( len > max_len )
11183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011184#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011185 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011188 "maximum fragment length: %d > %d",
11189 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011190 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011191 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011192 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011193#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011194#if defined(MBEDTLS_SSL_PROTO_TLS)
11195 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011196 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011197 }
11198#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011199 }
Paul Bakker887bd502011-06-08 13:10:54 +000011200
Paul Bakker5121ce52009-01-03 21:22:43 +000011201 if( ssl->out_left != 0 )
11202 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011203 /*
11204 * The user has previously tried to send the data and
11205 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
11206 * written. In this case, we expect the high-level write function
11207 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
11208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011209 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011211 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011212 return( ret );
11213 }
11214 }
Paul Bakker887bd502011-06-08 13:10:54 +000011215 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000011216 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011217 /*
11218 * The user is trying to send a message the first time, so we need to
11219 * copy the data into the internal buffers and setup the data structure
11220 * to keep track of partial writes
11221 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011222 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011223 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Teppo Järvelin91d79382019-10-02 09:09:31 +030011224 mbedtls_platform_memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000011225
Hanno Becker67bc7c32018-08-06 11:33:50 +010011226 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000011227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011228 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000011229 return( ret );
11230 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011231 }
11232
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011233 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000011234}
11235
11236/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011237 * Write application data, doing 1/n-1 splitting if necessary.
11238 *
11239 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011240 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010011241 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011243#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011244static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011245 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011246{
11247 int ret;
11248
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011249 if( ssl->conf->cbc_record_splitting ==
11250 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011251 len <= 1 ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011252 mbedtls_ssl_ver_gt(
11253 mbedtls_ssl_get_minor_ver( ssl ),
11254 MBEDTLS_SSL_MINOR_VERSION_1 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011255 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
11256 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011257 {
11258 return( ssl_write_real( ssl, buf, len ) );
11259 }
11260
11261 if( ssl->split_done == 0 )
11262 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011263 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011264 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011265 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011266 }
11267
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011268 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
11269 return( ret );
11270 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011271
11272 return( ret + 1 );
11273}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011274#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011275
11276/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011277 * Write application data (public-facing wrapper)
11278 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011279int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011280{
11281 int ret;
11282
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011284
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011285 if( ssl == NULL || ssl->conf == NULL )
11286 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11287
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011288#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011289 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
11290 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011291 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011292 return( ret );
11293 }
11294#endif
11295
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011296 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011297 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011298 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011299 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020011300 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011301 return( ret );
11302 }
11303 }
11304
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011305#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011306 ret = ssl_write_split( ssl, buf, len );
11307#else
11308 ret = ssl_write_real( ssl, buf, len );
11309#endif
11310
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011312
11313 return( ret );
11314}
11315
11316/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011317 * Notify the peer that the connection is being closed
11318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011319int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011320{
11321 int ret;
11322
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011323 if( ssl == NULL || ssl->conf == NULL )
11324 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011327
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011328 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011329 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011331 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000011332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011333 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
11334 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11335 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011337 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011338 return( ret );
11339 }
11340 }
11341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011343
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011344 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000011345}
11346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011347void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000011348{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011349 if( transform == NULL )
11350 return;
11351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011352#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000011353 deflateEnd( &transform->ctx_deflate );
11354 inflateEnd( &transform->ctx_inflate );
11355#endif
11356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011357 mbedtls_cipher_free( &transform->cipher_ctx_enc );
11358 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020011359
Hanno Becker92231322018-01-03 15:32:51 +000011360#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011361 mbedtls_md_free( &transform->md_ctx_enc );
11362 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000011363#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011364
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011365 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011366}
11367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011368#if defined(MBEDTLS_X509_CRT_PARSE_C)
11369static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011370{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011371 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011372
11373 while( cur != NULL )
11374 {
11375 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011376 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011377 cur = next;
11378 }
11379}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011380#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011381
Hanno Becker0271f962018-08-16 13:23:47 +010011382#if defined(MBEDTLS_SSL_PROTO_DTLS)
11383
11384static void ssl_buffering_free( mbedtls_ssl_context *ssl )
11385{
11386 unsigned offset;
11387 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11388
11389 if( hs == NULL )
11390 return;
11391
Hanno Becker283f5ef2018-08-24 09:34:47 +010011392 ssl_free_buffered_record( ssl );
11393
Hanno Becker0271f962018-08-16 13:23:47 +010011394 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010011395 ssl_buffering_free_slot( ssl, offset );
11396}
11397
11398static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
11399 uint8_t slot )
11400{
11401 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11402 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010011403
11404 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
11405 return;
11406
Hanno Beckere605b192018-08-21 15:59:07 +010011407 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010011408 {
Hanno Beckere605b192018-08-21 15:59:07 +010011409 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010011410 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010011411 mbedtls_free( hs_buf->data );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020011412 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010011413 }
11414}
11415
11416#endif /* MBEDTLS_SSL_PROTO_DTLS */
11417
Gilles Peskine9b562d52018-04-25 20:32:43 +020011418void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011419{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011420 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11421
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011422 if( handshake == NULL )
11423 return;
11424
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011425#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11426 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11427 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011428 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011429 handshake->async_in_progress = 0;
11430 }
11431#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11432
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011433#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11434 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11435 mbedtls_md5_free( &handshake->fin_md5 );
11436 mbedtls_sha1_free( &handshake->fin_sha1 );
11437#endif
11438#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11439#if defined(MBEDTLS_SHA256_C)
11440 mbedtls_sha256_free( &handshake->fin_sha256 );
11441#endif
11442#if defined(MBEDTLS_SHA512_C)
11443 mbedtls_sha512_free( &handshake->fin_sha512 );
11444#endif
11445#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011447#if defined(MBEDTLS_DHM_C)
11448 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011449#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011450#if defined(MBEDTLS_ECDH_C)
11451 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011452#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011453#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011454 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011455#if defined(MBEDTLS_SSL_CLI_C)
11456 mbedtls_free( handshake->ecjpake_cache );
11457 handshake->ecjpake_cache = NULL;
11458 handshake->ecjpake_cache_len = 0;
11459#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011460#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011461
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011462#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11463 if( handshake->psk != NULL )
11464 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011465 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011466 mbedtls_free( handshake->psk );
11467 }
11468#endif
11469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011470#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11471 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011472 /*
11473 * Free only the linked list wrapper, not the keys themselves
11474 * since the belong to the SNI callback
11475 */
11476 if( handshake->sni_key_cert != NULL )
11477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011478 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011479
11480 while( cur != NULL )
11481 {
11482 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011483 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011484 cur = next;
11485 }
11486 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011487#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011488
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011489#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011490 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011491 if( handshake->ecrs_peer_cert != NULL )
11492 {
11493 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11494 mbedtls_free( handshake->ecrs_peer_cert );
11495 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011496#endif
11497
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011498#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11499 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11500 mbedtls_pk_free( &handshake->peer_pubkey );
11501#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011503#if defined(MBEDTLS_SSL_PROTO_DTLS)
11504 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011505 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011506 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011507#endif
11508
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011509 mbedtls_platform_zeroize( handshake,
11510 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011511}
11512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011513void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011514{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011515 if( session == NULL )
11516 return;
11517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011518#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011519 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011520#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011521
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011522#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011523 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011524#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011525
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011526 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011527}
11528
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011529#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011530
11531#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11532#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11533#else
11534#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11535#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11536
11537#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11538#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11539#else
11540#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11541#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11542
11543#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11544#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11545#else
11546#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11547#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11548
11549#if defined(MBEDTLS_SSL_ALPN)
11550#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11551#else
11552#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11553#endif /* MBEDTLS_SSL_ALPN */
11554
11555#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11556#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11557#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11558#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11559
11560#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11561 ( (uint32_t) ( \
11562 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11563 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11564 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11565 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11566 0u ) )
11567
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011568static unsigned char ssl_serialized_context_header[] = {
11569 MBEDTLS_VERSION_MAJOR,
11570 MBEDTLS_VERSION_MINOR,
11571 MBEDTLS_VERSION_PATCH,
11572 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11573 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011574 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11575 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11576 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011577};
11578
Paul Bakker5121ce52009-01-03 21:22:43 +000011579/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011580 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011581 *
11582 * The format of the serialized data is:
11583 * (in the presentation language of TLS, RFC 8446 section 3)
11584 *
11585 * // header
11586 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011587 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011588 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011589 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011590 * Note: When updating the format, remember to keep these
11591 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011592 *
11593 * // session sub-structure
11594 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11595 * // transform sub-structure
11596 * uint8 random[64]; // ServerHello.random+ClientHello.random
11597 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11598 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11599 * // fields from ssl_context
11600 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11601 * uint64 in_window_top; // DTLS: last validated record seq_num
11602 * uint64 in_window; // DTLS: bitmask for replay protection
11603 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11604 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11605 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11606 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11607 *
11608 * Note that many fields of the ssl_context or sub-structures are not
11609 * serialized, as they fall in one of the following categories:
11610 *
11611 * 1. forced value (eg in_left must be 0)
11612 * 2. pointer to dynamically-allocated memory (eg session, transform)
11613 * 3. value can be re-derived from other data (eg session keys from MS)
11614 * 4. value was temporary (eg content of input buffer)
11615 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011616 */
11617int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11618 unsigned char *buf,
11619 size_t buf_len,
11620 size_t *olen )
11621{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011622 unsigned char *p = buf;
11623 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011624 size_t session_len;
11625 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011626
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011627 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011628 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11629 * this function's documentation.
11630 *
11631 * These are due to assumptions/limitations in the implementation. Some of
11632 * them are likely to stay (no handshake in progress) some might go away
11633 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011634 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011635 /* The initial handshake must be over */
11636 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011637 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011638 if( ssl->handshake != NULL )
11639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11640 /* Double-check that sub-structures are indeed ready */
11641 if( ssl->transform == NULL || ssl->session == NULL )
11642 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11643 /* There must be no pending incoming or outgoing data */
11644 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11645 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11646 if( ssl->out_left != 0 )
11647 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11648 /* Protocol must be DLTS, not TLS */
11649 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11650 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11651 /* Version must be 1.2 */
11652 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11654 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11656 /* We must be using an AEAD ciphersuite */
11657 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11659 /* Renegotiation must not be enabled */
11660 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11661 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011662
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011663 /*
11664 * Version and format identifier
11665 */
11666 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011667
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011668 if( used <= buf_len )
11669 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011670 mbedtls_platform_memcpy( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011671 sizeof( ssl_serialized_context_header ) );
11672 p += sizeof( ssl_serialized_context_header );
11673 }
11674
11675 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011676 * Session (length + data)
11677 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011678 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011679 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11680 return( ret );
11681
11682 used += 4 + session_len;
11683 if( used <= buf_len )
11684 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011685 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011686
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011687 ret = ssl_session_save( ssl->session, 1,
11688 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011689 if( ret != 0 )
11690 return( ret );
11691
11692 p += session_len;
11693 }
11694
11695 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011696 * Transform
11697 */
11698 used += sizeof( ssl->transform->randbytes );
11699 if( used <= buf_len )
11700 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011701 mbedtls_platform_memcpy( p, ssl->transform->randbytes,
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011702 sizeof( ssl->transform->randbytes ) );
11703 p += sizeof( ssl->transform->randbytes );
11704 }
11705
11706#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11707 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11708 if( used <= buf_len )
11709 {
11710 *p++ = ssl->transform->in_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011711 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11712 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011713 p += ssl->transform->in_cid_len;
11714
11715 *p++ = ssl->transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011716 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11717 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011718 p += ssl->transform->out_cid_len;
11719 }
11720#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11721
11722 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011723 * Saved fields from top-level ssl_context structure
11724 */
11725#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11726 used += 4;
11727 if( used <= buf_len )
11728 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011729 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011730 }
11731#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11732
11733#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11734 used += 16;
11735 if( used <= buf_len )
11736 {
11737 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11738 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11739 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11740 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11741 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11742 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11743 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11744 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11745
11746 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11747 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11748 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11749 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11750 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11751 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11752 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11753 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11754 }
11755#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11756
11757#if defined(MBEDTLS_SSL_PROTO_DTLS)
11758 used += 1;
11759 if( used <= buf_len )
11760 {
11761 *p++ = ssl->disable_datagram_packing;
11762 }
11763#endif /* MBEDTLS_SSL_PROTO_DTLS */
11764
11765 used += 8;
11766 if( used <= buf_len )
11767 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011768 mbedtls_platform_memcpy( p, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011769 p += 8;
11770 }
11771
11772#if defined(MBEDTLS_SSL_PROTO_DTLS)
11773 used += 2;
11774 if( used <= buf_len )
11775 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011776 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011777 }
11778#endif /* MBEDTLS_SSL_PROTO_DTLS */
11779
11780#if defined(MBEDTLS_SSL_ALPN)
11781 {
11782 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011783 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011784 : 0;
11785
11786 used += 1 + alpn_len;
11787 if( used <= buf_len )
11788 {
11789 *p++ = alpn_len;
11790
11791 if( ssl->alpn_chosen != NULL )
11792 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011793 mbedtls_platform_memcpy( p, ssl->alpn_chosen, alpn_len );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011794 p += alpn_len;
11795 }
11796 }
11797 }
11798#endif /* MBEDTLS_SSL_ALPN */
11799
11800 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011801 * Done
11802 */
11803 *olen = used;
11804
11805 if( used > buf_len )
11806 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011807
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011808 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11809
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011810 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011811}
11812
11813/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011814 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011815 *
11816 * This internal version is wrapped by a public function that cleans up in
11817 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011818 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011819static int ssl_context_load( mbedtls_ssl_context *ssl,
11820 const unsigned char *buf,
11821 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011822{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011823 const unsigned char *p = buf;
11824 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011825 size_t session_len;
11826 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011827
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011828 /*
11829 * The context should have been freshly setup or reset.
11830 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011831 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011832 * renegotiating, or if the user mistakenly loaded a session first.)
11833 */
11834 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11835 ssl->session != NULL )
11836 {
11837 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11838 }
11839
11840 /*
11841 * We can't check that the config matches the initial one, but we can at
11842 * least check it matches the requirements for serializing.
11843 */
11844 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011845 mbedtls_ssl_ver_lt(
11846 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ),
11847 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11848 mbedtls_ssl_ver_gt(
11849 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ),
11850 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11851 mbedtls_ssl_ver_lt(
11852 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ),
11853 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
11854 mbedtls_ssl_ver_gt(
11855 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ),
11856 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011857 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011858 {
11859 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11860 }
11861
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011862 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11863
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011864 /*
11865 * Check version identifier
11866 */
11867 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11868 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11869
Teppo Järvelin650343c2019-10-03 15:36:59 +030011870 // use regular memcmp as header is not that critical
11871 if( memcmp( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011872 sizeof( ssl_serialized_context_header ) ) != 0 )
11873 {
11874 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11875 }
11876 p += sizeof( ssl_serialized_context_header );
11877
11878 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011879 * Session
11880 */
11881 if( (size_t)( end - p ) < 4 )
11882 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11883
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011884 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011885 p += 4;
11886
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011887 /* This has been allocated by ssl_handshake_init(), called by
11888 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11889 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011890 ssl->session_in = ssl->session;
11891 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011892 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011893
11894 if( (size_t)( end - p ) < session_len )
11895 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11896
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011897 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011898 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011899 {
11900 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011901 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011902 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011903
11904 p += session_len;
11905
11906 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011907 * Transform
11908 */
11909
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011910 /* This has been allocated by ssl_handshake_init(), called by
11911 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11912 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011913 ssl->transform_in = ssl->transform;
11914 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011915 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011916
11917 /* Read random bytes and populate structure */
11918 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11919 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11920
11921 ret = ssl_populate_transform( ssl->transform,
11922 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11923 ssl->session->master,
11924#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11925#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11926 ssl->session->encrypt_then_mac,
11927#endif
11928#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11929 ssl->session->trunc_hmac,
11930#endif
11931#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11932#if defined(MBEDTLS_ZLIB_SUPPORT)
11933 ssl->session->compression,
11934#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011935 p, /* currently pointing to randbytes */
11936 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11937 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11938 ssl );
11939 if( ret != 0 )
11940 return( ret );
11941
11942 p += sizeof( ssl->transform->randbytes );
11943
11944#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11945 /* Read connection IDs and store them */
11946 if( (size_t)( end - p ) < 1 )
11947 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11948
11949 ssl->transform->in_cid_len = *p++;
11950
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011951 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011952 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11953
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011954 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11955 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011956 p += ssl->transform->in_cid_len;
11957
11958 ssl->transform->out_cid_len = *p++;
11959
11960 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11962
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011963 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11964 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011965 p += ssl->transform->out_cid_len;
11966#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11967
11968 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011969 * Saved fields from top-level ssl_context structure
11970 */
11971#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11972 if( (size_t)( end - p ) < 4 )
11973 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11974
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011975 ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011976 p += 4;
11977#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11978
11979#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11980 if( (size_t)( end - p ) < 16 )
11981 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11982
11983 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11984 ( (uint64_t) p[1] << 48 ) |
11985 ( (uint64_t) p[2] << 40 ) |
11986 ( (uint64_t) p[3] << 32 ) |
11987 ( (uint64_t) p[4] << 24 ) |
11988 ( (uint64_t) p[5] << 16 ) |
11989 ( (uint64_t) p[6] << 8 ) |
11990 ( (uint64_t) p[7] );
11991 p += 8;
11992
11993 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11994 ( (uint64_t) p[1] << 48 ) |
11995 ( (uint64_t) p[2] << 40 ) |
11996 ( (uint64_t) p[3] << 32 ) |
11997 ( (uint64_t) p[4] << 24 ) |
11998 ( (uint64_t) p[5] << 16 ) |
11999 ( (uint64_t) p[6] << 8 ) |
12000 ( (uint64_t) p[7] );
12001 p += 8;
12002#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
12003
12004#if defined(MBEDTLS_SSL_PROTO_DTLS)
12005 if( (size_t)( end - p ) < 1 )
12006 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12007
12008 ssl->disable_datagram_packing = *p++;
12009#endif /* MBEDTLS_SSL_PROTO_DTLS */
12010
12011 if( (size_t)( end - p ) < 8 )
12012 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12013
Teppo Järvelin91d79382019-10-02 09:09:31 +030012014 mbedtls_platform_memcpy( ssl->cur_out_ctr, p, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012015 p += 8;
12016
12017#if defined(MBEDTLS_SSL_PROTO_DTLS)
12018 if( (size_t)( end - p ) < 2 )
12019 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030012020 ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012021 p += 2;
12022#endif /* MBEDTLS_SSL_PROTO_DTLS */
12023
12024#if defined(MBEDTLS_SSL_ALPN)
12025 {
12026 uint8_t alpn_len;
12027 const char **cur;
12028
12029 if( (size_t)( end - p ) < 1 )
12030 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12031
12032 alpn_len = *p++;
12033
12034 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
12035 {
12036 /* alpn_chosen should point to an item in the configured list */
12037 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
12038 {
12039 if( strlen( *cur ) == alpn_len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +030012040 mbedtls_platform_memcmp( p, cur, alpn_len ) == 0 )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012041 {
12042 ssl->alpn_chosen = *cur;
12043 break;
12044 }
12045 }
12046 }
12047
12048 /* can only happen on conf mismatch */
12049 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
12050 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12051
12052 p += alpn_len;
12053 }
12054#endif /* MBEDTLS_SSL_ALPN */
12055
12056 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012057 * Forced fields from top-level ssl_context structure
12058 *
12059 * Most of them already set to the correct value by mbedtls_ssl_init() and
12060 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
12061 */
12062 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
12063
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012064#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012065 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012066#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
12067#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012068 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012069#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012070
Hanno Becker83985822019-08-30 10:42:49 +010012071 /* Adjust pointers for header fields of outgoing records to
12072 * the given transform, accounting for explicit IV and CID. */
12073 ssl_update_out_pointers( ssl, ssl->transform );
12074
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012075#if defined(MBEDTLS_SSL_PROTO_DTLS)
12076 ssl->in_epoch = 1;
12077#endif
12078
12079 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
12080 * which we don't want - otherwise we'd end up freeing the wrong transform
12081 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
12082 if( ssl->handshake != NULL )
12083 {
12084 mbedtls_ssl_handshake_free( ssl );
12085 mbedtls_free( ssl->handshake );
12086 ssl->handshake = NULL;
12087 }
12088
12089 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020012090 * Done - should have consumed entire buffer
12091 */
12092 if( p != end )
12093 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012094
12095 return( 0 );
12096}
12097
12098/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020012099 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012100 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012101int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012102 const unsigned char *buf,
12103 size_t len )
12104{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012105 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012106
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012107 if( ret != 0 )
12108 mbedtls_ssl_free( context );
12109
12110 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012111}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020012112#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012113
12114/*
Paul Bakker5121ce52009-01-03 21:22:43 +000012115 * Free an SSL context
12116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012117void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000012118{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020012119 if( ssl == NULL )
12120 return;
12121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012123
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012124 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012125 {
Angus Grattond8213d02016-05-25 20:56:48 +100012126 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012127 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012128 }
12129
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012130 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012131 {
Angus Grattond8213d02016-05-25 20:56:48 +100012132 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012133 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012134 }
12135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012136#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020012137 if( ssl->compress_buf != NULL )
12138 {
Angus Grattond8213d02016-05-25 20:56:48 +100012139 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012140 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020012141 }
12142#endif
12143
Paul Bakker48916f92012-09-16 19:57:18 +000012144 if( ssl->transform )
12145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012146 mbedtls_ssl_transform_free( ssl->transform );
12147 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000012148 }
12149
12150 if( ssl->handshake )
12151 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020012152 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012153 mbedtls_ssl_transform_free( ssl->transform_negotiate );
12154 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012156 mbedtls_free( ssl->handshake );
12157 mbedtls_free( ssl->transform_negotiate );
12158 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012159 }
12160
Paul Bakkerc0463502013-02-14 11:19:38 +010012161 if( ssl->session )
12162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012163 mbedtls_ssl_session_free( ssl->session );
12164 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010012165 }
12166
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030012167#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020012168 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012169 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012170 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012171 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000012172 }
Paul Bakker0be444a2013-08-27 21:55:01 +020012173#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000012174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012175#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
12176 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000012177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
12179 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000012180 }
12181#endif
12182
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012183#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012184 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020012185#endif
12186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000012188
Paul Bakker86f04f42013-02-14 11:20:09 +010012189 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012190 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012191}
12192
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012193/*
12194 * Initialze mbedtls_ssl_config
12195 */
12196void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
12197{
12198 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010012199
12200#if !defined(MBEDTLS_SSL_PROTO_TLS)
12201 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
12202#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012203}
12204
Simon Butcherc97b6972015-12-27 23:48:17 +000012205#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012206#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012207static int ssl_preset_default_hashes[] = {
12208#if defined(MBEDTLS_SHA512_C)
12209 MBEDTLS_MD_SHA512,
12210 MBEDTLS_MD_SHA384,
12211#endif
12212#if defined(MBEDTLS_SHA256_C)
12213 MBEDTLS_MD_SHA256,
12214 MBEDTLS_MD_SHA224,
12215#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020012216#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012217 MBEDTLS_MD_SHA1,
12218#endif
12219 MBEDTLS_MD_NONE
12220};
Simon Butcherc97b6972015-12-27 23:48:17 +000012221#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012222#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012223
Hanno Becker73f4cb12019-06-27 13:51:07 +010012224#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012225static int ssl_preset_suiteb_ciphersuites[] = {
12226 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
12227 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
12228 0
12229};
Hanno Becker73f4cb12019-06-27 13:51:07 +010012230#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012231
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012232#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012233#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012234static int ssl_preset_suiteb_hashes[] = {
12235 MBEDTLS_MD_SHA256,
12236 MBEDTLS_MD_SHA384,
12237 MBEDTLS_MD_NONE
12238};
12239#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012240#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012241
Hanno Beckerc1096e72019-06-19 12:30:41 +010012242#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012243static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010012244#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012245 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012246#endif
12247#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012248 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012249#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012250 MBEDTLS_ECP_DP_NONE
12251};
12252#endif
12253
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012254/*
Tillmann Karras588ad502015-09-25 04:27:22 +020012255 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012256 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012257int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012258 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012259{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012260#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012261 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012262#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012263
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020012264 /* Use the functions here so that they are covered in tests,
12265 * but otherwise access member directly for efficiency */
12266 mbedtls_ssl_conf_endpoint( conf, endpoint );
12267 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012268
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012269 /*
12270 * Things that are common to all presets
12271 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012272#if defined(MBEDTLS_SSL_CLI_C)
12273 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
12274 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012275#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012276 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012277#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012278#if defined(MBEDTLS_SSL_SESSION_TICKETS)
12279 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
12280#endif
12281 }
12282#endif
12283
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012284#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012285 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012286#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012287
12288#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
12289 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
12290#endif
12291
12292#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010012293#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012294 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012295#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
12296#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030012297 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030012298 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012299#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012300#endif
12301
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010012302#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
12303 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
12304#endif
12305
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012306#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012307 conf->f_cookie_write = ssl_cookie_write_dummy;
12308 conf->f_cookie_check = ssl_cookie_check_dummy;
12309#endif
12310
Hanno Becker7f376f42019-06-12 16:20:48 +010012311#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
12312 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012313 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
12314#endif
12315
Janos Follath088ce432017-04-10 12:42:31 +010012316#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012317#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010012318 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012319#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
12320#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010012321
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012322#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010012323#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012324 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012325#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
12326#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012327 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012328#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
12329#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012330
12331#if defined(MBEDTLS_SSL_RENEGOTIATION)
12332 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020012333 mbedtls_platform_memset( conf->renego_period, 0x00, 2 );
12334 mbedtls_platform_memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012335#endif
12336
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012337#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
12338 if( endpoint == MBEDTLS_SSL_IS_SERVER )
12339 {
Hanno Becker00d0a682017-10-04 13:14:29 +010012340 const unsigned char dhm_p[] =
12341 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
12342 const unsigned char dhm_g[] =
12343 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
12344
Hanno Beckera90658f2017-10-04 15:29:08 +010012345 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
12346 dhm_p, sizeof( dhm_p ),
12347 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012348 {
12349 return( ret );
12350 }
12351 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020012352#endif
12353
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012354 /*
12355 * Preset-specific defaults
12356 */
12357 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012358 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012359 /*
12360 * NSA Suite B
12361 */
12362 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010012363#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012364 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010012365#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12366#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012367 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010012368#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12369#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012370 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012371#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12372#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012373 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012374#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012375
Hanno Becker73f4cb12019-06-27 13:51:07 +010012376#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012377 conf->ciphersuite_list[0] =
12378 conf->ciphersuite_list[1] =
12379 conf->ciphersuite_list[2] =
12380 conf->ciphersuite_list[3] =
12381 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010012382#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012383
12384#if defined(MBEDTLS_X509_CRT_PARSE_C)
12385 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012386#endif
12387
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012388#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012389#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012390 conf->sig_hashes = ssl_preset_suiteb_hashes;
12391#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012392#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012393
12394#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012395#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012396 conf->curve_list = ssl_preset_suiteb_curves;
12397#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012398#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020012399 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012400
12401 /*
12402 * Default
12403 */
12404 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010012405#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012406 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
12407 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
12408 MBEDTLS_SSL_MIN_MAJOR_VERSION :
12409 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012410#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12411#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012412 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
12413 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
12414 MBEDTLS_SSL_MIN_MINOR_VERSION :
12415 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012416#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020012417 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012418 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
12419#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010012420#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12421#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
12422 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12423#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12424#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12425 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12426#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012427
Hanno Becker73f4cb12019-06-27 13:51:07 +010012428#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012429 conf->ciphersuite_list[0] =
12430 conf->ciphersuite_list[1] =
12431 conf->ciphersuite_list[2] =
12432 conf->ciphersuite_list[3] =
12433 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012434#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012435
12436#if defined(MBEDTLS_X509_CRT_PARSE_C)
12437 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12438#endif
12439
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012440#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012441#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012442 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012443#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012444#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012445
12446#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012447#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012448 conf->curve_list = mbedtls_ecp_grp_id_list();
12449#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012450#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012451
12452#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12453 conf->dhm_min_bitlen = 1024;
12454#endif
12455 }
12456
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012457 return( 0 );
12458}
12459
12460/*
12461 * Free mbedtls_ssl_config
12462 */
12463void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12464{
12465#if defined(MBEDTLS_DHM_C)
12466 mbedtls_mpi_free( &conf->dhm_P );
12467 mbedtls_mpi_free( &conf->dhm_G );
12468#endif
12469
12470#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12471 if( conf->psk != NULL )
12472 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012473 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012474 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012475 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012476 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012477 }
12478
12479 if( conf->psk_identity != NULL )
12480 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012481 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012482 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012483 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012484 conf->psk_identity_len = 0;
12485 }
12486#endif
12487
12488#if defined(MBEDTLS_X509_CRT_PARSE_C)
12489 ssl_key_cert_free( conf->key_cert );
12490#endif
12491
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012492 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012493}
12494
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012495#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012496 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12497 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012498/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012499 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012500 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012501unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012502{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012503#if defined(MBEDTLS_RSA_C)
12504 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12505 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012506#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012507#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012508 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12509 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012510#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012511 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012512}
12513
Hanno Becker7e5437a2017-04-28 17:15:26 +010012514unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12515{
12516 switch( type ) {
12517 case MBEDTLS_PK_RSA:
12518 return( MBEDTLS_SSL_SIG_RSA );
12519 case MBEDTLS_PK_ECDSA:
12520 case MBEDTLS_PK_ECKEY:
12521 return( MBEDTLS_SSL_SIG_ECDSA );
12522 default:
12523 return( MBEDTLS_SSL_SIG_ANON );
12524 }
12525}
12526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012527mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012528{
12529 switch( sig )
12530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012531#if defined(MBEDTLS_RSA_C)
12532 case MBEDTLS_SSL_SIG_RSA:
12533 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012534#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012535#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012536 case MBEDTLS_SSL_SIG_ECDSA:
12537 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012538#endif
12539 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012540 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012541 }
12542}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012543#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012544
Hanno Becker7e5437a2017-04-28 17:15:26 +010012545#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12546 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12547
12548/* Find an entry in a signature-hash set matching a given hash algorithm. */
12549mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12550 mbedtls_pk_type_t sig_alg )
12551{
12552 switch( sig_alg )
12553 {
12554 case MBEDTLS_PK_RSA:
12555 return( set->rsa );
12556 case MBEDTLS_PK_ECDSA:
12557 return( set->ecdsa );
12558 default:
12559 return( MBEDTLS_MD_NONE );
12560 }
12561}
12562
12563/* Add a signature-hash-pair to a signature-hash set */
12564void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12565 mbedtls_pk_type_t sig_alg,
12566 mbedtls_md_type_t md_alg )
12567{
12568 switch( sig_alg )
12569 {
12570 case MBEDTLS_PK_RSA:
12571 if( set->rsa == MBEDTLS_MD_NONE )
12572 set->rsa = md_alg;
12573 break;
12574
12575 case MBEDTLS_PK_ECDSA:
12576 if( set->ecdsa == MBEDTLS_MD_NONE )
12577 set->ecdsa = md_alg;
12578 break;
12579
12580 default:
12581 break;
12582 }
12583}
12584
12585/* Allow exactly one hash algorithm for each signature. */
12586void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12587 mbedtls_md_type_t md_alg )
12588{
12589 set->rsa = md_alg;
12590 set->ecdsa = md_alg;
12591}
12592
12593#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12594 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12595
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012596/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012597 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012599mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012600{
12601 switch( hash )
12602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012603#if defined(MBEDTLS_MD5_C)
12604 case MBEDTLS_SSL_HASH_MD5:
12605 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012606#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012607#if defined(MBEDTLS_SHA1_C)
12608 case MBEDTLS_SSL_HASH_SHA1:
12609 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012610#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012611#if defined(MBEDTLS_SHA256_C)
12612 case MBEDTLS_SSL_HASH_SHA224:
12613 return( MBEDTLS_MD_SHA224 );
12614 case MBEDTLS_SSL_HASH_SHA256:
12615 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012616#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012617#if defined(MBEDTLS_SHA512_C)
12618 case MBEDTLS_SSL_HASH_SHA384:
12619 return( MBEDTLS_MD_SHA384 );
12620 case MBEDTLS_SSL_HASH_SHA512:
12621 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012622#endif
12623 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012624 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012625 }
12626}
12627
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012628/*
12629 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12630 */
12631unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12632{
12633 switch( md )
12634 {
12635#if defined(MBEDTLS_MD5_C)
12636 case MBEDTLS_MD_MD5:
12637 return( MBEDTLS_SSL_HASH_MD5 );
12638#endif
12639#if defined(MBEDTLS_SHA1_C)
12640 case MBEDTLS_MD_SHA1:
12641 return( MBEDTLS_SSL_HASH_SHA1 );
12642#endif
12643#if defined(MBEDTLS_SHA256_C)
12644 case MBEDTLS_MD_SHA224:
12645 return( MBEDTLS_SSL_HASH_SHA224 );
12646 case MBEDTLS_MD_SHA256:
12647 return( MBEDTLS_SSL_HASH_SHA256 );
12648#endif
12649#if defined(MBEDTLS_SHA512_C)
12650 case MBEDTLS_MD_SHA384:
12651 return( MBEDTLS_SSL_HASH_SHA384 );
12652 case MBEDTLS_MD_SHA512:
12653 return( MBEDTLS_SSL_HASH_SHA512 );
12654#endif
12655 default:
12656 return( MBEDTLS_SSL_HASH_NONE );
12657 }
12658}
12659
Hanno Beckeree902df2019-08-23 13:47:47 +010012660#if defined(MBEDTLS_USE_TINYCRYPT)
12661/*
12662 * Check if a curve proposed by the peer is in our list.
12663 * Return 0 if we're willing to use it, -1 otherwise.
12664 */
12665int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12666 mbedtls_uecc_group_id grp_id )
12667{
12668 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12669 if( own_ec_id == grp_id )
12670 return( 0 );
12671 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12672
12673 return( -1 );
12674}
12675#endif /* MBEDTLS_USE_TINYCRYPT */
12676
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012677#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012678/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012679 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012680 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012681 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012682int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12683 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012684{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012685 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12686 if( own_ec_id == grp_id )
12687 return( 0 );
12688 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012689
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012690 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012691}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012692#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012693
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012694#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012695/*
12696 * Check if a hash proposed by the peer is in our list.
12697 * Return 0 if we're willing to use it, -1 otherwise.
12698 */
12699int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12700 mbedtls_md_type_t md )
12701{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012702 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12703 if( md_alg == md )
12704 return( 0 );
12705 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012706
12707 return( -1 );
12708}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012709#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012711#if defined(MBEDTLS_X509_CRT_PARSE_C)
12712int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012713 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012714 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012715 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012716{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012717 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012718#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012719 int usage = 0;
12720#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012721#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012722 const char *ext_oid;
12723 size_t ext_len;
12724#endif
12725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012726#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12727 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012728 ((void) cert);
12729 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012730 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012731#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012733#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12734 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012735 {
12736 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012737 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012739 case MBEDTLS_KEY_EXCHANGE_RSA:
12740 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012741 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012742 break;
12743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012744 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12745 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12746 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12747 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012748 break;
12749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012750 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12751 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012752 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012753 break;
12754
12755 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012756 case MBEDTLS_KEY_EXCHANGE_NONE:
12757 case MBEDTLS_KEY_EXCHANGE_PSK:
12758 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12759 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012760 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012761 usage = 0;
12762 }
12763 }
12764 else
12765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012766 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12767 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012768 }
12769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012770 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012771 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012772 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012773 ret = -1;
12774 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012775#else
12776 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012777#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012779#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12780 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012782 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12783 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012784 }
12785 else
12786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012787 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12788 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012789 }
12790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012791 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012792 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012793 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012794 ret = -1;
12795 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012796#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012797
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012798 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012799}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012800#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012801
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012802#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12803 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12804int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12805 unsigned char *output,
12806 unsigned char *data, size_t data_len )
12807{
12808 int ret = 0;
12809 mbedtls_md5_context mbedtls_md5;
12810 mbedtls_sha1_context mbedtls_sha1;
12811
12812 mbedtls_md5_init( &mbedtls_md5 );
12813 mbedtls_sha1_init( &mbedtls_sha1 );
12814
12815 /*
12816 * digitally-signed struct {
12817 * opaque md5_hash[16];
12818 * opaque sha_hash[20];
12819 * };
12820 *
12821 * md5_hash
12822 * MD5(ClientHello.random + ServerHello.random
12823 * + ServerParams);
12824 * sha_hash
12825 * SHA(ClientHello.random + ServerHello.random
12826 * + ServerParams);
12827 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012828 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012829 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012830 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012831 goto exit;
12832 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012833 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012834 ssl->handshake->randbytes, 64 ) ) != 0 )
12835 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012836 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012837 goto exit;
12838 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012839 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012840 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012841 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012842 goto exit;
12843 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012844 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012845 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012846 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012847 goto exit;
12848 }
12849
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012850 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012851 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012852 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_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_sha1_update_ret( &mbedtls_sha1,
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_sha1_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_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012862 data_len ) ) != 0 )
12863 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012864 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012865 goto exit;
12866 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012867 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012868 output + 16 ) ) != 0 )
12869 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012870 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012871 goto exit;
12872 }
12873
12874exit:
12875 mbedtls_md5_free( &mbedtls_md5 );
12876 mbedtls_sha1_free( &mbedtls_sha1 );
12877
12878 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012879 mbedtls_ssl_pend_fatal_alert( ssl,
12880 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012881
12882 return( ret );
12883
12884}
12885#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12886 MBEDTLS_SSL_PROTO_TLS1_1 */
12887
12888#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12889 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12890int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012891 unsigned char *hash, size_t *hashlen,
12892 unsigned char *data, size_t data_len,
12893 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012894{
12895 int ret = 0;
12896 mbedtls_md_context_t ctx;
Hanno Beckera5cedbc2019-07-17 11:21:02 +010012897 mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012898 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012899
12900 mbedtls_md_init( &ctx );
12901
12902 /*
12903 * digitally-signed struct {
12904 * opaque client_random[32];
12905 * opaque server_random[32];
12906 * ServerDHParams params;
12907 * };
12908 */
12909 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12910 {
12911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12912 goto exit;
12913 }
12914 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12915 {
12916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12917 goto exit;
12918 }
12919 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12920 {
12921 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12922 goto exit;
12923 }
12924 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12925 {
12926 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12927 goto exit;
12928 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012929 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012930 {
12931 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12932 goto exit;
12933 }
12934
12935exit:
12936 mbedtls_md_free( &ctx );
12937
12938 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012939 mbedtls_ssl_pend_fatal_alert( ssl,
12940 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012941
12942 return( ret );
12943}
12944#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12945 MBEDTLS_SSL_PROTO_TLS1_2 */
12946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012947#endif /* MBEDTLS_SSL_TLS_C */