blob: fea3b8a49ff4f560efe6f437baf14af061340989 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Jarno Lamsaaf60cd72019-12-19 16:45:23 +020051#include "mbedtls/platform.h"
52
Paul Bakker0be444a2013-08-27 21:55:01 +020053
Rich Evans00ab4702015-02-06 13:43:58 +000054#include <string.h>
55
Janos Follath23bdca02016-10-07 14:47:14 +010056#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020058#endif
59
Hanno Beckeref982d52019-07-23 15:56:18 +010060#if defined(MBEDTLS_USE_TINYCRYPT)
61static int uecc_rng_wrapper( uint8_t *dest, unsigned int size )
62{
Hanno Beckerd089fad2019-07-24 09:05:05 +010063 int ret;
64 ret = mbedtls_ssl_conf_rng_func( NULL, dest, size );
65 if( ret == 0 )
66 return( (int) size );
67
68 return( 0 );
Hanno Beckeref982d52019-07-23 15:56:18 +010069}
Hanno Becker75f12d12019-07-23 16:16:15 +010070
71int mbedtls_ssl_ecdh_read_peerkey( mbedtls_ssl_context *ssl,
72 unsigned char **p, unsigned char *end )
73{
74 size_t const secp256r1_uncompressed_point_length =
75 1 /* length */ + 1 /* length */ + 2 * NUM_ECC_BYTES /* data */;
76
77 if( (size_t)( end - *p ) < secp256r1_uncompressed_point_length )
78 {
79 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Bad ECDH peer pubkey (too short)" ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010081 }
82
83 if( (*p)[0] != 2 * NUM_ECC_BYTES + 1 ||
84 (*p)[1] != 0x04 )
85 {
86 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Unexpected ECDH peer pubkey header - expected { %#02x, %#02x }, got { %#02x, %#02x }",
87 2 * NUM_ECC_BYTES + 1,
88 0x04,
89 (unsigned) (*p)[0],
90 (unsigned) (*p)[1] ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010092 }
93
Teppo Järvelin91d79382019-10-02 09:09:31 +030094 mbedtls_platform_memcpy( ssl->handshake->ecdh_peerkey, *p + 2, 2 * NUM_ECC_BYTES );
Hanno Becker75f12d12019-07-23 16:16:15 +010095
96 *p += secp256r1_uncompressed_point_length;
97 return( 0 );
98}
Hanno Beckeref982d52019-07-23 15:56:18 +010099#endif /* MBEDTLS_USE_TINYCRYPT */
100
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100101static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +0100102static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100103
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100104/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100106{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200107#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +0100108 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100109#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200110
111#if defined(MBEDTLS_SSL_PROTO_DTLS)
112 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
113 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200114 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200115#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200116#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100117 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200118#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100119}
120
Hanno Beckerb82350b2019-07-26 07:24:05 +0100121static void ssl_send_pending_fatal_alert( mbedtls_ssl_context *ssl )
122{
123 if( ssl->pending_fatal_alert_msg == MBEDTLS_SSL_ALERT_MSG_NONE )
124 return;
125
126 mbedtls_ssl_send_alert_message( ssl,
127 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
128 ssl->pending_fatal_alert_msg );
129 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
130}
131
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200132/*
133 * Start a timer.
134 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200137{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100138 if( mbedtls_ssl_get_set_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200139 return;
140
141 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
Hanno Becker0ae6b242019-06-13 16:45:36 +0100142 mbedtls_ssl_get_set_timer( ssl )( ssl->p_timer,
143 millisecs / 4,
144 millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200145}
146
147/*
148 * Return -1 is timer is expired, 0 if it isn't.
149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200151{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100152 if( mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200153 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200154
Hanno Becker0ae6b242019-06-13 16:45:36 +0100155 if( mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200156 {
157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200158 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200159 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200160
161 return( 0 );
162}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200163
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100164static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
165 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100166static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100167
Hanno Becker02f26092019-07-03 16:13:00 +0100168#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker03e2db62019-07-12 14:40:00 +0100169static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
170 unsigned char *buf,
171 size_t len,
172 mbedtls_record *rec );
173
Hanno Becker02f26092019-07-03 16:13:00 +0100174int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
175 unsigned char *buf,
176 size_t buflen )
177{
Hanno Becker03e2db62019-07-12 14:40:00 +0100178 int ret = 0;
179 mbedtls_record rec;
180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
181 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
182
183 /* We don't support record checking in TLS because
184 * (a) there doesn't seem to be a usecase for it, and
185 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
186 * and we'd need to backup the transform here.
187 */
188#if defined(MBEDTLS_SSL_PROTO_TLS)
189 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
190 {
191 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
192 goto exit;
193 }
194 MBEDTLS_SSL_TRANSPORT_ELSE
195#endif /* MBEDTLS_SSL_PROTO_TLS */
196#if defined(MBEDTLS_SSL_PROTO_DTLS)
197 {
198 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
199 if( ret != 0 )
200 {
201 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
202 goto exit;
203 }
204
205 if( ssl->transform_in != NULL )
206 {
207 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
208 if( ret != 0 )
209 {
210 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
211 goto exit;
212 }
213 }
214 }
215#endif /* MBEDTLS_SSL_PROTO_DTLS */
216
217exit:
218 /* On success, we have decrypted the buffer in-place, so make
219 * sure we don't leak any plaintext data. */
220 mbedtls_platform_zeroize( buf, buflen );
221
222 /* For the purpose of this API, treat messages with unexpected CID
223 * as well as such from future epochs as unexpected. */
224 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
225 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
226 {
227 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
228 }
229
230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
231 return( ret );
Hanno Becker02f26092019-07-03 16:13:00 +0100232}
233#endif /* MBEDTLS_SSL_RECORD_CHECKING */
234
Hanno Becker67bc7c32018-08-06 11:33:50 +0100235#define SSL_DONT_FORCE_FLUSH 0
236#define SSL_FORCE_FLUSH 1
237
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200238#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100239
Hanno Beckera5a2b082019-05-15 14:03:01 +0100240#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100241/* Top-level Connection ID API */
242
Hanno Beckere0200da2019-06-13 09:23:43 +0100243#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
244 !defined(MBEDTLS_SSL_CONF_CID_LEN) && \
245 !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100246int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
247 size_t len,
248 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100249{
250 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
251 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
252
Hanno Becker791ec6b2019-05-14 11:45:26 +0100253 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
254 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
255 {
256 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
257 }
258
259 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100260 conf->cid_len = len;
261 return( 0 );
262}
Hanno Beckere0200da2019-06-13 09:23:43 +0100263#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
264 !MBEDTLS_SSL_CONF_CID_LEN &&
265 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
266
267#if MBEDTLS_SSL_CONF_CID_LEN > MBEDTLS_SSL_CID_IN_LEN_MAX
268#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_CID_LEN"
269#endif
270#if MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE && \
271 MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_FAIL
272#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID"
273#endif
274
275#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
276 !MBEDTLS_SSL_CONF_CID_LEN &&
277 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
Hanno Beckereec2be92019-05-03 13:06:44 +0100278
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100279int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
280 int enable,
281 unsigned char const *own_cid,
282 size_t own_cid_len )
283{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200284 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100285 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
286
Hanno Becker07489862019-04-25 16:01:49 +0100287 ssl->negotiate_cid = enable;
288 if( enable == MBEDTLS_SSL_CID_DISABLED )
289 {
290 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
291 return( 0 );
292 }
293 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100294 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100295
Hanno Beckere0200da2019-06-13 09:23:43 +0100296 if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) )
Hanno Becker07489862019-04-25 16:01:49 +0100297 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100298 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
299 (unsigned) own_cid_len,
Hanno Beckere0200da2019-06-13 09:23:43 +0100300 (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) );
Hanno Becker07489862019-04-25 16:01:49 +0100301 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
302 }
303
Teppo Järvelin6f4e0302019-10-04 13:53:53 +0300304 /* Not using more secure mbedtls_platform_memcpy as cid is public */
305 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100306 /* Truncation is not an issue here because
307 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
308 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100309
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100310 return( 0 );
311}
312
313int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
314 int *enabled,
315 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
316 size_t *peer_cid_len )
317{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100318 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100319
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200320 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100321 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
322 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100323 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100324 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100325
Hanno Beckercb063f52019-05-03 12:54:52 +0100326 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
327 * were used, but client and server requested the empty CID.
328 * This is indistinguishable from not using the CID extension
329 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100330 if( ssl->transform_in->in_cid_len == 0 &&
331 ssl->transform_in->out_cid_len == 0 )
332 {
333 return( 0 );
334 }
335
Hanno Becker633d6042019-05-22 16:50:35 +0100336 if( peer_cid_len != NULL )
337 {
338 *peer_cid_len = ssl->transform_in->out_cid_len;
339 if( peer_cid != NULL )
340 {
Teppo Järvelin6f4e0302019-10-04 13:53:53 +0300341 /* Not using more secure mbedtls_platform_memcpy as cid is public */
342 memcpy( peer_cid, ssl->transform_in->out_cid,
Hanno Becker633d6042019-05-22 16:50:35 +0100343 ssl->transform_in->out_cid_len );
344 }
345 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100346
347 *enabled = MBEDTLS_SSL_CID_ENABLED;
348
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100349 return( 0 );
350}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100351#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100352
Hanno Beckerd5847772018-08-28 10:09:23 +0100353/* Forward declarations for functions related to message buffering. */
354static void ssl_buffering_free( mbedtls_ssl_context *ssl );
355static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
356 uint8_t slot );
357static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
358static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
359static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
360static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +0100361static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
362 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100363static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100364
Hanno Beckera67dee22018-08-22 10:05:20 +0100365static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100366static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100367{
Hanno Becker11682cc2018-08-22 14:41:02 +0100368 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100369
370 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100371 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100372
373 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
374}
375
Hanno Becker67bc7c32018-08-06 11:33:50 +0100376static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
377{
Hanno Becker11682cc2018-08-22 14:41:02 +0100378 size_t const bytes_written = ssl->out_left;
379 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100380
381 /* Double-check that the write-index hasn't gone
382 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100383 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100384 {
385 /* Should never happen... */
386 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
387 }
388
389 return( (int) ( mtu - bytes_written ) );
390}
391
392static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
393{
394 int ret;
395 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400396 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100397
398#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
399 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
400
401 if( max_len > mfl )
402 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100403
404 /* By the standard (RFC 6066 Sect. 4), the MFL extension
405 * only limits the maximum record payload size, so in theory
406 * we would be allowed to pack multiple records of payload size
407 * MFL into a single datagram. However, this would mean that there's
408 * no way to explicitly communicate MTU restrictions to the peer.
409 *
410 * The following reduction of max_len makes sure that we never
411 * write datagrams larger than MFL + Record Expansion Overhead.
412 */
413 if( max_len <= ssl->out_left )
414 return( 0 );
415
416 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100417#endif
418
419 ret = ssl_get_remaining_space_in_datagram( ssl );
420 if( ret < 0 )
421 return( ret );
422 remaining = (size_t) ret;
423
424 ret = mbedtls_ssl_get_record_expansion( ssl );
425 if( ret < 0 )
426 return( ret );
427 expansion = (size_t) ret;
428
429 if( remaining <= expansion )
430 return( 0 );
431
432 remaining -= expansion;
433 if( remaining >= max_len )
434 remaining = max_len;
435
436 return( (int) remaining );
437}
438
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200439/*
440 * Double the retransmit timeout value, within the allowed range,
441 * returning -1 if the maximum value has already been reached.
442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200444{
445 uint32_t new_timeout;
446
Hanno Becker1f835fa2019-06-13 10:14:59 +0100447 if( ssl->handshake->retransmit_timeout >=
448 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
449 {
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200450 return( -1 );
Hanno Becker1f835fa2019-06-13 10:14:59 +0100451 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200452
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200453 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
454 * in the following way: after the initial transmission and a first
455 * retransmission, back off to a temporary estimated MTU of 508 bytes.
456 * This value is guaranteed to be deliverable (if not guaranteed to be
457 * delivered) of any compliant IPv4 (and IPv6) network, and should work
458 * on most non-IP stacks too. */
Hanno Becker1f835fa2019-06-13 10:14:59 +0100459 if( ssl->handshake->retransmit_timeout !=
460 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400461 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200462 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
464 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200465
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200466 new_timeout = 2 * ssl->handshake->retransmit_timeout;
467
468 /* Avoid arithmetic overflow and range overflow */
469 if( new_timeout < ssl->handshake->retransmit_timeout ||
Hanno Becker1f835fa2019-06-13 10:14:59 +0100470 new_timeout > mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200471 {
Hanno Becker1f835fa2019-06-13 10:14:59 +0100472 new_timeout = mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200473 }
474
475 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200477 ssl->handshake->retransmit_timeout ) );
478
479 return( 0 );
480}
481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200483{
Hanno Becker1f835fa2019-06-13 10:14:59 +0100484 ssl->handshake->retransmit_timeout = mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200486 ssl->handshake->retransmit_timeout ) );
487}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200491/*
492 * Convert max_fragment_length codes to length.
493 * RFC 6066 says:
494 * enum{
495 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
496 * } MaxFragmentLength;
497 * and we add 0 -> extension unused
498 */
Angus Grattond8213d02016-05-25 20:56:48 +1000499static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200500{
Angus Grattond8213d02016-05-25 20:56:48 +1000501 switch( mfl )
502 {
503 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300504 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000505 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
506 return 512;
507 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
508 return 1024;
509 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
510 return 2048;
511 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
512 return 4096;
513 default:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300514 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000515 }
516}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200518
Hanno Becker58fccf22019-02-06 14:30:46 +0000519int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
520 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200521{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_ssl_session_free( dst );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300523 mbedtls_platform_memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000526
527#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200528 if( src->peer_cert != NULL )
529 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200530 int ret;
531
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200532 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200533 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200534 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200539 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200542 dst->peer_cert = NULL;
543 return( ret );
544 }
545 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100546#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000547 if( src->peer_cert_digest != NULL )
548 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000549 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000550 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000551 if( dst->peer_cert_digest == NULL )
552 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
553
Teppo Järvelin91d79382019-10-02 09:09:31 +0300554 mbedtls_platform_memcpy( dst->peer_cert_digest, src->peer_cert_digest,
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000555 src->peer_cert_digest_len );
556 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000557 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000558 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100559#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200562
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200563#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200564 if( src->ticket != NULL )
565 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200566 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200567 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200568 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200569
Teppo Järvelin91d79382019-10-02 09:09:31 +0300570 mbedtls_platform_memcpy( dst->ticket, src->ticket, src->ticket_len );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200571 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200572#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200573
574 return( 0 );
575}
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
578int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200579 const unsigned char *key_enc, const unsigned char *key_dec,
580 size_t keylen,
581 const unsigned char *iv_enc, const unsigned char *iv_dec,
582 size_t ivlen,
583 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200584 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
586int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
587int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
588int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
589int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
590#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000591
Paul Bakker5121ce52009-01-03 21:22:43 +0000592/*
593 * Key material generation
594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100596MBEDTLS_NO_INLINE static int ssl3_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200597 const char *label,
598 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000599 unsigned char *dstbuf, size_t dlen )
600{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100601 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000602 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200603 mbedtls_md5_context md5;
604 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000605 unsigned char padding[16];
606 unsigned char sha1sum[20];
607 ((void)label);
608
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200609 mbedtls_md5_init( &md5 );
610 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200611
Paul Bakker5f70b252012-09-13 14:23:06 +0000612 /*
613 * SSLv3:
614 * block =
615 * MD5( secret + SHA1( 'A' + secret + random ) ) +
616 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
617 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
618 * ...
619 */
620 for( i = 0; i < dlen / 16; i++ )
621 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200622 mbedtls_platform_memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000623
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100624 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100625 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100626 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100627 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100628 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100629 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100630 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100631 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100632 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100633 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000634
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100635 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100636 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100637 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100638 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100639 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100640 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100641 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100642 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000643 }
644
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100645exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200646 mbedtls_md5_free( &md5 );
647 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000648
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500649 mbedtls_platform_zeroize( padding, sizeof( padding ) );
650 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000651
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100652 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000653}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100657MBEDTLS_NO_INLINE static int tls1_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200658 const char *label,
659 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000660 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000661{
Paul Bakker23986e52011-04-24 08:57:21 +0000662 size_t nb, hs;
663 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200664 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 unsigned char tmp[128];
666 unsigned char h_i[20];
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100667 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100669 int ret;
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
673 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
676 hs = ( slen + 1 ) / 2;
677 S1 = secret;
678 S2 = secret + slen - hs;
679
680 nb = strlen( label );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300681 mbedtls_platform_memcpy( tmp + 20, label, nb );
682 mbedtls_platform_memcpy( tmp + 20 + nb, random, rlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 nb += rlen;
684
685 /*
686 * First compute P_md5(secret,label+random)[0..dlen]
687 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100688 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) ==
689 MBEDTLS_MD_INVALID_HANDLE )
690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100692 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100695 return( ret );
696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
698 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
699 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000700
701 for( i = 0; i < dlen; i += 16 )
702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_md_hmac_reset ( &md_ctx );
704 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
705 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_md_hmac_reset ( &md_ctx );
708 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
709 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
711 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
712
713 for( j = 0; j < k; j++ )
714 dstbuf[i + j] = h_i[j];
715 }
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100718
Paul Bakker5121ce52009-01-03 21:22:43 +0000719 /*
720 * XOR out with P_sha1(secret,label+random)[0..dlen]
721 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100722 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) ==
723 MBEDTLS_MD_INVALID_HANDLE )
724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100726 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100729 return( ret );
730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
732 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
733 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
735 for( i = 0; i < dlen; i += 20 )
736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_md_hmac_reset ( &md_ctx );
738 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
739 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_md_hmac_reset ( &md_ctx );
742 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
743 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000744
745 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
746
747 for( j = 0; j < k; j++ )
748 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
749 }
750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100752
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500753 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
754 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
756 return( 0 );
757}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerf6cc7422019-08-16 14:34:52 +0100761#if !( defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_SHA512_C) )
762MBEDTLS_ALWAYS_INLINE static inline
763#else
764static
765#endif
766int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100767 const unsigned char *secret, size_t slen,
768 const char *label,
769 const unsigned char *random, size_t rlen,
770 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000771{
772 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100773 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000774 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100776 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100778 int ret;
779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000781
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100782 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) ==
783 MBEDTLS_MD_INVALID_HANDLE )
784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100786 }
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100789
790 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000792
793 nb = strlen( label );
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200794 (void)mbedtls_platform_memcpy( tmp + md_len, label, nb );
795 (void)mbedtls_platform_memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000796 nb += rlen;
797
798 /*
799 * Compute P_<hash>(secret, label + random)[0..dlen]
800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100802 return( ret );
803
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200804 if ( ( ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ) ) != 0 )
805 return( ret );
806 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ) ) != 0 )
807 return( ret );
808 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, tmp ) ) != 0 )
809 return( ret );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100810
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100811 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000812 {
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200813 if ( ( ret = mbedtls_md_hmac_reset ( &md_ctx ) ) != 0 )
814 return( ret );
815 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ) ) != 0 )
816 return( ret );
817 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, h_i ) ) != 0 )
818 return( ret );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100819
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200820 if ( ( ret = mbedtls_md_hmac_reset ( &md_ctx ) ) != 0 )
821 return( ret );
822 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ) ) != 0 )
823 return( ret );
824 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, tmp ) ) != 0 )
825 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000826
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100827 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000828
829 for( j = 0; j < k; j++ )
830 dstbuf[i + j] = h_i[j];
831 }
832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100834
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200835 (void)mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
836 (void)mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000837
838 return( 0 );
839}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100842MBEDTLS_NO_INLINE static int tls_prf_sha256(
843 const unsigned char *secret, size_t slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100844 const char *label,
845 const unsigned char *random, size_t rlen,
846 unsigned char *dstbuf, size_t dlen )
847{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100849 label, random, rlen, dstbuf, dlen ) );
850}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100854MBEDTLS_NO_INLINE static int tls_prf_sha384(
855 const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200856 const char *label,
857 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000858 unsigned char *dstbuf, size_t dlen )
859{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100861 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000862}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863#endif /* MBEDTLS_SHA512_C */
864#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000865
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100866/*
867 * Call the appropriate PRF function
868 */
Hanno Becker2793f742019-08-16 14:28:43 +0100869MBEDTLS_ALWAYS_INLINE static inline int ssl_prf( int minor_ver,
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100870 mbedtls_md_type_t hash,
871 const unsigned char *secret, size_t slen,
872 const char *label,
873 const unsigned char *random, size_t rlen,
874 unsigned char *dstbuf, size_t dlen )
875{
876#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
877 (void) hash;
878#endif
879
880#if defined(MBEDTLS_SSL_PROTO_SSL3)
881 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
882 return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
883 else
884#endif
885#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +0100886 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100887 return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
888 else
889#endif
890#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
891#if defined(MBEDTLS_SHA512_C)
892 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
893 hash == MBEDTLS_MD_SHA384 )
894 {
895 return( tls_prf_sha384( secret, slen, label, random, rlen,
896 dstbuf, dlen ) );
897 }
898 else
899#endif
900#if defined(MBEDTLS_SHA256_C)
901 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
902 {
903 return( tls_prf_sha256( secret, slen, label, random, rlen,
904 dstbuf, dlen ) );
905 }
906#endif
907#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
908
909 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
910}
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200911
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100912#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100913MBEDTLS_NO_INLINE static void ssl_calc_finished_ssl(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100914 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
915{
916 const char *sender;
917 mbedtls_md5_context md5;
918 mbedtls_sha1_context sha1;
919
920 unsigned char padbuf[48];
921 unsigned char md5sum[16];
922 unsigned char sha1sum[20];
923
924 mbedtls_ssl_session *session = ssl->session_negotiate;
925 if( !session )
926 session = ssl->session;
927
928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
929
930 mbedtls_md5_init( &md5 );
931 mbedtls_sha1_init( &sha1 );
932
933 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
934 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
935
936 /*
937 * SSLv3:
938 * hash =
939 * MD5( master + pad2 +
940 * MD5( handshake + sender + master + pad1 ) )
941 * + SHA1( master + pad2 +
942 * SHA1( handshake + sender + master + pad1 ) )
943 */
944
945#if !defined(MBEDTLS_MD5_ALT)
946 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
947 md5.state, sizeof( md5.state ) );
948#endif
949
950#if !defined(MBEDTLS_SHA1_ALT)
951 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
952 sha1.state, sizeof( sha1.state ) );
953#endif
954
955 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
956 : "SRVR";
957
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200958 mbedtls_platform_memset( padbuf, 0x36, 48 );
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100959
960 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
961 mbedtls_md5_update_ret( &md5, session->master, 48 );
962 mbedtls_md5_update_ret( &md5, padbuf, 48 );
963 mbedtls_md5_finish_ret( &md5, md5sum );
964
965 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
966 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
967 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
968 mbedtls_sha1_finish_ret( &sha1, sha1sum );
969
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200970 mbedtls_platform_memset( padbuf, 0x5C, 48 );
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100971
972 mbedtls_md5_starts_ret( &md5 );
973 mbedtls_md5_update_ret( &md5, session->master, 48 );
974 mbedtls_md5_update_ret( &md5, padbuf, 48 );
975 mbedtls_md5_update_ret( &md5, md5sum, 16 );
976 mbedtls_md5_finish_ret( &md5, buf );
977
978 mbedtls_sha1_starts_ret( &sha1 );
979 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
980 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
981 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
982 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
983
984 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
985
986 mbedtls_md5_free( &md5 );
987 mbedtls_sha1_free( &sha1 );
988
989 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
990 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
991 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
992
993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
994}
995#endif /* MBEDTLS_SSL_PROTO_SSL3 */
996
997#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100998MBEDTLS_NO_INLINE static void ssl_calc_finished_tls(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100999 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1000{
1001 int len = 12;
1002 const char *sender;
1003 mbedtls_md5_context md5;
1004 mbedtls_sha1_context sha1;
1005 unsigned char padbuf[36];
1006
1007 mbedtls_ssl_session *session = ssl->session_negotiate;
1008 if( !session )
1009 session = ssl->session;
1010
1011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
1012
1013 mbedtls_md5_init( &md5 );
1014 mbedtls_sha1_init( &sha1 );
1015
1016 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1017 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1018
1019 /*
1020 * TLSv1:
1021 * hash = PRF( master, finished_label,
1022 * MD5( handshake ) + SHA1( handshake ) )[0..11]
1023 */
1024
1025#if !defined(MBEDTLS_MD5_ALT)
1026 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1027 md5.state, sizeof( md5.state ) );
1028#endif
1029
1030#if !defined(MBEDTLS_SHA1_ALT)
1031 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1032 sha1.state, sizeof( sha1.state ) );
1033#endif
1034
1035 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1036 ? "client finished"
1037 : "server finished";
1038
1039 mbedtls_md5_finish_ret( &md5, padbuf );
1040 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
1041
1042 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1043 mbedtls_ssl_suite_get_mac(
1044 mbedtls_ssl_ciphersuite_from_id(
1045 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1046 session->master, 48, sender,
1047 padbuf, 36, buf, len );
1048
1049 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1050
1051 mbedtls_md5_free( &md5 );
1052 mbedtls_sha1_free( &sha1 );
1053
1054 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1055
1056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1057}
1058#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1059
1060#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1061#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001062MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha256(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001063 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1064{
1065 int len = 12;
1066 const char *sender;
1067 mbedtls_sha256_context sha256;
1068 unsigned char padbuf[32];
1069
1070 mbedtls_ssl_session *session = ssl->session_negotiate;
1071 if( !session )
1072 session = ssl->session;
1073
1074 mbedtls_sha256_init( &sha256 );
1075
1076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
1077
1078 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1079
1080 /*
1081 * TLSv1.2:
1082 * hash = PRF( master, finished_label,
1083 * Hash( handshake ) )[0.11]
1084 */
1085
1086#if !defined(MBEDTLS_SHA256_ALT)
1087 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
1088 sha256.state, sizeof( sha256.state ) );
1089#endif
1090
1091 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1092 ? "client finished"
1093 : "server finished";
1094
1095 mbedtls_sha256_finish_ret( &sha256, padbuf );
1096
1097 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1098 mbedtls_ssl_suite_get_mac(
1099 mbedtls_ssl_ciphersuite_from_id(
1100 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1101 session->master, 48, sender,
1102 padbuf, 32, buf, len );
1103
1104 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1105
1106 mbedtls_sha256_free( &sha256 );
1107
1108 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1109
1110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1111}
1112#endif /* MBEDTLS_SHA256_C */
1113
1114#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001115MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha384(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001116 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1117{
1118 int len = 12;
1119 const char *sender;
1120 mbedtls_sha512_context sha512;
1121 unsigned char padbuf[48];
1122
1123 mbedtls_ssl_session *session = ssl->session_negotiate;
1124 if( !session )
1125 session = ssl->session;
1126
1127 mbedtls_sha512_init( &sha512 );
1128
1129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
1130
1131 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1132
1133 /*
1134 * TLSv1.2:
1135 * hash = PRF( master, finished_label,
1136 * Hash( handshake ) )[0.11]
1137 */
1138
1139#if !defined(MBEDTLS_SHA512_ALT)
1140 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
1141 sha512.state, sizeof( sha512.state ) );
1142#endif
1143
1144 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1145 ? "client finished"
1146 : "server finished";
1147
1148 mbedtls_sha512_finish_ret( &sha512, padbuf );
1149
1150 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1151 mbedtls_ssl_suite_get_mac(
1152 mbedtls_ssl_ciphersuite_from_id(
1153 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1154 session->master, 48, sender,
1155 padbuf, 48, buf, len );
1156
1157 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1158
1159 mbedtls_sha512_free( &sha512 );
1160
1161 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1162
1163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1164}
1165#endif /* MBEDTLS_SHA512_C */
1166#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1167
Hanno Becker2793f742019-08-16 14:28:43 +01001168MBEDTLS_ALWAYS_INLINE static inline int ssl_calc_finished(
1169 int minor_ver,
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001170 mbedtls_md_type_t hash,
1171 mbedtls_ssl_context *ssl,
1172 unsigned char *buf,
1173 int from )
1174{
1175#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1176 (void) hash;
1177#endif
1178
1179#if defined(MBEDTLS_SSL_PROTO_SSL3)
1180 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1181 ssl_calc_finished_ssl( ssl, buf, from );
1182 else
1183#endif
1184#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001185 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001186 ssl_calc_finished_tls( ssl, buf, from );
1187 else
1188#endif
1189#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1190#if defined(MBEDTLS_SHA512_C)
1191 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1192 hash == MBEDTLS_MD_SHA384 )
1193 {
1194 ssl_calc_finished_tls_sha384( ssl, buf, from );
1195 }
1196 else
1197#endif
1198#if defined(MBEDTLS_SHA256_C)
1199 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1200 ssl_calc_finished_tls_sha256( ssl, buf, from );
1201 else
1202#endif
1203#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1204 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1205
1206 return( 0 );
1207}
1208
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001209/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001210 * Populate a transform structure with session keys and all the other
1211 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001212 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001213 * Parameters:
1214 * - [in/out]: transform: structure to populate
1215 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001216 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001217 * - [in] ciphersuite
1218 * - [in] master
1219 * - [in] encrypt_then_mac
1220 * - [in] trunc_hmac
1221 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001222 * - [in] tls_prf: pointer to PRF to use for key derivation
1223 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001224 * - [in] minor_ver: SSL/TLS minor version
1225 * - [in] endpoint: client or server
1226 * - [in] ssl: optionally used for:
1227 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1228 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1229 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001230 */
Hanno Becker298a4702019-08-16 10:21:32 +01001231/* Force compilers to inline this function if it's used only
1232 * from one place, because at least ARMC5 doesn't do that
1233 * automatically. */
1234#if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1235MBEDTLS_ALWAYS_INLINE static inline
1236#else
1237static
1238#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1239int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001240 int ciphersuite,
1241 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001242#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001243#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1244 int encrypt_then_mac,
1245#endif
1246#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1247 int trunc_hmac,
1248#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001249#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001250#if defined(MBEDTLS_ZLIB_SUPPORT)
1251 int compression,
1252#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001253 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001254 int minor_ver,
1255 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001256 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001257{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001258 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 unsigned char keyblk[256];
1260 unsigned char *key1;
1261 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001262 unsigned char *mac_enc;
1263 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001264 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001265 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001266 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001267 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 const mbedtls_cipher_info_t *cipher_info;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001269 mbedtls_md_handle_t md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001270
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001271#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1272 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1273 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001274 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001275 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001276#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001277
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001278 /*
1279 * Some data just needs copying into the structure
1280 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001281#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1282 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001283 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001284#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001285
1286#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001287 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001288#else
1289 ((void) minor_ver);
1290#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001292#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Teppo Järvelin91d79382019-10-02 09:09:31 +03001293 mbedtls_platform_memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001294#endif
1295
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001296 /*
1297 * Get various info structures
1298 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001299 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001300 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001301 {
1302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001303 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001304 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1305 }
1306
Hanno Becker473f98f2019-06-26 10:27:32 +01001307 cipher_info = mbedtls_cipher_info_from_type(
1308 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001309 if( cipher_info == NULL )
1310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001312 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001314 }
1315
Hanno Becker473f98f2019-06-26 10:27:32 +01001316 md_info = mbedtls_md_info_from_type(
1317 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001318 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Paul Bakker68884e32013-01-07 18:20:04 +01001319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001321 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001323 }
1324
Hanno Beckera5a2b082019-05-15 14:03:01 +01001325#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001326 /* Copy own and peer's CID if the use of the CID
1327 * extension has been negotiated. */
1328 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1329 {
1330 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001331
Hanno Becker4932f9f2019-05-03 15:23:51 +01001332 transform->in_cid_len = ssl->own_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001333 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1334 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001335 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001336 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001337
1338 transform->out_cid_len = ssl->handshake->peer_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001339 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1340 memcpy( transform->out_cid, ssl->handshake->peer_cid,
Hanno Beckere582d122019-05-15 10:21:55 +01001341 ssl->handshake->peer_cid_len );
1342 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1343 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001344 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001345#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001346
Paul Bakker5121ce52009-01-03 21:22:43 +00001347 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001348 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001349 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001350 ret = ssl_prf( minor_ver,
1351 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1352 master, 48, "key expansion", randbytes, 64,
1353 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001354 if( ret != 0 )
1355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001357 return( ret );
1358 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001361 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001362 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001363 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001365
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 /*
1367 * Determine the appropriate key, IV and MAC length.
1368 */
Paul Bakker68884e32013-01-07 18:20:04 +01001369
Hanno Beckere7f2df02017-12-27 08:17:40 +00001370 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001371
Hanno Beckerf1229442018-01-03 15:32:31 +00001372#if defined(MBEDTLS_GCM_C) || \
1373 defined(MBEDTLS_CCM_C) || \
1374 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001376 cipher_info->mode == MBEDTLS_MODE_CCM ||
1377 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001379 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001380 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001381 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1382 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001383
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001384 /* All modes haves 96-bit IVs;
1385 * GCM and CCM has 4 implicit and 8 explicit bytes
1386 * ChachaPoly has all 12 bytes implicit
1387 */
Paul Bakker68884e32013-01-07 18:20:04 +01001388 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001389 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1390 transform->fixed_ivlen = 12;
1391 else
1392 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001393 }
1394 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001395#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1396#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1397 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1398 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001399 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001400 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1402 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001405 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001406 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001407
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001408 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001409 mac_key_len = mbedtls_md_get_size( md_info );
1410 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001413 /*
1414 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1415 * (rfc 6066 page 13 or rfc 2104 section 4),
1416 * so we only need to adjust the length here.
1417 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001418 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001421
1422#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1423 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001424 * HMAC implementation which also truncates the key
1425 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001426 mac_key_len = transform->maclen;
1427#endif
1428 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001430
1431 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001432 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001434 else
1435#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1436 {
1437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1438 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1439 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001440
Hanno Beckera9d5c452019-07-25 16:47:12 +01001441 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001442 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001443 (unsigned) transform->ivlen,
1444 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001445
1446 /*
1447 * Finally setup the cipher contexts, IVs and MAC secrets.
1448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001450 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001451 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001452 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001453 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001454
Paul Bakker68884e32013-01-07 18:20:04 +01001455 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001456 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001457
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001458 /*
1459 * This is not used in TLS v1.1.
1460 */
Paul Bakker48916f92012-09-16 19:57:18 +00001461 iv_copy_len = ( transform->fixed_ivlen ) ?
1462 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001463 mbedtls_platform_memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1464 mbedtls_platform_memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001465 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001466 }
1467 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468#endif /* MBEDTLS_SSL_CLI_C */
1469#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001470 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001471 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001472 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001473 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001474
Hanno Becker81c7b182017-11-09 18:39:33 +00001475 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001476 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001477
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001478 /*
1479 * This is not used in TLS v1.1.
1480 */
Paul Bakker48916f92012-09-16 19:57:18 +00001481 iv_copy_len = ( transform->fixed_ivlen ) ?
1482 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001483 mbedtls_platform_memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1484 mbedtls_platform_memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001485 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001486 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001487 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1491 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001492 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001493
Hanno Becker92231322018-01-03 15:32:51 +00001494#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001496 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001497 {
Hanno Becker92231322018-01-03 15:32:51 +00001498 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1501 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001502 }
1503
Teppo Järvelin91d79382019-10-02 09:09:31 +03001504 mbedtls_platform_memcpy( transform->mac_enc, mac_enc, mac_key_len );
1505 mbedtls_platform_memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001506 }
1507 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1509#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1510 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001511 if( mbedtls_ssl_ver_geq( minor_ver, MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakker68884e32013-01-07 18:20:04 +01001512 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001513 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1514 For AEAD-based ciphersuites, there is nothing to do here. */
1515 if( mac_key_len != 0 )
1516 {
1517 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1518 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1519 }
Paul Bakker68884e32013-01-07 18:20:04 +01001520 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001521 else
1522#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1525 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001526 }
Hanno Becker92231322018-01-03 15:32:51 +00001527#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1530 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001533
Hanno Beckere7f2df02017-12-27 08:17:40 +00001534 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001535 transform->iv_enc, transform->iv_dec,
1536 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001537 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001538 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1541 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001542 }
1543 }
Hanno Becker92231322018-01-03 15:32:51 +00001544#else
1545 ((void) mac_dec);
1546 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001548
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001549#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1550 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001551 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001552 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001553 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001554 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001555 iv_copy_len );
1556 }
1557#endif
1558
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001559 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001560 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001561 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001562 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001563 return( ret );
1564 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001565
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001566 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001567 cipher_info ) ) != 0 )
1568 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001569 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001570 return( ret );
1571 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001574 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001578 return( ret );
1579 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001582 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001586 return( ret );
1587 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589#if defined(MBEDTLS_CIPHER_MODE_CBC)
1590 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1593 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001596 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001597 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1600 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001603 return( ret );
1604 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001605 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001607
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001608 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001609
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001610 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001612 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001615
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001616 mbedtls_platform_memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1617 mbedtls_platform_memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001618
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001619 if( deflateInit( &transform->ctx_deflate,
1620 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001621 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1624 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001625 }
1626 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001628
Paul Bakker5121ce52009-01-03 21:22:43 +00001629 return( 0 );
1630}
1631
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001632#if defined(MBEDTLS_SSL_PROTO_SSL3)
1633static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1634 unsigned char hash[36],
1635 size_t *hlen )
1636{
1637 mbedtls_md5_context md5;
1638 mbedtls_sha1_context sha1;
1639 unsigned char pad_1[48];
1640 unsigned char pad_2[48];
1641
1642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1643
1644 mbedtls_md5_init( &md5 );
1645 mbedtls_sha1_init( &sha1 );
1646
1647 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1648 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1649
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001650 mbedtls_platform_memset( pad_1, 0x36, 48 );
1651 mbedtls_platform_memset( pad_2, 0x5C, 48 );
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001652
1653 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1654 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1655 mbedtls_md5_finish_ret( &md5, hash );
1656
1657 mbedtls_md5_starts_ret( &md5 );
1658 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1659 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1660 mbedtls_md5_update_ret( &md5, hash, 16 );
1661 mbedtls_md5_finish_ret( &md5, hash );
1662
1663 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1664 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1665 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1666
1667 mbedtls_sha1_starts_ret( &sha1 );
1668 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1669 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1670 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1671 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1672
1673 *hlen = 36;
1674
1675 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1677
1678 mbedtls_md5_free( &md5 );
1679 mbedtls_sha1_free( &sha1 );
1680
1681 return;
1682}
1683#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1684
1685#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1686static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1687 unsigned char hash[36],
1688 size_t *hlen )
1689{
1690 mbedtls_md5_context md5;
1691 mbedtls_sha1_context sha1;
1692
1693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1694
1695 mbedtls_md5_init( &md5 );
1696 mbedtls_sha1_init( &sha1 );
1697
1698 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1699 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1700
1701 mbedtls_md5_finish_ret( &md5, hash );
1702 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1703
1704 *hlen = 36;
1705
1706 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1708
1709 mbedtls_md5_free( &md5 );
1710 mbedtls_sha1_free( &sha1 );
1711
1712 return;
1713}
1714#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1715
1716#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1717#if defined(MBEDTLS_SHA256_C)
1718static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1719 unsigned char hash[32],
1720 size_t *hlen )
1721{
1722 mbedtls_sha256_context sha256;
1723
1724 mbedtls_sha256_init( &sha256 );
1725
1726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1727
1728 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1729 mbedtls_sha256_finish_ret( &sha256, hash );
1730
1731 *hlen = 32;
1732
1733 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1735
1736 mbedtls_sha256_free( &sha256 );
1737
1738 return;
1739}
1740#endif /* MBEDTLS_SHA256_C */
1741
1742#if defined(MBEDTLS_SHA512_C)
1743static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1744 unsigned char hash[48],
1745 size_t *hlen )
1746{
1747 mbedtls_sha512_context sha512;
1748
1749 mbedtls_sha512_init( &sha512 );
1750
1751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1752
1753 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1754 mbedtls_sha512_finish_ret( &sha512, hash );
1755
1756 *hlen = 48;
1757
1758 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1760
1761 mbedtls_sha512_free( &sha512 );
1762
1763 return;
1764}
1765#endif /* MBEDTLS_SHA512_C */
1766#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1767
Hanno Becker2f41b242019-08-15 17:29:43 +01001768int mbedtls_ssl_calc_verify( int minor_ver,
1769 mbedtls_md_type_t hash,
1770 mbedtls_ssl_context const *ssl,
1771 unsigned char *dst,
1772 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001773{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001774#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1775 (void) hash;
1776#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001777
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001778#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001779 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001780 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001781 else
1782#endif
1783#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001784 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker2f41b242019-08-15 17:29:43 +01001785 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001786 else
1787#endif
1788#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1789#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001790 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1791 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001792 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001793 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001794 }
1795 else
1796#endif
1797#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001798 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001799 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001800 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001801 }
1802 else
1803#endif
1804#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1805 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001806 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1807 }
1808
1809 return( 0 );
1810}
1811
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001812/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001813 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001814 *
1815 * Parameters:
1816 * [in/out] handshake
1817 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1818 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001819 * [out] master
1820 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001821 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001822static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001823 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001824 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001825{
1826 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001827
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001828/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1829/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1830/* (void) ssl; */
1831/* #endif */
1832
1833 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1834 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001835
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001836#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02001837 if( handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001838 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001839 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1840 return( 0 );
1841 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001842#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001843
1844 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1845 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001846
1847#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001848 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1849 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001850 {
1851 unsigned char session_hash[48];
1852 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001853
Hanno Becker2f41b242019-08-15 17:29:43 +01001854 mbedtls_ssl_calc_verify(
1855 mbedtls_ssl_get_minor_ver( ssl ),
1856 mbedtls_ssl_suite_get_mac( ciphersuite ),
1857 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001858
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001859 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1860 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001861
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001862 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1863 mbedtls_ssl_suite_get_mac( ciphersuite ),
1864 handshake->premaster, handshake->pmslen,
1865 "extended master secret",
1866 session_hash, hash_len,
1867 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001868 }
1869 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001870#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001871 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001872 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1873 mbedtls_ssl_suite_get_mac( ciphersuite ),
1874 handshake->premaster, handshake->pmslen,
1875 "master secret",
1876 handshake->randbytes, 64,
1877 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001878 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001879 if( ret != 0 )
1880 {
1881 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1882 return( ret );
1883 }
1884
1885 mbedtls_platform_zeroize( handshake->premaster,
1886 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001887
1888 return( 0 );
1889}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001890
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001891int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1892{
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02001893 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001894
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001896 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_UNSET;
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001897 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001898 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001899 ssl->session_negotiate->master,
1900 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001901 if( ret != 0 )
1902 {
1903 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1904 return( ret );
1905 }
1906
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001907 /* Swap the client and server random values:
1908 * - MS derivation wanted client+server (RFC 5246 8.1)
1909 * - key derivation wants server+client (RFC 5246 6.3) */
1910 {
1911 unsigned char tmp[64];
Teppo Järvelin91d79382019-10-02 09:09:31 +03001912 mbedtls_platform_memcpy( tmp, ssl->handshake->randbytes, 64 );
1913 mbedtls_platform_memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1914 mbedtls_platform_memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001915 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1916 }
1917
1918 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001919 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001920 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1921 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001922#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001923#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001924 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001925#endif
1926#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001927 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001928#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001929#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001930#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001931 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001932#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001933 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001934 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001935 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1936 ssl );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001937 if( ret == 0 )
1938 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02001939 mbedtls_platform_random_delay();
Jarno Lamsa4031a452019-12-19 08:11:12 +02001940 if( ret == 0 )
1941 {
1942 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_SET;
1943 }
1944 else
1945 {
1946 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
1947 }
1948 }
1949 else
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001950 {
1951 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1952 return( ret );
1953 }
1954
1955 /* We no longer need Server/ClientHello.random values */
1956 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1957 sizeof( ssl->handshake->randbytes ) );
1958
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001959 /* Allocate compression buffer */
1960#if defined(MBEDTLS_ZLIB_SUPPORT)
jiblime92af9a92019-12-18 21:40:01 -08001961 if( ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001962 ssl->compress_buf == NULL )
1963 {
1964 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1965 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1966 if( ssl->compress_buf == NULL )
1967 {
1968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001969 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001970 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1971 }
1972 }
1973#endif
1974
Paul Bakker5121ce52009-01-03 21:22:43 +00001975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1976
1977 return( 0 );
1978}
1979
Hanno Becker09d23642019-07-22 17:18:18 +01001980int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1981{
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02001982 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker09d23642019-07-22 17:18:18 +01001983
1984 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1985 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Beckera3c2c172019-07-23 16:51:57 +01001986#if defined(MBEDTLS_USE_TINYCRYPT)
1987 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001988 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Hanno Beckera3c2c172019-07-23 16:51:57 +01001989 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001990 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1991 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1992 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1993 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1994 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001995 {
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01001996 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1997 ssl->handshake->ecdh_privkey,
1998 ssl->handshake->premaster );
1999 if( ret == UECC_FAULT_DETECTED )
2000 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2001 if( ret != UECC_SUCCESS )
Hanno Beckera3c2c172019-07-23 16:51:57 +01002002 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002003 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Hanno Beckera3c2c172019-07-23 16:51:57 +01002004
2005 ssl->handshake->pmslen = NUM_ECC_BYTES;
2006 }
2007 else
2008#endif
Hanno Becker09d23642019-07-22 17:18:18 +01002009#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2010 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2011 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
2012 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002013 ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002014 ssl->handshake->premaster,
2015 MBEDTLS_PREMASTER_SIZE,
2016 &ssl->handshake->pmslen,
2017 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002018 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2019 if( ret == 0 )
2020 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002021 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002022 if( ret == 0 )
2023 {
2024 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2025 }
2026 else
2027 {
2028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2029 return( ret );
2030 }
2031 }
2032 else
Hanno Becker09d23642019-07-22 17:18:18 +01002033 {
2034 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2035 return( ret );
2036 }
2037
2038 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2039 }
2040 else
2041#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01002042#if defined(MBEDTLS_ECDH_C) && \
2043 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2044 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2045 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2046 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002047 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2048 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2049 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2050 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2051 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2052 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2053 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2054 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2055 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002056 ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002057 &ssl->handshake->pmslen,
2058 ssl->handshake->premaster,
2059 MBEDTLS_MPI_MAX_SIZE,
2060 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002061 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2062 if( ret == 0 )
2063 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002064 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002065 if( ret == 0 )
2066 {
2067 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2068 }
2069 else
2070 {
2071 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002072 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002073 }
2074 }
2075 else
Hanno Becker09d23642019-07-22 17:18:18 +01002076 {
2077 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2078 return( ret );
2079 }
2080
2081 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2082 }
2083 else
2084#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2085 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2086 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2087 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2088#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2089 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2090 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002091 ret = mbedtls_ssl_psk_derive_premaster( ssl,
2092 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) );
2093 if( ret == 0 )
2094 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002095 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002096 if( ret == 0 )
2097 {
2098 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2099 }
2100 else
2101 {
2102 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002103 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002104 }
2105 }
2106 else
Hanno Becker09d23642019-07-22 17:18:18 +01002107 {
2108 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2109 return( ret );
2110 }
2111 }
2112 else
2113#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2114#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2115 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2116 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2117 {
2118 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2119 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2120 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002121 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002122 if( ret == 0 )
2123 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002124 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002125 if( ret == 0 )
2126 {
2127 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2128 }
2129 else
2130 {
2131 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002132 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002133 }
2134 }
2135 else
Hanno Becker09d23642019-07-22 17:18:18 +01002136 {
2137 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2138 return( ret );
2139 }
2140 }
2141 else
2142#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2143#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2144 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2145 == MBEDTLS_KEY_EXCHANGE_RSA )
2146 {
2147 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002148 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002149 * ssl_rsa_generate_partial_pms(). Only the
2150 * PMS length needs to be set. */
2151 ssl->handshake->pmslen = 48;
2152 }
2153 else
2154#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2155 {
2156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2157 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2158 }
2159
2160 return( 0 );
2161}
2162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2164int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002165{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002166 unsigned char *p = ssl->handshake->premaster;
2167 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002168 const unsigned char *psk = ssl->conf->psk;
2169 size_t psk_len = ssl->conf->psk_len;
2170
2171 /* If the psk callback was called, use its result */
2172 if( ssl->handshake->psk != NULL )
2173 {
2174 psk = ssl->handshake->psk;
2175 psk_len = ssl->handshake->psk_len;
2176 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002177
2178 /*
2179 * PMS = struct {
2180 * opaque other_secret<0..2^16-1>;
2181 * opaque psk<0..2^16-1>;
2182 * };
2183 * with "other_secret" depending on the particular key exchange
2184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2186 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002187 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002188 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002190
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002191 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002192
2193 if( end < p || (size_t)( end - p ) < psk_len )
2194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2195
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002196 mbedtls_platform_memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002197 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002198 }
2199 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2201#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2202 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002203 {
2204 /*
2205 * other_secret already set by the ClientKeyExchange message,
2206 * and is 48 bytes long
2207 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002208 if( end - p < 2 )
2209 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2210
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002211 *p++ = 0;
2212 *p++ = 48;
2213 p += 48;
2214 }
2215 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2217#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2218 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002219 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002220 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002221 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002222
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002223 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002225 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002226 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002227 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002230 return( ret );
2231 }
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002232 p = mbedtls_platform_put_uint16_be( p, len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002233 p += len;
2234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002236 }
2237 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2239#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2240 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002241 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002242 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002243 size_t zlen;
2244
Hanno Becker982da7e2019-09-02 09:47:39 +01002245#if defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01002246 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2247 ssl->handshake->ecdh_privkey,
2248 p + 2 );
2249 if( ret == UECC_FAULT_DETECTED )
2250 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2251 if( ret != UECC_SUCCESS )
Hanno Becker982da7e2019-09-02 09:47:39 +01002252 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Hanno Becker982da7e2019-09-02 09:47:39 +01002253
2254 zlen = NUM_ECC_BYTES;
2255#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002257 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002258 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002259 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002262 return( ret );
2263 }
2264
Hanno Becker982da7e2019-09-02 09:47:39 +01002265 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2266 MBEDTLS_DEBUG_ECDH_Z );
2267#endif /* MBEDTLS_USE_TINYCRYPT */
2268
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002269 p = mbedtls_platform_put_uint16_be( p, zlen );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002270 p += zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002271 }
2272 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2276 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002277 }
2278
2279 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002280 if( end - p < 2 )
2281 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002282
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002283 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002284
2285 if( end < p || (size_t)( end - p ) < psk_len )
2286 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2287
Teppo Järvelin91d79382019-10-02 09:09:31 +03002288 mbedtls_platform_memcpy( p, psk, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002289 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002290
2291 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2292
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002293 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2294
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002295 return( 0 );
2296}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002300/*
2301 * SSLv3.0 MAC functions
2302 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002303#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002304static void ssl_mac( mbedtls_md_context_t *md_ctx,
2305 const unsigned char *secret,
2306 const unsigned char *buf, size_t len,
2307 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002308 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002309{
2310 unsigned char header[11];
2311 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002312 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2314 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002315
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002316 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002318 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002319 else
Paul Bakker68884e32013-01-07 18:20:04 +01002320 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002321
Teppo Järvelin91d79382019-10-02 09:09:31 +03002322 mbedtls_platform_memcpy( header, ctr, 8 );
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03002323 header[8] = (unsigned char) type;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002324 (void)mbedtls_platform_put_uint16_be( &header[9], len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002325
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002326 mbedtls_platform_memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 mbedtls_md_starts( md_ctx );
2328 mbedtls_md_update( md_ctx, secret, md_size );
2329 mbedtls_md_update( md_ctx, padding, padlen );
2330 mbedtls_md_update( md_ctx, header, 11 );
2331 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002332 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002333
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002334 mbedtls_platform_memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 mbedtls_md_starts( md_ctx );
2336 mbedtls_md_update( md_ctx, secret, md_size );
2337 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002338 mbedtls_md_update( md_ctx, out, md_size );
2339 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002340}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002342
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002343/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002344 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002345#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002346 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2347 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2348 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2349/* This function makes sure every byte in the memory region is accessed
2350 * (in ascending addresses order) */
2351static void ssl_read_memory( unsigned char *p, size_t len )
2352{
2353 unsigned char acc = 0;
2354 volatile unsigned char force;
2355
2356 for( ; len != 0; p++, len-- )
2357 acc ^= *p;
2358
2359 force = acc;
2360 (void) force;
2361}
2362#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2363
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002364/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002365 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002366 */
Hanno Becker3307b532017-12-27 21:37:21 +00002367
Hanno Beckera5a2b082019-05-15 14:03:01 +01002368#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002369/* This functions transforms a DTLS plaintext fragment and a record content
2370 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002371 *
2372 * struct {
2373 * opaque content[DTLSPlaintext.length];
2374 * ContentType real_type;
2375 * uint8 zeros[length_of_padding];
2376 * } DTLSInnerPlaintext;
2377 *
2378 * Input:
2379 * - `content`: The beginning of the buffer holding the
2380 * plaintext to be wrapped.
2381 * - `*content_size`: The length of the plaintext in Bytes.
2382 * - `max_len`: The number of Bytes available starting from
2383 * `content`. This must be `>= *content_size`.
2384 * - `rec_type`: The desired record content type.
2385 *
2386 * Output:
2387 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2388 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2389 *
2390 * Returns:
2391 * - `0` on success.
2392 * - A negative error code if `max_len` didn't offer enough space
2393 * for the expansion.
2394 */
2395static int ssl_cid_build_inner_plaintext( unsigned char *content,
2396 size_t *content_size,
2397 size_t remaining,
2398 uint8_t rec_type )
2399{
2400 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002401 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2402 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2403 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002404
2405 /* Write real content type */
2406 if( remaining == 0 )
2407 return( -1 );
2408 content[ len ] = rec_type;
2409 len++;
2410 remaining--;
2411
2412 if( remaining < pad )
2413 return( -1 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002414 mbedtls_platform_memset( content + len, 0, pad );
Hanno Becker92c930f2019-04-29 17:31:37 +01002415 len += pad;
2416 remaining -= pad;
2417
2418 *content_size = len;
2419 return( 0 );
2420}
2421
Hanno Becker7dc25772019-05-20 15:08:01 +01002422/* This function parses a DTLSInnerPlaintext structure.
2423 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002424static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2425 size_t *content_size,
2426 uint8_t *rec_type )
2427{
2428 size_t remaining = *content_size;
2429
2430 /* Determine length of padding by skipping zeroes from the back. */
2431 do
2432 {
2433 if( remaining == 0 )
2434 return( -1 );
2435 remaining--;
2436 } while( content[ remaining ] == 0 );
2437
2438 *content_size = remaining;
2439 *rec_type = content[ remaining ];
2440
2441 return( 0 );
2442}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002443#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002444
Hanno Becker99abf512019-05-20 14:50:53 +01002445/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002446 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002447static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002448 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002449 mbedtls_record *rec )
2450{
Hanno Becker99abf512019-05-20 14:50:53 +01002451 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002452 *
2453 * additional_data = seq_num + TLSCompressed.type +
2454 * TLSCompressed.version + TLSCompressed.length;
2455 *
Hanno Becker99abf512019-05-20 14:50:53 +01002456 * For the CID extension, this is extended as follows
2457 * (quoting draft-ietf-tls-dtls-connection-id-05,
2458 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002459 *
2460 * additional_data = seq_num + DTLSPlaintext.type +
2461 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002462 * cid +
2463 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002464 * length_of_DTLSInnerPlaintext;
2465 */
2466
Teppo Järvelin91d79382019-10-02 09:09:31 +03002467 mbedtls_platform_memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002468 add_data[8] = rec->type;
Teppo Järvelin91d79382019-10-02 09:09:31 +03002469 mbedtls_platform_memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002470
Hanno Beckera5a2b082019-05-15 14:03:01 +01002471#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002472 if( rec->cid_len != 0 )
2473 {
Teppo Järvelin91d79382019-10-02 09:09:31 +03002474 mbedtls_platform_memcpy( add_data + 11, rec->cid, rec->cid_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002475 add_data[11 + rec->cid_len + 0] = rec->cid_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002476 (void)mbedtls_platform_put_uint16_be( &add_data[11 + rec->cid_len + 1],
2477 rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002478 *add_data_len = 13 + 1 + rec->cid_len;
2479 }
2480 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002481#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002482 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002483 (void)mbedtls_platform_put_uint16_be( &add_data[11], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002484 *add_data_len = 13;
2485 }
Hanno Becker3307b532017-12-27 21:37:21 +00002486}
2487
Hanno Becker611a83b2018-01-03 14:27:32 +00002488int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2489 mbedtls_ssl_transform *transform,
2490 mbedtls_record *rec,
2491 int (*f_rng)(void *, unsigned char *, size_t),
2492 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002493{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002495 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002496 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002497 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002498 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002499 size_t post_avail;
2500
2501 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002502#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002503 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002504 ((void) ssl);
2505#endif
2506
2507 /* The PRNG is used for dynamic IV generation that's used
2508 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2509#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2510 ( defined(MBEDTLS_AES_C) || \
2511 defined(MBEDTLS_ARIA_C) || \
2512 defined(MBEDTLS_CAMELLIA_C) ) && \
2513 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2514 ((void) f_rng);
2515 ((void) p_rng);
2516#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Hanno Becker3307b532017-12-27 21:37:21 +00002520 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002521 {
Hanno Becker3307b532017-12-27 21:37:21 +00002522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2523 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2524 }
Hanno Becker505089d2019-05-01 09:45:57 +01002525 if( rec == NULL
2526 || rec->buf == NULL
2527 || rec->buf_len < rec->data_offset
2528 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002529#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002530 || rec->cid_len != 0
2531#endif
2532 )
Hanno Becker3307b532017-12-27 21:37:21 +00002533 {
2534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002536 }
2537
Hanno Becker3307b532017-12-27 21:37:21 +00002538 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002539 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002541 data, rec->data_len );
2542
2543 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2544
2545 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2546 {
2547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2548 (unsigned) rec->data_len,
2549 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2551 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002552
Hanno Beckera5a2b082019-05-15 14:03:01 +01002553#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002554 /*
2555 * Add CID information
2556 */
2557 rec->cid_len = transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03002558 /* Not using more secure mbedtls_platform_memcpy as cid is public */
2559 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
Hanno Beckere83efe62019-04-29 13:52:53 +01002560 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002561
2562 if( rec->cid_len != 0 )
2563 {
2564 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002565 * Wrap plaintext into DTLSInnerPlaintext structure.
2566 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002567 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002568 * Note that this changes `rec->data_len`, and hence
2569 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002570 */
2571 if( ssl_cid_build_inner_plaintext( data,
2572 &rec->data_len,
2573 post_avail,
2574 rec->type ) != 0 )
2575 {
2576 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2577 }
2578
2579 rec->type = MBEDTLS_SSL_MSG_CID;
2580 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002581#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002582
Hanno Becker92c930f2019-04-29 17:31:37 +01002583 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2584
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002586 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002588#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589 if( mode == MBEDTLS_MODE_STREAM ||
2590 ( mode == MBEDTLS_MODE_CBC
2591#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002592 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002593#endif
2594 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 {
Hanno Becker3307b532017-12-27 21:37:21 +00002596 if( post_avail < transform->maclen )
2597 {
2598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2599 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2600 }
2601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002603 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2604 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002605 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002606 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002607 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2608 data, rec->data_len, rec->ctr, rec->type, mac );
Teppo Järvelin91d79382019-10-02 09:09:31 +03002609 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002610 }
2611 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002612#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2614 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002615 if( mbedtls_ssl_ver_geq(
2616 mbedtls_ssl_transform_get_minor_ver( transform ),
2617 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002618 {
Hanno Becker992b6872017-11-09 18:57:39 +00002619 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2620
Hanno Beckere83efe62019-04-29 13:52:53 +01002621 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002622
Hanno Becker3307b532017-12-27 21:37:21 +00002623 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002624 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002625 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2626 data, rec->data_len );
2627 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2628 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2629
Teppo Järvelin91d79382019-10-02 09:09:31 +03002630 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002631 }
2632 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002633#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2636 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002637 }
2638
Hanno Becker3307b532017-12-27 21:37:21 +00002639 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2640 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002641
Hanno Becker3307b532017-12-27 21:37:21 +00002642 rec->data_len += transform->maclen;
2643 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002644 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002645 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002646#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002647
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002648 /*
2649 * Encrypt
2650 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2652 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002654 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002655 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002657 "including %d bytes of padding",
2658 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
Hanno Becker3307b532017-12-27 21:37:21 +00002660 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2661 transform->iv_enc, transform->ivlen,
2662 data, rec->data_len,
2663 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002666 return( ret );
2667 }
2668
Hanno Becker3307b532017-12-27 21:37:21 +00002669 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2672 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002673 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 }
Paul Bakker68884e32013-01-07 18:20:04 +01002675 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002677
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002678#if defined(MBEDTLS_GCM_C) || \
2679 defined(MBEDTLS_CCM_C) || \
2680 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002682 mode == MBEDTLS_MODE_CCM ||
2683 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002684 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002685 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002686 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002687 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002688
Hanno Becker3307b532017-12-27 21:37:21 +00002689 /* Check that there's space for both the authentication tag
2690 * and the explicit IV before and after the record content. */
2691 if( post_avail < transform->taglen ||
2692 rec->data_offset < explicit_iv_len )
2693 {
2694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2695 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2696 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002697
Paul Bakker68884e32013-01-07 18:20:04 +01002698 /*
2699 * Generate IV
2700 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002701 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2702 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002703 /* GCM and CCM: fixed || explicit (=seqnum) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002704 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2705 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, rec->ctr,
Hanno Becker3307b532017-12-27 21:37:21 +00002706 explicit_iv_len );
2707 /* Prefix record content with explicit IV. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002708 mbedtls_platform_memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002709 }
2710 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2711 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002712 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002713 unsigned char i;
2714
Teppo Järvelin91d79382019-10-02 09:09:31 +03002715 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002716
2717 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002718 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002719 }
2720 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002721 {
2722 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2724 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002725 }
2726
Hanno Beckere83efe62019-04-29 13:52:53 +01002727 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002728
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002729 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2730 iv, transform->ivlen );
2731 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002732 data - explicit_iv_len, explicit_iv_len );
2733 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002734 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002736 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002737 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002738
Paul Bakker68884e32013-01-07 18:20:04 +01002739 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002740 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002741 */
Hanno Becker3307b532017-12-27 21:37:21 +00002742
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002743 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002744 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002745 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002746 data, rec->data_len, /* source */
2747 data, &rec->data_len, /* destination */
2748 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002751 return( ret );
2752 }
2753
Hanno Becker3307b532017-12-27 21:37:21 +00002754 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2755 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002756
Hanno Becker3307b532017-12-27 21:37:21 +00002757 rec->data_len += transform->taglen + explicit_iv_len;
2758 rec->data_offset -= explicit_iv_len;
2759 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002760 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002761 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002762 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2764#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002765 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002767 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002768 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002769 size_t padlen, i;
2770 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002771
Hanno Becker3307b532017-12-27 21:37:21 +00002772 /* Currently we're always using minimal padding
2773 * (up to 255 bytes would be allowed). */
2774 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2775 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002776 padlen = 0;
2777
Hanno Becker3307b532017-12-27 21:37:21 +00002778 /* Check there's enough space in the buffer for the padding. */
2779 if( post_avail < padlen + 1 )
2780 {
2781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2782 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2783 }
2784
Paul Bakker5121ce52009-01-03 21:22:43 +00002785 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002786 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002787
Hanno Becker3307b532017-12-27 21:37:21 +00002788 rec->data_len += padlen + 1;
2789 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002792 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002793 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2794 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002795 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002796 if( mbedtls_ssl_ver_geq(
2797 mbedtls_ssl_transform_get_minor_ver( transform ),
2798 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002799 {
Hanno Becker3307b532017-12-27 21:37:21 +00002800 if( f_rng == NULL )
2801 {
2802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2803 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2804 }
2805
2806 if( rec->data_offset < transform->ivlen )
2807 {
2808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2809 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2810 }
2811
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002812 /*
2813 * Generate IV
2814 */
Hanno Becker3307b532017-12-27 21:37:21 +00002815 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002816 if( ret != 0 )
2817 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002818
Teppo Järvelin91d79382019-10-02 09:09:31 +03002819 mbedtls_platform_memcpy( data - transform->ivlen, transform->iv_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002820 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002821
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002822 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002826 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002827 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002828 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002829
Hanno Becker3307b532017-12-27 21:37:21 +00002830 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2831 transform->iv_enc,
2832 transform->ivlen,
2833 data, rec->data_len,
2834 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002837 return( ret );
2838 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002839
Hanno Becker3307b532017-12-27 21:37:21 +00002840 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2843 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002844 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002847 if( mbedtls_ssl_ver_lt(
2848 mbedtls_ssl_transform_get_minor_ver( transform ),
2849 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02002850 {
2851 /*
2852 * Save IV in SSL3 and TLS1
2853 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002854 mbedtls_platform_memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
Hanno Becker3307b532017-12-27 21:37:21 +00002855 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002856 }
Hanno Becker3307b532017-12-27 21:37:21 +00002857 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002858#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002859 {
2860 data -= transform->ivlen;
2861 rec->data_offset -= transform->ivlen;
2862 rec->data_len += transform->ivlen;
2863 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002866 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002867 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002868 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2869
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002870 /*
2871 * MAC(MAC_write_key, seq_num +
2872 * TLSCipherText.type +
2873 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002874 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002875 * IV + // except for TLS 1.0
2876 * ENC(content + padding + padding_length));
2877 */
Hanno Becker3307b532017-12-27 21:37:21 +00002878
2879 if( post_avail < transform->maclen)
2880 {
2881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2882 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2883 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002884
Hanno Beckere83efe62019-04-29 13:52:53 +01002885 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002888 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002889 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002890
Hanno Becker3307b532017-12-27 21:37:21 +00002891 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002892 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002893 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2894 data, rec->data_len );
2895 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2896 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002897
Teppo Järvelin91d79382019-10-02 09:09:31 +03002898 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002899
Hanno Becker3307b532017-12-27 21:37:21 +00002900 rec->data_len += transform->maclen;
2901 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002902 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002903 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002905 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002906 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002908 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2911 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002912 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002913
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002914 /* Make extra sure authentication was performed, exactly once */
2915 if( auth_done != 1 )
2916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2918 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002919 }
2920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002922
2923 return( 0 );
2924}
2925
Hanno Becker40478be2019-07-12 08:23:59 +01002926int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002927 mbedtls_ssl_transform *transform,
2928 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002929{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002930 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002932 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002933#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002934 size_t padlen = 0, correct = 1;
2935#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002936 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002937 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002938 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002939
Hanno Becker611a83b2018-01-03 14:27:32 +00002940#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002941 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002942 ((void) ssl);
2943#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002946 if( rec == NULL ||
2947 rec->buf == NULL ||
2948 rec->buf_len < rec->data_offset ||
2949 rec->buf_len - rec->data_offset < rec->data_len )
2950 {
2951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002953 }
2954
Hanno Becker4c6876b2017-12-27 21:28:58 +00002955 data = rec->buf + rec->data_offset;
2956 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002957
Hanno Beckera5a2b082019-05-15 14:03:01 +01002958#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002959 /*
2960 * Match record's CID with incoming CID.
2961 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002962 if( rec->cid_len != transform->in_cid_len ||
Teppo Järvelin0efac532019-10-04 13:21:08 +03002963 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) // use regular memcmp as CID is public
Hanno Beckerabd7c892019-05-08 13:02:22 +01002964 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002965 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002966 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002967#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2970 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002971 {
2972 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002973 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2974 transform->iv_dec,
2975 transform->ivlen,
2976 data, rec->data_len,
2977 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002980 return( ret );
2981 }
2982
Hanno Becker4c6876b2017-12-27 21:28:58 +00002983 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2986 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002987 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002988 }
Paul Bakker68884e32013-01-07 18:20:04 +01002989 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002991#if defined(MBEDTLS_GCM_C) || \
2992 defined(MBEDTLS_CCM_C) || \
2993 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002995 mode == MBEDTLS_MODE_CCM ||
2996 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002997 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002998 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002999 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003000
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003001 /*
3002 * Prepare IV from explicit and implicit data.
3003 */
3004
3005 /* Check that there's enough space for the explicit IV
3006 * (at the beginning of the record) and the MAC (at the
3007 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003008 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003011 "+ taglen (%d)", rec->data_len,
3012 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003014 }
Paul Bakker68884e32013-01-07 18:20:04 +01003015
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003016#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003017 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
3018 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003019 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01003020
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003021 /* Fixed */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003022 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003023 /* Explicit */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003024 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003025 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003026 else
3027#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3028#if defined(MBEDTLS_CHACHAPOLY_C)
3029 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003030 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02003031 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003032 unsigned char i;
3033
Teppo Järvelin91d79382019-10-02 09:09:31 +03003034 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003035
3036 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003037 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003038 }
3039 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003040#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003041 {
3042 /* Reminder if we ever add an AEAD mode with a different size */
3043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3044 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3045 }
3046
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003047 /* Group changes to data, data_len, and add_data, because
3048 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003049 data += explicit_iv_len;
3050 rec->data_offset += explicit_iv_len;
3051 rec->data_len -= explicit_iv_len + transform->taglen;
3052
Hanno Beckere83efe62019-04-29 13:52:53 +01003053 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003054 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01003055 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003056
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003057 /* Because of the check above, we know that there are
3058 * explicit_iv_len Bytes preceeding data, and taglen
3059 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01003060 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003061 * mbedtls_cipher_auth_decrypt() below. */
3062
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003063 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003064 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00003065 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01003066
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003067 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003068 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003069 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003070 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
3071 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01003072 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003073 data, rec->data_len,
3074 data, &olen,
3075 data + rec->data_len,
3076 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
3081 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003082
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003083 return( ret );
3084 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003085 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003086
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003087 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003088 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3091 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003092 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003093 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003094 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3096#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003097 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003099 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003100 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003101
Paul Bakker5121ce52009-01-03 21:22:43 +00003102 /*
Paul Bakker45829992013-01-03 14:52:21 +01003103 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003106 if( mbedtls_ssl_ver_geq(
3107 mbedtls_ssl_transform_get_minor_ver( transform ),
3108 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003109 {
3110 /* The ciphertext is prefixed with the CBC IV. */
3111 minlen += transform->ivlen;
3112 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003113#endif
Paul Bakker45829992013-01-03 14:52:21 +01003114
Hanno Becker4c6876b2017-12-27 21:28:58 +00003115 /* Size considerations:
3116 *
3117 * - The CBC cipher text must not be empty and hence
3118 * at least of size transform->ivlen.
3119 *
3120 * Together with the potential IV-prefix, this explains
3121 * the first of the two checks below.
3122 *
3123 * - The record must contain a MAC, either in plain or
3124 * encrypted, depending on whether Encrypt-then-MAC
3125 * is used or not.
3126 * - If it is, the message contains the IV-prefix,
3127 * the CBC ciphertext, and the MAC.
3128 * - If it is not, the padded plaintext, and hence
3129 * the CBC ciphertext, has at least length maclen + 1
3130 * because there is at least the padding length byte.
3131 *
3132 * As the CBC ciphertext is not empty, both cases give the
3133 * lower bound minlen + maclen + 1 on the record size, which
3134 * we test for in the second check below.
3135 */
3136 if( rec->data_len < minlen + transform->ivlen ||
3137 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003140 "+ 1 ) ( + expl IV )", rec->data_len,
3141 transform->ivlen,
3142 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003144 }
3145
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003146 /*
3147 * Authenticate before decrypt if enabled
3148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003150 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003151 {
Hanno Becker992b6872017-11-09 18:57:39 +00003152 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003155
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003156 /* Update data_len in tandem with add_data.
3157 *
3158 * The subtraction is safe because of the previous check
3159 * data_len >= minlen + maclen + 1.
3160 *
3161 * Afterwards, we know that data + data_len is followed by at
3162 * least maclen Bytes, which justifies the call to
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003163 * mbedtls_platform_memcmp() below.
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003164 *
3165 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003166 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003167 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003168
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003169 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003170 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3171 add_data_len );
3172 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3173 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003174 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3175 data, rec->data_len );
3176 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3177 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003178
Hanno Becker4c6876b2017-12-27 21:28:58 +00003179 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3180 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003181 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003182 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003183
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003184 /* Compare expected MAC with MAC at the end of the record. */
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003185 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003186 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003190 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003191 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003192 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003194
3195 /*
3196 * Check length sanity
3197 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003198
3199 /* We know from above that data_len > minlen >= 0,
3200 * so the following check in particular implies that
3201 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003202 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003205 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003207 }
3208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003210 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003211 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003212 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003213 if( mbedtls_ssl_ver_geq(
3214 mbedtls_ssl_transform_get_minor_ver( transform ),
3215 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003216 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003217 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003218 mbedtls_platform_memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003219
Hanno Becker4c6876b2017-12-27 21:28:58 +00003220 data += transform->ivlen;
3221 rec->data_offset += transform->ivlen;
3222 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003223 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003225
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003226 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3227
Hanno Becker4c6876b2017-12-27 21:28:58 +00003228 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3229 transform->iv_dec, transform->ivlen,
3230 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003233 return( ret );
3234 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003235
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003236 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003237 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3240 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003241 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003244 if( mbedtls_ssl_ver_lt(
3245 mbedtls_ssl_transform_get_minor_ver( transform ),
3246 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02003247 {
3248 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003249 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3250 * records is equivalent to CBC decryption of the concatenation
3251 * of the records; in other words, IVs are maintained across
3252 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003253 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003254 mbedtls_platform_memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003255 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003256 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003257#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003258
Hanno Becker4c6876b2017-12-27 21:28:58 +00003259 /* Safe since data_len >= minlen + maclen + 1, so after having
3260 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003261 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3262 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003263 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003264
Hanno Becker4c6876b2017-12-27 21:28:58 +00003265 if( auth_done == 1 )
3266 {
3267 correct *= ( rec->data_len >= padlen + 1 );
3268 padlen *= ( rec->data_len >= padlen + 1 );
3269 }
3270 else
Paul Bakker45829992013-01-03 14:52:21 +01003271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003273 if( rec->data_len < transform->maclen + padlen + 1 )
3274 {
3275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3276 rec->data_len,
3277 transform->maclen,
3278 padlen + 1 ) );
3279 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003280#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003281
3282 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3283 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003284 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003285
Hanno Becker4c6876b2017-12-27 21:28:58 +00003286 padlen++;
3287
3288 /* Regardless of the validity of the padding,
3289 * we have data_len >= padlen here. */
3290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003292 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3293 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003294 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003295 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297#if defined(MBEDTLS_SSL_DEBUG_ALL)
3298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003299 "should be no more than %d",
3300 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003301#endif
Paul Bakker45829992013-01-03 14:52:21 +01003302 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003303 }
3304 }
3305 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3307#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3308 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003309 if( mbedtls_ssl_ver_gt(
3310 mbedtls_ssl_transform_get_minor_ver( transform ),
3311 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003312 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003313 /* The padding check involves a series of up to 256
3314 * consecutive memory reads at the end of the record
3315 * plaintext buffer. In order to hide the length and
3316 * validity of the padding, always perform exactly
3317 * `min(256,plaintext_len)` reads (but take into account
3318 * only the last `padlen` bytes for the padding check). */
3319 size_t pad_count = 0;
3320 size_t real_count = 0;
3321 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003322
Hanno Becker4c6876b2017-12-27 21:28:58 +00003323 /* Index of first padding byte; it has been ensured above
3324 * that the subtraction is safe. */
3325 size_t const padding_idx = rec->data_len - padlen;
3326 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3327 size_t const start_idx = rec->data_len - num_checks;
3328 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003329
Hanno Becker4c6876b2017-12-27 21:28:58 +00003330 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003331 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003332 real_count |= ( idx >= padding_idx );
3333 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003334 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003335 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003338 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003340#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003341 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003342 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003343 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3345 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3348 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003349 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003350
Hanno Becker4c6876b2017-12-27 21:28:58 +00003351 /* If the padding was found to be invalid, padlen == 0
3352 * and the subtraction is safe. If the padding was found valid,
3353 * padlen hasn't been changed and the previous assertion
3354 * data_len >= padlen still holds. */
3355 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003356 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003357 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003359 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3362 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003363 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003364
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003365#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003366 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003367 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003368#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003369
3370 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003371 * Authenticate if not done yet.
3372 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003373 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003374#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003375 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003376 {
Hanno Becker992b6872017-11-09 18:57:39 +00003377 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003378
Hanno Becker4c6876b2017-12-27 21:28:58 +00003379 /* If the initial value of padlen was such that
3380 * data_len < maclen + padlen + 1, then padlen
3381 * got reset to 1, and the initial check
3382 * data_len >= minlen + maclen + 1
3383 * guarantees that at this point we still
3384 * have at least data_len >= maclen.
3385 *
3386 * If the initial value of padlen was such that
3387 * data_len >= maclen + padlen + 1, then we have
3388 * subtracted either padlen + 1 (if the padding was correct)
3389 * or 0 (if the padding was incorrect) since then,
3390 * hence data_len >= maclen in any case.
3391 */
3392 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003393 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003396 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3397 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003398 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003399 ssl_mac( &transform->md_ctx_dec,
3400 transform->mac_dec,
3401 data, rec->data_len,
3402 rec->ctr, rec->type,
3403 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003404 }
3405 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3407#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3408 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003409 if( mbedtls_ssl_ver_gt(
3410 mbedtls_ssl_transform_get_minor_ver( transform ),
3411 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003412 {
3413 /*
3414 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003415 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003416 *
3417 * Known timing attacks:
3418 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3419 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003420 * To compensate for different timings for the MAC calculation
3421 * depending on how much padding was removed (which is determined
3422 * by padlen), process extra_run more blocks through the hash
3423 * function.
3424 *
3425 * The formula in the paper is
3426 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3427 * where L1 is the size of the header plus the decrypted message
3428 * plus CBC padding and L2 is the size of the header plus the
3429 * decrypted message. This is for an underlying hash function
3430 * with 64-byte blocks.
3431 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3432 * correctly. We round down instead of up, so -56 is the correct
3433 * value for our calculations instead of -55.
3434 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003435 * Repeat the formula rather than defining a block_size variable.
3436 * This avoids requiring division by a variable at runtime
3437 * (which would be marginally less efficient and would require
3438 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003439 */
3440 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003441 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003442
3443 /*
3444 * The next two sizes are the minimum and maximum values of
3445 * in_msglen over all padlen values.
3446 *
3447 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003448 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003449 *
3450 * Note that max_len + maclen is never more than the buffer
3451 * length, as we previously did in_msglen -= maclen too.
3452 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003453 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003454 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3455
Hanno Becker4c6876b2017-12-27 21:28:58 +00003456 memset( tmp, 0, sizeof( tmp ) );
3457
Hanno Beckera5cedbc2019-07-17 11:21:02 +01003458 switch( mbedtls_md_get_type(
3459 mbedtls_md_get_handle( &transform->md_ctx_dec ) ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003460 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003461#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3462 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003463 case MBEDTLS_MD_MD5:
3464 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003465 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003466 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003467 extra_run =
3468 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3469 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003470 break;
3471#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003472#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003473 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003474 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003475 extra_run =
3476 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3477 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003478 break;
3479#endif
3480 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003482 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3483 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003484
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003485 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003486
Hanno Beckere83efe62019-04-29 13:52:53 +01003487 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3488 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003489 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3490 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003491 /* Make sure we access everything even when padlen > 0. This
3492 * makes the synchronisation requirements for just-in-time
3493 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003494 ssl_read_memory( data + rec->data_len, padlen );
3495 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003496
3497 /* Call mbedtls_md_process at least once due to cache attacks
3498 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003499 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003500 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003501
Hanno Becker4c6876b2017-12-27 21:28:58 +00003502 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003503
3504 /* Make sure we access all the memory that could contain the MAC,
3505 * before we check it in the next code block. This makes the
3506 * synchronisation requirements for just-in-time Prime+Probe
3507 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003508 ssl_read_memory( data + min_len,
3509 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003510 }
3511 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3513 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3516 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003517 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003518
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003519#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003520 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3521 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003522#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003523
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003524 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003525 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527#if defined(MBEDTLS_SSL_DEBUG_ALL)
3528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003529#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003530 correct = 0;
3531 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003532 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003533 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003534
3535 /*
3536 * Finally check the correct flag
3537 */
3538 if( correct == 0 )
3539 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003540#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003541
3542 /* Make extra sure authentication was performed, exactly once */
3543 if( auth_done != 1 )
3544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3546 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003547 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003548
Hanno Beckera5a2b082019-05-15 14:03:01 +01003549#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003550 if( rec->cid_len != 0 )
3551 {
3552 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3553 &rec->type );
3554 if( ret != 0 )
3555 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3556 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003557#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003560
3561 return( 0 );
3562}
3563
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003564#undef MAC_NONE
3565#undef MAC_PLAINTEXT
3566#undef MAC_CIPHERTEXT
3567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003569/*
3570 * Compression/decompression functions
3571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003573{
3574 int ret;
3575 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003576 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003577 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003578 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003581
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003582 if( len_pre == 0 )
3583 return( 0 );
3584
Teppo Järvelin91d79382019-10-02 09:09:31 +03003585 mbedtls_platform_memcpy( msg_pre, ssl->out_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003588 ssl->out_msglen ) );
3589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003591 ssl->out_msg, ssl->out_msglen );
3592
Paul Bakker48916f92012-09-16 19:57:18 +00003593 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3594 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3595 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003596 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003597
Paul Bakker48916f92012-09-16 19:57:18 +00003598 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003599 if( ret != Z_OK )
3600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3602 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003603 }
3604
Angus Grattond8213d02016-05-25 20:56:48 +10003605 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003606 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003609 ssl->out_msglen ) );
3610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003612 ssl->out_msg, ssl->out_msglen );
3613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003615
3616 return( 0 );
3617}
3618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003620{
3621 int ret;
3622 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003623 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003624 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003625 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003628
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003629 if( len_pre == 0 )
3630 return( 0 );
3631
Teppo Järvelin91d79382019-10-02 09:09:31 +03003632 mbedtls_platform_memcpy( msg_pre, ssl->in_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003634 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003635 ssl->in_msglen ) );
3636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003638 ssl->in_msg, ssl->in_msglen );
3639
Paul Bakker48916f92012-09-16 19:57:18 +00003640 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3641 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3642 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003643 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003644 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003645
Paul Bakker48916f92012-09-16 19:57:18 +00003646 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003647 if( ret != Z_OK )
3648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3650 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003651 }
3652
Angus Grattond8213d02016-05-25 20:56:48 +10003653 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003654 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003657 ssl->in_msglen ) );
3658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003660 ssl->in_msg, ssl->in_msglen );
3661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003663
3664 return( 0 );
3665}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3669static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671#if defined(MBEDTLS_SSL_PROTO_DTLS)
3672static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003673{
3674 /* If renegotiation is not enforced, retransmit until we would reach max
3675 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003676 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003677 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003678 uint32_t ratio =
3679 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3680 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003681 unsigned char doublings = 1;
3682
3683 while( ratio != 0 )
3684 {
3685 ++doublings;
3686 ratio >>= 1;
3687 }
3688
3689 if( ++ssl->renego_records_seen > doublings )
3690 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003692 return( 0 );
3693 }
3694 }
3695
3696 return( ssl_write_hello_request( ssl ) );
3697}
3698#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003699#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003700
Paul Bakker5121ce52009-01-03 21:22:43 +00003701/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003702 * Fill the input message buffer by appending data to it.
3703 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003704 *
3705 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3706 * available (from this read and/or a previous one). Otherwise, an error code
3707 * is returned (possibly EOF or WANT_READ).
3708 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003709 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3710 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3711 * since we always read a whole datagram at once.
3712 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003713 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003714 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003717{
Paul Bakker23986e52011-04-24 08:57:21 +00003718 int ret;
3719 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003722
Hanno Beckera58a8962019-06-13 16:11:15 +01003723 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3724 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003727 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003729 }
3730
Angus Grattond8213d02016-05-25 20:56:48 +10003731 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3734 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003735 }
3736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003738 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003739 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003740 uint32_t timeout;
3741
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003742 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003743 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3744 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003745 {
3746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3747 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3748 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3749 }
3750
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003751 /*
3752 * The point is, we need to always read a full datagram at once, so we
3753 * sometimes read more then requested, and handle the additional data.
3754 * It could be the rest of the current record (while fetching the
3755 * header) and/or some other records in the same datagram.
3756 */
3757
3758 /*
3759 * Move to the next record in the already read datagram if applicable
3760 */
3761 if( ssl->next_record_offset != 0 )
3762 {
3763 if( ssl->in_left < ssl->next_record_offset )
3764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3766 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003767 }
3768
3769 ssl->in_left -= ssl->next_record_offset;
3770
3771 if( ssl->in_left != 0 )
3772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003774 ssl->next_record_offset ) );
3775 memmove( ssl->in_hdr,
3776 ssl->in_hdr + ssl->next_record_offset,
3777 ssl->in_left );
3778 }
3779
3780 ssl->next_record_offset = 0;
3781 }
3782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003784 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003785
3786 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003787 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003788 */
3789 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003792 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003793 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003794
3795 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003796 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003797 * are not at the beginning of a new record, the caller did something
3798 * wrong.
3799 */
3800 if( ssl->in_left != 0 )
3801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3803 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003804 }
3805
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003806 /*
3807 * Don't even try to read if time's out already.
3808 * This avoids by-passing the timer when repeatedly receiving messages
3809 * that will end up being dropped.
3810 */
3811 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003812 {
3813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003814 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003815 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003816 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003817 {
Angus Grattond8213d02016-05-25 20:56:48 +10003818 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003820 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003821 timeout = ssl->handshake->retransmit_timeout;
3822 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003823 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003825 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003826
Hanno Beckera58a8962019-06-13 16:11:15 +01003827 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3828 {
3829 ret = mbedtls_ssl_get_recv_timeout( ssl )
3830 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3831 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003832 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003833 {
3834 ret = mbedtls_ssl_get_recv( ssl )
3835 ( ssl->p_bio, ssl->in_hdr, len );
3836 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003839
3840 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003842 }
3843
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003844 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003847 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003850 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003851 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003854 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003855 }
3856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003859 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003860 return( ret );
3861 }
3862
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003863 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003864 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003866 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3867 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003868 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003869 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003870 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003873 return( ret );
3874 }
3875
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003876 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003877 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003879 }
3880
Paul Bakker5121ce52009-01-03 21:22:43 +00003881 if( ret < 0 )
3882 return( ret );
3883
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003884 ssl->in_left = ret;
3885 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003886 MBEDTLS_SSL_TRANSPORT_ELSE
3887#endif /* MBEDTLS_SSL_PROTO_DTLS */
3888#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003891 ssl->in_left, nb_want ) );
3892
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003893 while( ssl->in_left < nb_want )
3894 {
3895 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003896
3897 if( ssl_check_timer( ssl ) != 0 )
3898 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3899 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003900 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003901 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003902 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003903 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3904 ssl->in_hdr + ssl->in_left, len,
3905 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003906 }
3907 else
3908 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003909 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003910 ssl->in_hdr + ssl->in_left, len );
3911 }
3912 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003915 ssl->in_left, nb_want ) );
3916 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003917
3918 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003920
3921 if( ret < 0 )
3922 return( ret );
3923
mohammad160352aecb92018-03-28 23:41:40 -07003924 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003925 {
Darryl Green11999bb2018-03-13 15:22:58 +00003926 MBEDTLS_SSL_DEBUG_MSG( 1,
3927 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003928 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003929 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3930 }
3931
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003932 ssl->in_left += ret;
3933 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003934 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003935#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003938
3939 return( 0 );
3940}
3941
3942/*
3943 * Flush any data not yet written
3944 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003945int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003946{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003947 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003948 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003951
Hanno Beckera58a8962019-06-13 16:11:15 +01003952 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003955 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003957 }
3958
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003959 /* Avoid incrementing counter if data is flushed */
3960 if( ssl->out_left == 0 )
3961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003963 return( 0 );
3964 }
3965
Paul Bakker5121ce52009-01-03 21:22:43 +00003966 while( ssl->out_left > 0 )
3967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003969 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003970
Hanno Becker2b1e3542018-08-06 11:19:13 +01003971 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003972 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003974 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003975
3976 if( ret <= 0 )
3977 return( ret );
3978
mohammad160352aecb92018-03-28 23:41:40 -07003979 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003980 {
Darryl Green11999bb2018-03-13 15:22:58 +00003981 MBEDTLS_SSL_DEBUG_MSG( 1,
3982 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003983 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003984 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3985 }
3986
Paul Bakker5121ce52009-01-03 21:22:43 +00003987 ssl->out_left -= ret;
3988 }
3989
Hanno Becker2b1e3542018-08-06 11:19:13 +01003990#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003991 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003992 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003993 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003994 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003995 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003996#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003997#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003998 {
3999 ssl->out_hdr = ssl->out_buf + 8;
4000 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004001#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01004002 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004005
4006 return( 0 );
4007}
4008
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004009/*
4010 * Functions to handle the DTLS retransmission state machine
4011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004012#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004013/*
4014 * Append current handshake message to current outgoing flight
4015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004017{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01004019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
4020 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
4021 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004022
4023 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004024 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004025 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004028 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004029 }
4030
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004031 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004032 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004034 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004035 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004036 }
4037
4038 /* Copy current handshake message with headers */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004039 mbedtls_platform_memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004040 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004041 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004042 msg->next = NULL;
4043
4044 /* Append to the current flight */
4045 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004046 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004047 else
4048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004049 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004050 while( cur->next != NULL )
4051 cur = cur->next;
4052 cur->next = msg;
4053 }
4054
Hanno Becker3b235902018-08-06 09:54:53 +01004055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004056 return( 0 );
4057}
4058
4059/*
4060 * Free the current flight of handshake messages
4061 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004062static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004063{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064 mbedtls_ssl_flight_item *cur = flight;
4065 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004066
4067 while( cur != NULL )
4068 {
4069 next = cur->next;
4070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071 mbedtls_free( cur->p );
4072 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004073
4074 cur = next;
4075 }
4076}
4077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4079static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004080#endif
4081
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004082/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004083 * Swap transform_out and out_ctr with the alternative ones
4084 */
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004085static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004086{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004088 unsigned char tmp_out_ctr[8];
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004089#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4090 int ret;
4091#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004092
4093 if( ssl->transform_out == ssl->handshake->alt_transform_out )
4094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004096 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004097 }
4098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004100
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004101 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004102 tmp_transform = ssl->transform_out;
4103 ssl->transform_out = ssl->handshake->alt_transform_out;
4104 ssl->handshake->alt_transform_out = tmp_transform;
4105
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004106 /* Swap epoch + sequence_number */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004107 mbedtls_platform_memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4108 mbedtls_platform_memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
4109 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004110
4111 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004112 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004114#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4115 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004117 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4120 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004121 }
4122 }
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004123#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
4124
4125 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004126}
4127
4128/*
4129 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004130 */
4131int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4132{
4133 int ret = 0;
4134
4135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4136
4137 ret = mbedtls_ssl_flight_transmit( ssl );
4138
4139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4140
4141 return( ret );
4142}
4143
4144/*
4145 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004146 *
4147 * Need to remember the current message in case flush_output returns
4148 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004149 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004150 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004151int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004152{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004153 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004157 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004159
4160 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004161 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004162 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4163 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004165 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004166 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004167
4168 while( ssl->handshake->cur_msg != NULL )
4169 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004170 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004171 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004172
Hanno Beckere1dcb032018-08-17 16:47:58 +01004173 int const is_finished =
4174 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4175 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4176
Hanno Becker04da1892018-08-14 13:22:10 +01004177 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4178 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4179
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004180 /* Swap epochs before sending Finished: we can't do it after
4181 * sending ChangeCipherSpec, in case write returns WANT_READ.
4182 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004183 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004184 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004186 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4187 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004188 }
4189
Hanno Becker67bc7c32018-08-06 11:33:50 +01004190 ret = ssl_get_remaining_payload_in_datagram( ssl );
4191 if( ret < 0 )
4192 return( ret );
4193 max_frag_len = (size_t) ret;
4194
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004195 /* CCS is copied as is, while HS messages may need fragmentation */
4196 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4197 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004198 if( max_frag_len == 0 )
4199 {
4200 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4201 return( ret );
4202
4203 continue;
4204 }
4205
Teppo Järvelin91d79382019-10-02 09:09:31 +03004206 mbedtls_platform_memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004207 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004208 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004209
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004210 /* Update position inside current message */
4211 ssl->handshake->cur_msg_p += cur->len;
4212 }
4213 else
4214 {
4215 const unsigned char * const p = ssl->handshake->cur_msg_p;
4216 const size_t hs_len = cur->len - 12;
4217 const size_t frag_off = p - ( cur->p + 12 );
4218 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004219 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004220
Hanno Beckere1dcb032018-08-17 16:47:58 +01004221 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004222 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004223 if( is_finished )
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004224 {
4225 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4226 return( ret );
4227 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004228
Hanno Becker67bc7c32018-08-06 11:33:50 +01004229 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4230 return( ret );
4231
4232 continue;
4233 }
4234 max_hs_frag_len = max_frag_len - 12;
4235
4236 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4237 max_hs_frag_len : rem_len;
4238
4239 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004240 {
4241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004242 (unsigned) cur_hs_frag_len,
4243 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004244 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004245
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004246 /* Messages are stored with handshake headers as if not fragmented,
4247 * copy beginning of headers then fill fragmentation fields.
4248 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004249 mbedtls_platform_memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004250
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004251 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[6], frag_off );
4252 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[9],
4253 cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004254
4255 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4256
Hanno Becker3f7b9732018-08-28 09:53:25 +01004257 /* Copy the handshake message content and set records fields */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004258 mbedtls_platform_memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004259 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004260 ssl->out_msgtype = cur->type;
4261
4262 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004263 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004264 }
4265
4266 /* If done with the current message move to the next one if any */
4267 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4268 {
4269 if( cur->next != NULL )
4270 {
4271 ssl->handshake->cur_msg = cur->next;
4272 ssl->handshake->cur_msg_p = cur->next->p + 12;
4273 }
4274 else
4275 {
4276 ssl->handshake->cur_msg = NULL;
4277 ssl->handshake->cur_msg_p = NULL;
4278 }
4279 }
4280
4281 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004282 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004285 return( ret );
4286 }
4287 }
4288
Hanno Becker67bc7c32018-08-06 11:33:50 +01004289 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4290 return( ret );
4291
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004292 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004293 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4294 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004295 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004298 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4299 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004300
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004302
4303 return( 0 );
4304}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004305
4306/*
4307 * To be called when the last message of an incoming flight is received.
4308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004309void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004310{
4311 /* We won't need to resend that one any more */
4312 ssl_flight_free( ssl->handshake->flight );
4313 ssl->handshake->flight = NULL;
4314 ssl->handshake->cur_msg = NULL;
4315
4316 /* The next incoming flight will start with this msg_seq */
4317 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4318
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004319 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004320 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004321
Hanno Becker0271f962018-08-16 13:23:47 +01004322 /* Clear future message buffering structure. */
4323 ssl_buffering_free( ssl );
4324
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004325 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004326 ssl_set_timer( ssl, 0 );
4327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004328 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4329 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004332 }
4333 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004335}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004336
4337/*
4338 * To be called when the last message of an outgoing flight is send.
4339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004341{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004342 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004343 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004345 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4346 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004348 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004349 }
4350 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004352}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004353#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004354
Paul Bakker5121ce52009-01-03 21:22:43 +00004355/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004356 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004357 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004358
4359/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004360 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004361 *
4362 * - fill in handshake headers
4363 * - update handshake checksum
4364 * - DTLS: save message for resending
4365 * - then pass to the record layer
4366 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004367 * DTLS: except for HelloRequest, messages are only queued, and will only be
4368 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004369 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004370 * Inputs:
4371 * - ssl->out_msglen: 4 + actual handshake message len
4372 * (4 is the size of handshake headers for TLS)
4373 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4374 * - ssl->out_msg + 4: the handshake message body
4375 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004376 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004377 * - ssl->out_msglen: the length of the record contents
4378 * (including handshake headers but excluding record headers)
4379 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004380 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004381int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004382{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004383 int ret;
4384 const size_t hs_len = ssl->out_msglen - 4;
4385 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004386
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4388
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004389 /*
4390 * Sanity checks
4391 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004392 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004393 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4394 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004395 /* In SSLv3, the client might send a NoCertificate alert. */
4396#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004397 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004398 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004399 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4400 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004401#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4402 {
4403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4404 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4405 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004406 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004407
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004408 /* Whenever we send anything different from a
4409 * HelloRequest we should be in a handshake - double check. */
4410 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4411 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004412 ssl->handshake == NULL )
4413 {
4414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4415 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4416 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004418#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004419 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004420 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004421 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004422 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4424 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004425 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004426#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004427
Hanno Beckerb50a2532018-08-06 11:52:54 +01004428 /* Double-check that we did not exceed the bounds
4429 * of the outgoing record buffer.
4430 * This should never fail as the various message
4431 * writing functions must obey the bounds of the
4432 * outgoing record buffer, but better be safe.
4433 *
4434 * Note: We deliberately do not check for the MTU or MFL here.
4435 */
4436 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4437 {
4438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4439 "size %u, maximum %u",
4440 (unsigned) ssl->out_msglen,
4441 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4442 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4443 }
4444
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004445 /*
4446 * Fill handshake headers
4447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004448 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004449 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004450 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[1], hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004451
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004452 /*
4453 * DTLS has additional fields in the Handshake layer,
4454 * between the length field and the actual payload:
4455 * uint16 message_seq;
4456 * uint24 fragment_offset;
4457 * uint24 fragment_length;
4458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004460 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004461 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004462 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004463 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004464 {
4465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4466 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004467 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004468 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004469 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4470 }
4471
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004472 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004473 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004474
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004475 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004476 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004477 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004478 (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[4],
4479 ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004480 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004481 }
4482 else
4483 {
4484 ssl->out_msg[4] = 0;
4485 ssl->out_msg[5] = 0;
4486 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004487
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004488 /* Handshake hashes are computed without fragmentation,
4489 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004490 mbedtls_platform_memset( ssl->out_msg + 6, 0x00, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03004491 mbedtls_platform_memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004492 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004493#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004494
Hanno Becker0207e532018-08-28 10:28:28 +01004495 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004496 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004497 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004498 }
4499
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004500 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004501#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004502 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004503 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4504 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004505 {
4506 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004508 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004509 return( ret );
4510 }
4511 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004512 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004513#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004514 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004515 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004516 {
4517 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4518 return( ret );
4519 }
4520 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004521
4522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4523
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004524 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004525}
4526
4527/*
4528 * Record layer functions
4529 */
4530
4531/*
4532 * Write current record.
4533 *
4534 * Uses:
4535 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4536 * - ssl->out_msglen: length of the record content (excl headers)
4537 * - ssl->out_msg: record content
4538 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004539int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004540{
4541 int ret, done = 0;
4542 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004543 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004544
4545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004548 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004549 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004550 {
4551 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004553 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004554 return( ret );
4555 }
4556
4557 len = ssl->out_msglen;
4558 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004559#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004561#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4562 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004566 ret = mbedtls_ssl_hw_record_write( ssl );
4567 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004569 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4570 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004571 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004572
4573 if( ret == 0 )
4574 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004575 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004576#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004577 if( !done )
4578 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004579 unsigned i;
4580 size_t protected_record_size;
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004581 volatile int encrypted_fi = 0;
Hanno Becker2b1e3542018-08-06 11:19:13 +01004582
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004583 /* Skip writing the record content type to after the encryption,
4584 * as it may change when using the CID extension. */
4585
Hanno Becker2881d802019-05-22 14:44:53 +01004586 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4587 mbedtls_ssl_get_minor_ver( ssl ),
4588 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004589
Teppo Järvelin91d79382019-10-02 09:09:31 +03004590 mbedtls_platform_memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004591 (void)mbedtls_platform_put_uint16_be( ssl->out_len, len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004592
Paul Bakker48916f92012-09-16 19:57:18 +00004593 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004594 {
Hanno Becker3307b532017-12-27 21:37:21 +00004595 mbedtls_record rec;
4596
4597 rec.buf = ssl->out_iv;
4598 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4599 ( ssl->out_iv - ssl->out_buf );
4600 rec.data_len = ssl->out_msglen;
4601 rec.data_offset = ssl->out_msg - rec.buf;
4602
Teppo Järvelin91d79382019-10-02 09:09:31 +03004603 mbedtls_platform_memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004604 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4605 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004606 ssl->conf->transport, rec.ver );
4607 rec.type = ssl->out_msgtype;
4608
Hanno Beckera5a2b082019-05-15 14:03:01 +01004609#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004610 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004611 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004612#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004613
Hanno Becker611a83b2018-01-03 14:27:32 +00004614 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004615 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004616 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004618 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004619 return( ret );
4620 }
4621
Hanno Becker3307b532017-12-27 21:37:21 +00004622 if( rec.data_offset != 0 )
4623 {
4624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4625 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4626 }
4627
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004628 /* Update the record content type and CID. */
4629 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004630#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03004631 /* Not using more secure mbedtls_platform_memcpy as cid is public */
4632 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004633#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004634 ssl->out_msglen = len = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004635 (void)mbedtls_platform_put_uint16_be( ssl->out_len, rec.data_len );
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004636 encrypted_fi = 1;
4637 }
4638
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004639 /* Double check to ensure the encryption has been done */
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004640 if( ssl->transform_out != NULL && encrypted_fi == 0 )
4641 {
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004642 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004643 }
4644
Hanno Becker43395762019-05-03 14:46:38 +01004645 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004646
4647#if defined(MBEDTLS_SSL_PROTO_DTLS)
4648 /* In case of DTLS, double-check that we don't exceed
4649 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004650 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004651 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004652 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004653 if( ret < 0 )
4654 return( ret );
4655
4656 if( protected_record_size > (size_t) ret )
4657 {
4658 /* Should never happen */
4659 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4660 }
4661 }
4662#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004663
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004664 /* Now write the potentially updated record content type. */
4665 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004667 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004668 "version = [%d:%d], msglen = %d",
4669 ssl->out_hdr[0], ssl->out_hdr[1],
4670 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004672 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004673 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004674
4675 ssl->out_left += protected_record_size;
4676 ssl->out_hdr += protected_record_size;
4677 ssl_update_out_pointers( ssl, ssl->transform_out );
4678
Hanno Becker04484622018-08-06 09:49:38 +01004679 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4680 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4681 break;
4682
4683 /* The loop goes to its end iff the counter is wrapping */
4684 if( i == ssl_ep_len( ssl ) )
4685 {
4686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4687 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4688 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004689 }
4690
Hanno Becker67bc7c32018-08-06 11:33:50 +01004691#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004692 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004693 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004694 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004695 size_t remaining;
4696 ret = ssl_get_remaining_payload_in_datagram( ssl );
4697 if( ret < 0 )
4698 {
4699 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4700 ret );
4701 return( ret );
4702 }
4703
4704 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004705 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004706 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004707 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004708 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004709 else
4710 {
Hanno Becker513815a2018-08-20 11:56:09 +01004711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004712 }
4713 }
4714#endif /* MBEDTLS_SSL_PROTO_DTLS */
4715
4716 if( ( flush == SSL_FORCE_FLUSH ) &&
4717 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004719 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004720 return( ret );
4721 }
4722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004724
4725 return( 0 );
4726}
4727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004728#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004729
4730static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4731{
4732 if( ssl->in_msglen < ssl->in_hslen ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +03004733 mbedtls_platform_memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4734 mbedtls_platform_memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
Hanno Beckere25e3b72018-08-16 09:30:53 +01004735 {
4736 return( 1 );
4737 }
4738 return( 0 );
4739}
Hanno Becker44650b72018-08-16 12:51:11 +01004740
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004741static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004742{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004743 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[9] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004744}
4745
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004746static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004747{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004748 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[6] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004749}
4750
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004751static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004752{
4753 uint32_t msg_len, frag_off, frag_len;
4754
4755 msg_len = ssl_get_hs_total_len( ssl );
4756 frag_off = ssl_get_hs_frag_off( ssl );
4757 frag_len = ssl_get_hs_frag_len( ssl );
4758
4759 if( frag_off > msg_len )
4760 return( -1 );
4761
4762 if( frag_len > msg_len - frag_off )
4763 return( -1 );
4764
4765 if( frag_len + 12 > ssl->in_msglen )
4766 return( -1 );
4767
4768 return( 0 );
4769}
4770
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004771/*
4772 * Mark bits in bitmask (used for DTLS HS reassembly)
4773 */
4774static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4775{
4776 unsigned int start_bits, end_bits;
4777
4778 start_bits = 8 - ( offset % 8 );
4779 if( start_bits != 8 )
4780 {
4781 size_t first_byte_idx = offset / 8;
4782
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004783 /* Special case */
4784 if( len <= start_bits )
4785 {
4786 for( ; len != 0; len-- )
4787 mask[first_byte_idx] |= 1 << ( start_bits - len );
4788
4789 /* Avoid potential issues with offset or len becoming invalid */
4790 return;
4791 }
4792
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004793 offset += start_bits; /* Now offset % 8 == 0 */
4794 len -= start_bits;
4795
4796 for( ; start_bits != 0; start_bits-- )
4797 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4798 }
4799
4800 end_bits = len % 8;
4801 if( end_bits != 0 )
4802 {
4803 size_t last_byte_idx = ( offset + len ) / 8;
4804
4805 len -= end_bits; /* Now len % 8 == 0 */
4806
4807 for( ; end_bits != 0; end_bits-- )
4808 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4809 }
4810
4811 memset( mask + offset / 8, 0xFF, len / 8 );
4812}
4813
4814/*
4815 * Check that bitmask is full
4816 */
4817static int ssl_bitmask_check( unsigned char *mask, size_t len )
4818{
4819 size_t i;
4820
4821 for( i = 0; i < len / 8; i++ )
4822 if( mask[i] != 0xFF )
4823 return( -1 );
4824
4825 for( i = 0; i < len % 8; i++ )
4826 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4827 return( -1 );
4828
4829 return( 0 );
4830}
4831
Hanno Becker56e205e2018-08-16 09:06:12 +01004832/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004833static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004834 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004835{
Hanno Becker56e205e2018-08-16 09:06:12 +01004836 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004837
Hanno Becker56e205e2018-08-16 09:06:12 +01004838 alloc_len = 12; /* Handshake header */
4839 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004840
Hanno Beckerd07df862018-08-16 09:14:58 +01004841 if( add_bitmap )
4842 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004843
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004844 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004845}
Hanno Becker56e205e2018-08-16 09:06:12 +01004846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004847#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004848
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004849static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004850{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004851 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[1] ) );
Hanno Becker12555c62018-08-16 12:47:53 +01004852}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004853
Simon Butcher99000142016-10-13 17:21:01 +01004854int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004855{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004856 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004859 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004860 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004861 }
4862
Hanno Becker12555c62018-08-16 12:47:53 +01004863 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004865 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004866 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004867 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004869#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004870 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004871 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004872 int ret;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004873 unsigned int recv_msg_seq = (unsigned int)
4874 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004875
Hanno Becker44650b72018-08-16 12:51:11 +01004876 if( ssl_check_hs_header( ssl ) != 0 )
4877 {
4878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4879 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4880 }
4881
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004882 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004883 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4884 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4885 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4886 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004887 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004888 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4889 {
4890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4891 recv_msg_seq,
4892 ssl->handshake->in_msg_seq ) );
4893 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4894 }
4895
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004896 /* Retransmit only on last message from previous flight, to avoid
4897 * too many retransmissions.
4898 * Besides, No sane server ever retransmits HelloVerifyRequest */
4899 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004900 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004903 "message_seq = %d, start_of_flight = %d",
4904 recv_msg_seq,
4905 ssl->handshake->in_flight_start_seq ) );
4906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004907 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004909 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004910 return( ret );
4911 }
4912 }
4913 else
4914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004916 "message_seq = %d, expected = %d",
4917 recv_msg_seq,
4918 ssl->handshake->in_msg_seq ) );
4919 }
4920
Hanno Becker90333da2017-10-10 11:27:13 +01004921 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004922 }
4923 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004924
Hanno Becker6d97ef52018-08-16 13:09:04 +01004925 /* Message reassembly is handled alongside buffering of future
4926 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004927 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004928 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004929 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004932 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004933 }
4934 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004935 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004936#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004937#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004938 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004939 /* With TLS we don't handle fragmentation (for now) */
4940 if( ssl->in_msglen < ssl->in_hslen )
4941 {
4942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4943 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4944 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004945 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004946#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004947
Simon Butcher99000142016-10-13 17:21:01 +01004948 return( 0 );
4949}
4950
4951void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4952{
Hanno Becker0271f962018-08-16 13:23:47 +01004953 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004954
Hanno Becker0271f962018-08-16 13:23:47 +01004955 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004956 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004957
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004958 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004959#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004960 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004961 ssl->handshake != NULL )
4962 {
Hanno Becker0271f962018-08-16 13:23:47 +01004963 unsigned offset;
4964 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004965
Hanno Becker0271f962018-08-16 13:23:47 +01004966 /* Increment handshake sequence number */
4967 hs->in_msg_seq++;
4968
4969 /*
4970 * Clear up handshake buffering and reassembly structure.
4971 */
4972
4973 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004974 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004975
4976 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004977 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4978 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004979 offset++, hs_buf++ )
4980 {
4981 *hs_buf = *(hs_buf + 1);
4982 }
4983
4984 /* Create a fresh last entry */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004985 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004986 }
4987#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004988}
4989
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004990/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004991 * DTLS anti-replay: RFC 6347 4.1.2.6
4992 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004993 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4994 * Bit n is set iff record number in_window_top - n has been seen.
4995 *
4996 * Usually, in_window_top is the last record number seen and the lsb of
4997 * in_window is set. The only exception is the initial state (record number 0
4998 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004999 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005000#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5001static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005002{
5003 ssl->in_window_top = 0;
5004 ssl->in_window = 0;
5005}
5006
5007static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
5008{
5009 return( ( (uint64_t) buf[0] << 40 ) |
5010 ( (uint64_t) buf[1] << 32 ) |
5011 ( (uint64_t) buf[2] << 24 ) |
5012 ( (uint64_t) buf[3] << 16 ) |
5013 ( (uint64_t) buf[4] << 8 ) |
5014 ( (uint64_t) buf[5] ) );
5015}
5016
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005017static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
5018{
5019 int ret;
5020 unsigned char *original_in_ctr;
5021
5022 // save original in_ctr
5023 original_in_ctr = ssl->in_ctr;
5024
5025 // use counter from record
5026 ssl->in_ctr = record_in_ctr;
5027
5028 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
5029
5030 // restore the counter
5031 ssl->in_ctr = original_in_ctr;
5032
5033 return ret;
5034}
5035
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005036/*
5037 * Return 0 if sequence number is acceptable, -1 otherwise
5038 */
Hanno Beckerfc551722019-07-12 08:50:37 +01005039int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005040{
5041 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5042 uint64_t bit;
5043
Hanno Becker7f376f42019-06-12 16:20:48 +01005044 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5045 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5046 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005047 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01005048 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005049
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005050 if( rec_seqnum > ssl->in_window_top )
5051 return( 0 );
5052
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005053 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005054
5055 if( bit >= 64 )
5056 return( -1 );
5057
5058 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
5059 return( -1 );
5060
5061 return( 0 );
5062}
5063
5064/*
5065 * Update replay window on new validated record
5066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005068{
5069 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5070
Hanno Becker7f376f42019-06-12 16:20:48 +01005071 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5072 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5073 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005074 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01005075 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005076
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005077 if( rec_seqnum > ssl->in_window_top )
5078 {
5079 /* Update window_top and the contents of the window */
5080 uint64_t shift = rec_seqnum - ssl->in_window_top;
5081
5082 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005083 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005084 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005085 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005086 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005087 ssl->in_window |= 1;
5088 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005089
5090 ssl->in_window_top = rec_seqnum;
5091 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005092 else
5093 {
5094 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005095 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005096
5097 if( bit < 64 ) /* Always true, but be extra sure */
5098 ssl->in_window |= (uint64_t) 1 << bit;
5099 }
5100}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005101#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005102
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005103#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005104/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005105static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
5106
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005107/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005108 * Without any SSL context, check if a datagram looks like a ClientHello with
5109 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01005110 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005111 *
5112 * - if cookie is valid, return 0
5113 * - if ClientHello looks superficially valid but cookie is not,
5114 * fill obuf and set olen, then
5115 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
5116 * - otherwise return a specific error code
5117 */
5118static int ssl_check_dtls_clihlo_cookie(
5119 mbedtls_ssl_cookie_write_t *f_cookie_write,
5120 mbedtls_ssl_cookie_check_t *f_cookie_check,
5121 void *p_cookie,
5122 const unsigned char *cli_id, size_t cli_id_len,
5123 const unsigned char *in, size_t in_len,
5124 unsigned char *obuf, size_t buf_len, size_t *olen )
5125{
5126 size_t sid_len, cookie_len;
5127 unsigned char *p;
5128
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005129 /*
5130 * Structure of ClientHello with record and handshake headers,
5131 * and expected values. We don't need to check a lot, more checks will be
5132 * done when actually parsing the ClientHello - skipping those checks
5133 * avoids code duplication and does not make cookie forging any easier.
5134 *
5135 * 0-0 ContentType type; copied, must be handshake
5136 * 1-2 ProtocolVersion version; copied
5137 * 3-4 uint16 epoch; copied, must be 0
5138 * 5-10 uint48 sequence_number; copied
5139 * 11-12 uint16 length; (ignored)
5140 *
5141 * 13-13 HandshakeType msg_type; (ignored)
5142 * 14-16 uint24 length; (ignored)
5143 * 17-18 uint16 message_seq; copied
5144 * 19-21 uint24 fragment_offset; copied, must be 0
5145 * 22-24 uint24 fragment_length; (ignored)
5146 *
5147 * 25-26 ProtocolVersion client_version; (ignored)
5148 * 27-58 Random random; (ignored)
5149 * 59-xx SessionID session_id; 1 byte len + sid_len content
5150 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5151 * ...
5152 *
5153 * Minimum length is 61 bytes.
5154 */
5155 if( in_len < 61 ||
5156 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5157 in[3] != 0 || in[4] != 0 ||
5158 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5159 {
5160 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5161 }
5162
5163 sid_len = in[59];
5164 if( sid_len > in_len - 61 )
5165 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5166
5167 cookie_len = in[60 + sid_len];
5168 if( cookie_len > in_len - 60 )
5169 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5170
5171 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5172 cli_id, cli_id_len ) == 0 )
5173 {
5174 /* Valid cookie */
5175 return( 0 );
5176 }
5177
5178 /*
5179 * If we get here, we've got an invalid cookie, let's prepare HVR.
5180 *
5181 * 0-0 ContentType type; copied
5182 * 1-2 ProtocolVersion version; copied
5183 * 3-4 uint16 epoch; copied
5184 * 5-10 uint48 sequence_number; copied
5185 * 11-12 uint16 length; olen - 13
5186 *
5187 * 13-13 HandshakeType msg_type; hello_verify_request
5188 * 14-16 uint24 length; olen - 25
5189 * 17-18 uint16 message_seq; copied
5190 * 19-21 uint24 fragment_offset; copied
5191 * 22-24 uint24 fragment_length; olen - 25
5192 *
5193 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5194 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5195 *
5196 * Minimum length is 28.
5197 */
5198 if( buf_len < 28 )
5199 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5200
5201 /* Copy most fields and adapt others */
Teppo Järvelin91d79382019-10-02 09:09:31 +03005202 mbedtls_platform_memcpy( obuf, in, 25 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005203 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5204 obuf[25] = 0xfe;
5205 obuf[26] = 0xff;
5206
5207 /* Generate and write actual cookie */
5208 p = obuf + 28;
5209 if( f_cookie_write( p_cookie,
5210 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5211 {
5212 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5213 }
5214
5215 *olen = p - obuf;
5216
5217 /* Go back and fill length fields */
5218 obuf[27] = (unsigned char)( *olen - 28 );
5219
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005220 (void)mbedtls_platform_put_uint24_be( &obuf[14], ( *olen - 25 ) );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005221 obuf[22] = obuf[14];
5222 obuf[23] = obuf[15];
5223 obuf[24] = obuf[16];
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005224
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005225 (void)mbedtls_platform_put_uint16_be( &obuf[11], ( *olen - 13 ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005226
5227 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5228}
5229
5230/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005231 * Handle possible client reconnect with the same UDP quadruplet
5232 * (RFC 6347 Section 4.2.8).
5233 *
5234 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5235 * that looks like a ClientHello.
5236 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005237 * - if the input looks like a ClientHello without cookies,
5238 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005239 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005240 * - if the input looks like a ClientHello with a valid cookie,
5241 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005242 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005243 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005244 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005245 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005246 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5247 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005248 */
5249static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5250{
5251 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005252 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005253
Hanno Becker87b56262019-07-10 14:37:41 +01005254 if( ssl->conf->f_cookie_write == NULL ||
5255 ssl->conf->f_cookie_check == NULL )
5256 {
5257 /* If we can't use cookies to verify reachability of the peer,
5258 * drop the record. */
5259 return( 0 );
5260 }
5261
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005262 ret = ssl_check_dtls_clihlo_cookie(
5263 ssl->conf->f_cookie_write,
5264 ssl->conf->f_cookie_check,
5265 ssl->conf->p_cookie,
5266 ssl->cli_id, ssl->cli_id_len,
5267 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005268 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005269
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005270 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5271
5272 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005273 {
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005274 int send_ret;
5275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
5276 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
5277 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08005278 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005279 * If the error is permanent we'll catch it later,
5280 * if it's not, then hopefully it'll work next time. */
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005281 send_ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
5282 MBEDTLS_SSL_DEBUG_RET( 2, "mbedtls_ssl_get_send", send_ret );
5283 (void) send_ret;
Hanno Becker87b56262019-07-10 14:37:41 +01005284 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005285 }
5286
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005287 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005288 {
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005290 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5291 {
5292 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5293 return( ret );
5294 }
5295
5296 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005297 }
5298
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005299 return( ret );
5300}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005301#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005302
Hanno Becker46483f12019-05-03 13:25:54 +01005303static int ssl_check_record_type( uint8_t record_type )
5304{
5305 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5306 record_type != MBEDTLS_SSL_MSG_ALERT &&
5307 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5308 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5309 {
5310 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5311 }
5312
5313 return( 0 );
5314}
5315
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005316/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005317 * ContentType type;
5318 * ProtocolVersion version;
5319 * uint16 epoch; // DTLS only
5320 * uint48 sequence_number; // DTLS only
5321 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005322 *
5323 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005324 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005325 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5326 *
5327 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005328 * 1. proceed with the record if this function returns 0
5329 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5330 * 3. return CLIENT_RECONNECT if this function return that value
5331 * 4. drop the whole datagram if this function returns anything else.
5332 * Point 2 is needed when the peer is resending, and we have already received
5333 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005334 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005335static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005336 unsigned char *buf,
5337 size_t len,
5338 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005339{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005340 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005341
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005342 size_t const rec_hdr_type_offset = 0;
5343 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005344
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005345 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5346 rec_hdr_type_len;
5347 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005348
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005349 size_t const rec_hdr_ctr_len = 8;
5350#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005351 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005352 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5353 rec_hdr_version_len;
5354
Hanno Beckera5a2b082019-05-15 14:03:01 +01005355#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005356 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5357 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005358 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005359#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5360#endif /* MBEDTLS_SSL_PROTO_DTLS */
5361
5362 size_t rec_hdr_len_offset; /* To be determined */
5363 size_t const rec_hdr_len_len = 2;
5364
5365 /*
5366 * Check minimum lengths for record header.
5367 */
5368
5369#if defined(MBEDTLS_SSL_PROTO_DTLS)
5370 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5371 {
5372 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5373 }
5374 MBEDTLS_SSL_TRANSPORT_ELSE
5375#endif /* MBEDTLS_SSL_PROTO_DTLS */
5376#if defined(MBEDTLS_SSL_PROTO_TLS)
5377 {
5378 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5379 }
5380#endif /* MBEDTLS_SSL_PROTO_DTLS */
5381
5382 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5383 {
5384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5385 (unsigned) len,
5386 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5387 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5388 }
5389
5390 /*
5391 * Parse and validate record content type
5392 */
5393
5394 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005395
5396 /* Check record content type */
5397#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5398 rec->cid_len = 0;
5399
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005400 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005401 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5402 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005403 {
5404 /* Shift pointers to account for record header including CID
5405 * struct {
5406 * ContentType special_type = tls12_cid;
5407 * ProtocolVersion version;
5408 * uint16 epoch;
5409 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005410 * opaque cid[cid_length]; // Additional field compared to
5411 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005412 * uint16 length;
5413 * opaque enc_content[DTLSCiphertext.length];
5414 * } DTLSCiphertext;
5415 */
5416
5417 /* So far, we only support static CID lengths
5418 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005419 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5420 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005421
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005422 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005423 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5425 (unsigned) len,
5426 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005427 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005428 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005429
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005430 /* configured CID len is guaranteed at most 255, see
5431 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5432 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005433 /* Not using more secure mbedtls_platform_memcpy as cid is public */
5434 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005435 }
5436 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005437#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005438 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005439 if( ssl_check_record_type( rec->type ) )
5440 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5442 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005443 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5444 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005445 }
5446
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005447 /*
5448 * Parse and validate record version
5449 */
5450
Hanno Becker8061c6e2019-07-26 08:07:03 +01005451 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5452 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005453 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5454 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005455 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005456
Hanno Becker2881d802019-05-22 14:44:53 +01005457 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5460 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005461 }
5462
Hanno Becker7bcf2b52019-07-26 09:02:40 +01005463 if( mbedtls_ssl_ver_gt( minor_ver,
5464 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5467 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005468 }
5469
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005470 /*
5471 * Parse/Copy record sequence number.
5472 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005473
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005474#if defined(MBEDTLS_SSL_PROTO_DTLS)
5475 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5476 {
5477 /* Copy explicit record sequence number from input buffer. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005478 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5479 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005480 rec_hdr_ctr_len );
5481 }
5482 MBEDTLS_SSL_TRANSPORT_ELSE
5483#endif /* MBEDTLS_SSL_PROTO_DTLS */
5484#if defined(MBEDTLS_SSL_PROTO_TLS)
5485 {
5486 /* Copy implicit record sequence number from SSL context structure. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005487 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5488 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005489 }
5490#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005491
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005492 /*
5493 * Parse record length.
5494 */
5495
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005496 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005497 rec->data_len = mbedtls_platform_get_uint16_be( &buf[rec_hdr_len_offset] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005498 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5499
Hanno Becker8b09b732019-05-08 12:03:28 +01005500 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005501 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005502 rec->type,
5503 major_ver, minor_ver, rec->data_len ) );
5504
5505 rec->buf = buf;
5506 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005507
Hanno Beckerec014082019-07-26 08:20:27 +01005508 if( rec->data_len == 0 )
5509 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5510
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005511 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005512 * DTLS-related tests.
5513 * Check epoch before checking length constraint because
5514 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5515 * message gets duplicated before the corresponding Finished message,
5516 * the second ChangeCipherSpec should be discarded because it belongs
5517 * to an old epoch, but not because its length is shorter than
5518 * the minimum record length for packets using the new record transform.
5519 * Note that these two kinds of failures are handled differently,
5520 * as an unexpected record is silently skipped but an invalid
5521 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005522 */
5523#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005524 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005525 {
Arto Kinnunena3fa06e2019-09-09 12:22:51 +03005526 rec_epoch = (uint32_t)mbedtls_platform_get_uint16_be( rec->ctr );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005527
Hanno Beckere0452772019-07-10 17:12:07 +01005528 /* Check that the datagram is large enough to contain a record
5529 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005530 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005531 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5533 (unsigned) len,
5534 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005535 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5536 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005537
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005538 /* Records from other, non-matching epochs are silently discarded.
5539 * (The case of same-port Client reconnects must be considered in
5540 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005541 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005542 {
5543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5544 "expected %d, received %d",
5545 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005546
5547 /* Records from the next epoch are considered for buffering
5548 * (concretely: early Finished messages). */
5549 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5550 {
5551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5552 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5553 }
5554
Hanno Becker87b56262019-07-10 14:37:41 +01005555 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005556 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005557#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005558 /* For records from the correct epoch, check whether their
5559 * sequence number has been seen before. */
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005560 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
5561 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005562 {
5563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5564 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5565 }
5566#endif
5567 }
5568#endif /* MBEDTLS_SSL_PROTO_DTLS */
5569
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005570 return( 0 );
5571}
Paul Bakker5121ce52009-01-03 21:22:43 +00005572
Hanno Becker87b56262019-07-10 14:37:41 +01005573
5574#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5575static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5576{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005577 unsigned int rec_epoch = (unsigned int)
5578 mbedtls_platform_get_uint16_be( &ssl->in_ctr[0] );
Hanno Becker87b56262019-07-10 14:37:41 +01005579
5580 /*
5581 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5582 * access the first byte of record content (handshake type), as we
5583 * have an active transform (possibly iv_len != 0), so use the
5584 * fact that the record header len is 13 instead.
5585 */
5586 if( rec_epoch == 0 &&
5587 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5588 MBEDTLS_SSL_IS_SERVER &&
5589 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5590 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5591 ssl->in_left > 13 &&
5592 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5593 {
5594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5595 "from the same port" ) );
5596 return( ssl_handle_possible_reconnect( ssl ) );
5597 }
5598
5599 return( 0 );
5600}
5601#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5602
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005603/*
Manuel Pégourié-Gonnardae48d862020-01-03 12:18:49 +01005604 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005605 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005606static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5607 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005608{
5609 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005611 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005612 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5615 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005619 ret = mbedtls_ssl_hw_record_read( ssl );
5620 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5623 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005624 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005625
5626 if( ret == 0 )
5627 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005628 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005630 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005631 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005632 unsigned char const old_msg_type = rec->type;
5633
Hanno Becker611a83b2018-01-03 14:27:32 +00005634 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005635 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005637 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005638
Hanno Beckera5a2b082019-05-15 14:03:01 +01005639#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005640 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005641 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5642 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005643 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005644 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005645 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005646 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005647#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005648
Paul Bakker5121ce52009-01-03 21:22:43 +00005649 return( ret );
5650 }
5651
Hanno Becker106f3da2019-07-12 09:35:58 +01005652 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005653 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005654 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005655 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005656 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005657
Paul Bakker5121ce52009-01-03 21:22:43 +00005658 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005659 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005660
Hanno Beckera5a2b082019-05-15 14:03:01 +01005661#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005662 /* We have already checked the record content type
5663 * in ssl_parse_record_header(), failing or silently
5664 * dropping the record in the case of an unknown type.
5665 *
5666 * Since with the use of CIDs, the record content type
5667 * might change during decryption, re-check the record
5668 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005669 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005670 {
5671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5672 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5673 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005674#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005675
Hanno Becker106f3da2019-07-12 09:35:58 +01005676 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005677 {
5678#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005679 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005680 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005681 {
5682 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5684 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5685 }
5686#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5687
5688 ssl->nb_zero++;
5689
5690 /*
5691 * Three or more empty messages may be a DoS attack
5692 * (excessive CPU consumption).
5693 */
5694 if( ssl->nb_zero > 3 )
5695 {
5696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005697 "messages, possible DoS attack" ) );
5698 /* Treat the records as if they were not properly authenticated,
5699 * thereby failing the connection if we see more than allowed
5700 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005701 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5702 }
5703 }
5704 else
5705 ssl->nb_zero = 0;
5706
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005707 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5708#if defined(MBEDTLS_SSL_PROTO_TLS)
5709 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005710 {
5711 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005712 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005713 if( ++ssl->in_ctr[i - 1] != 0 )
5714 break;
5715
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005716 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005717 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005718 {
5719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5720 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5721 }
5722 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005723#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005724
Paul Bakker5121ce52009-01-03 21:22:43 +00005725 }
5726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005728 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005730 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005731 }
5732#endif
5733
Hanno Beckerf0242852019-07-09 17:30:02 +01005734 /* Check actual (decrypted) record content length against
5735 * configured maximum. */
5736 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5737 {
5738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5739 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5740 }
5741
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005742 return( 0 );
5743}
5744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005745static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005746
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005747/*
5748 * Read a record.
5749 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005750 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5751 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5752 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005753 */
Hanno Becker1097b342018-08-15 14:09:41 +01005754
5755/* Helper functions for mbedtls_ssl_read_record(). */
5756static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005757static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5758static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005759
Hanno Becker327c93b2018-08-15 13:56:18 +01005760int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005761 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005762{
5763 int ret;
5764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005766
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005767 if( ssl->keep_current_message == 0 )
5768 {
5769 do {
Simon Butcher99000142016-10-13 17:21:01 +01005770
Hanno Becker26994592018-08-15 14:14:59 +01005771 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005772 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005773 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005774
Hanno Beckere74d5562018-08-15 14:26:08 +01005775 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005776 {
Hanno Becker40f50842018-08-15 14:48:01 +01005777#if defined(MBEDTLS_SSL_PROTO_DTLS)
5778 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005779
Hanno Becker40f50842018-08-15 14:48:01 +01005780 /* We only check for buffered messages if the
5781 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005782 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005783 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005784 {
Hanno Becker40f50842018-08-15 14:48:01 +01005785 if( ssl_load_buffered_message( ssl ) == 0 )
5786 have_buffered = 1;
5787 }
5788
5789 if( have_buffered == 0 )
5790#endif /* MBEDTLS_SSL_PROTO_DTLS */
5791 {
5792 ret = ssl_get_next_record( ssl );
5793 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5794 continue;
5795
5796 if( ret != 0 )
5797 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005798 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005799 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005800 return( ret );
5801 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005802 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005803 }
5804
5805 ret = mbedtls_ssl_handle_message_type( ssl );
5806
Hanno Becker40f50842018-08-15 14:48:01 +01005807#if defined(MBEDTLS_SSL_PROTO_DTLS)
5808 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5809 {
5810 /* Buffer future message */
5811 ret = ssl_buffer_message( ssl );
5812 if( ret != 0 )
5813 return( ret );
5814
5815 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5816 }
5817#endif /* MBEDTLS_SSL_PROTO_DTLS */
5818
Hanno Becker90333da2017-10-10 11:27:13 +01005819 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5820 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005821
5822 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005823 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005824 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005825 return( ret );
5826 }
5827
Hanno Becker327c93b2018-08-15 13:56:18 +01005828 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005829 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005830 {
5831 mbedtls_ssl_update_handshake_status( ssl );
5832 }
Simon Butcher99000142016-10-13 17:21:01 +01005833 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005834 else
Simon Butcher99000142016-10-13 17:21:01 +01005835 {
Hanno Becker02f59072018-08-15 14:00:24 +01005836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005837 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005838 }
5839
5840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5841
5842 return( 0 );
5843}
5844
Hanno Becker40f50842018-08-15 14:48:01 +01005845#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005846static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005847{
Hanno Becker40f50842018-08-15 14:48:01 +01005848 if( ssl->in_left > ssl->next_record_offset )
5849 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005850
Hanno Becker40f50842018-08-15 14:48:01 +01005851 return( 0 );
5852}
5853
5854static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5855{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005856 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005857 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005858 int ret = 0;
5859
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005860 if( hs == NULL )
5861 return( -1 );
5862
Hanno Beckere00ae372018-08-20 09:39:42 +01005863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5864
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005865 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5866 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5867 {
5868 /* Check if we have seen a ChangeCipherSpec before.
5869 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005870 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005871 {
5872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5873 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005874 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005875 }
5876
Hanno Becker39b8bc92018-08-28 17:17:13 +01005877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005878 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5879 ssl->in_msglen = 1;
5880 ssl->in_msg[0] = 1;
5881
5882 /* As long as they are equal, the exact value doesn't matter. */
5883 ssl->in_left = 0;
5884 ssl->next_record_offset = 0;
5885
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005886 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005887 goto exit;
5888 }
Hanno Becker37f95322018-08-16 13:55:32 +01005889
Hanno Beckerb8f50142018-08-28 10:01:34 +01005890#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005891 /* Debug only */
5892 {
5893 unsigned offset;
5894 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5895 {
5896 hs_buf = &hs->buffering.hs[offset];
5897 if( hs_buf->is_valid == 1 )
5898 {
5899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5900 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005901 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005902 }
5903 }
5904 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005905#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005906
5907 /* Check if we have buffered and/or fully reassembled the
5908 * next handshake message. */
5909 hs_buf = &hs->buffering.hs[0];
5910 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5911 {
5912 /* Synthesize a record containing the buffered HS message. */
Arto Kinnunen84eeb4f2019-09-10 10:32:30 +03005913 size_t msg_len = mbedtls_platform_get_uint24_be( &hs_buf->data[1] );
Hanno Becker37f95322018-08-16 13:55:32 +01005914
5915 /* Double-check that we haven't accidentally buffered
5916 * a message that doesn't fit into the input buffer. */
5917 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5918 {
5919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5920 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5921 }
5922
5923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5924 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5925 hs_buf->data, msg_len + 12 );
5926
5927 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5928 ssl->in_hslen = msg_len + 12;
5929 ssl->in_msglen = msg_len + 12;
Teppo Järvelin91d79382019-10-02 09:09:31 +03005930 mbedtls_platform_memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
Hanno Becker37f95322018-08-16 13:55:32 +01005931
5932 ret = 0;
5933 goto exit;
5934 }
5935 else
5936 {
5937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5938 hs->in_msg_seq ) );
5939 }
5940
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005941 ret = -1;
5942
5943exit:
5944
5945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5946 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005947}
5948
Hanno Beckera02b0b42018-08-21 17:20:27 +01005949static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5950 size_t desired )
5951{
5952 int offset;
5953 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5955 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005956
Hanno Becker01315ea2018-08-21 17:22:17 +01005957 /* Get rid of future records epoch first, if such exist. */
5958 ssl_free_buffered_record( ssl );
5959
5960 /* Check if we have enough space available now. */
5961 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5962 hs->buffering.total_bytes_buffered ) )
5963 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005965 return( 0 );
5966 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005967
Hanno Becker4f432ad2018-08-28 10:02:32 +01005968 /* We don't have enough space to buffer the next expected handshake
5969 * message. Remove buffers used for future messages to gain space,
5970 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005971 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5972 offset >= 0; offset-- )
5973 {
5974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5975 offset ) );
5976
Hanno Beckerb309b922018-08-23 13:18:05 +01005977 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005978
5979 /* Check if we have enough space available now. */
5980 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5981 hs->buffering.total_bytes_buffered ) )
5982 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005984 return( 0 );
5985 }
5986 }
5987
5988 return( -1 );
5989}
5990
Hanno Becker40f50842018-08-15 14:48:01 +01005991static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5992{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005993 int ret = 0;
5994 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5995
5996 if( hs == NULL )
5997 return( 0 );
5998
5999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
6000
6001 switch( ssl->in_msgtype )
6002 {
6003 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
6004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01006005
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01006006 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006007 break;
6008
6009 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01006010 {
6011 unsigned recv_msg_seq_offset;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03006012 unsigned recv_msg_seq = (unsigned)
6013 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006014
Hanno Becker37f95322018-08-16 13:55:32 +01006015 mbedtls_ssl_hs_buffer *hs_buf;
6016 size_t msg_len = ssl->in_hslen - 12;
6017
6018 /* We should never receive an old handshake
6019 * message - double-check nonetheless. */
6020 if( recv_msg_seq < ssl->handshake->in_msg_seq )
6021 {
6022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6023 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6024 }
6025
6026 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
6027 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
6028 {
6029 /* Silently ignore -- message too far in the future */
6030 MBEDTLS_SSL_DEBUG_MSG( 2,
6031 ( "Ignore future HS message with sequence number %u, "
6032 "buffering window %u - %u",
6033 recv_msg_seq, ssl->handshake->in_msg_seq,
6034 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
6035
6036 goto exit;
6037 }
6038
6039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
6040 recv_msg_seq, recv_msg_seq_offset ) );
6041
6042 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
6043
6044 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01006045 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01006046 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006047 size_t reassembly_buf_sz;
6048
Hanno Becker37f95322018-08-16 13:55:32 +01006049 hs_buf->is_fragmented =
6050 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
6051
6052 /* We copy the message back into the input buffer
6053 * after reassembly, so check that it's not too large.
6054 * This is an implementation-specific limitation
6055 * and not one from the standard, hence it is not
6056 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01006057 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01006058 {
6059 /* Ignore message */
6060 goto exit;
6061 }
6062
Hanno Beckere0b150f2018-08-21 15:51:03 +01006063 /* Check if we have enough space to buffer the message. */
6064 if( hs->buffering.total_bytes_buffered >
6065 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
6066 {
6067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6068 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6069 }
6070
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006071 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
6072 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01006073
6074 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
6075 hs->buffering.total_bytes_buffered ) )
6076 {
6077 if( recv_msg_seq_offset > 0 )
6078 {
6079 /* If we can't buffer a future message because
6080 * of space limitations -- ignore. */
6081 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",
6082 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6083 (unsigned) hs->buffering.total_bytes_buffered ) );
6084 goto exit;
6085 }
Hanno Beckere1801392018-08-21 16:51:05 +01006086 else
6087 {
6088 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",
6089 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6090 (unsigned) hs->buffering.total_bytes_buffered ) );
6091 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006092
Hanno Beckera02b0b42018-08-21 17:20:27 +01006093 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006094 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01006095 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",
6096 (unsigned) msg_len,
6097 (unsigned) reassembly_buf_sz,
6098 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01006099 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006100 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6101 goto exit;
6102 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006103 }
6104
6105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
6106 msg_len ) );
6107
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006108 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
6109 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01006110 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01006111 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01006112 goto exit;
6113 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006114 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006115
6116 /* Prepare final header: copy msg_type, length and message_seq,
6117 * then add standardised fragment_offset and fragment_length */
Teppo Järvelin91d79382019-10-02 09:09:31 +03006118 mbedtls_platform_memcpy( hs_buf->data, ssl->in_msg, 6 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02006119 mbedtls_platform_memset( hs_buf->data + 6, 0, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006120 mbedtls_platform_memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
Hanno Becker37f95322018-08-16 13:55:32 +01006121
6122 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01006123
6124 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006125 }
6126 else
6127 {
6128 /* Make sure msg_type and length are consistent */
Teppo Järvelin0efac532019-10-04 13:21:08 +03006129 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 +01006130 {
6131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6132 /* Ignore */
6133 goto exit;
6134 }
6135 }
6136
Hanno Becker4422bbb2018-08-20 09:40:19 +01006137 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006138 {
6139 size_t frag_len, frag_off;
6140 unsigned char * const msg = hs_buf->data + 12;
6141
6142 /*
6143 * Check and copy current fragment
6144 */
6145
6146 /* Validation of header fields already done in
6147 * mbedtls_ssl_prepare_handshake_record(). */
6148 frag_off = ssl_get_hs_frag_off( ssl );
6149 frag_len = ssl_get_hs_frag_len( ssl );
6150
6151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6152 frag_off, frag_len ) );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006153 mbedtls_platform_memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
Hanno Becker37f95322018-08-16 13:55:32 +01006154
6155 if( hs_buf->is_fragmented )
6156 {
6157 unsigned char * const bitmask = msg + msg_len;
6158 ssl_bitmask_set( bitmask, frag_off, frag_len );
6159 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6160 msg_len ) == 0 );
6161 }
6162 else
6163 {
6164 hs_buf->is_complete = 1;
6165 }
6166
6167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6168 hs_buf->is_complete ? "" : "not yet " ) );
6169 }
6170
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006171 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006172 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006173
6174 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006175 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006176 break;
6177 }
6178
6179exit:
6180
6181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6182 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006183}
6184#endif /* MBEDTLS_SSL_PROTO_DTLS */
6185
Hanno Becker1097b342018-08-15 14:09:41 +01006186static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006187{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006188 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006189 * Consume last content-layer message and potentially
6190 * update in_msglen which keeps track of the contents'
6191 * consumption state.
6192 *
6193 * (1) Handshake messages:
6194 * Remove last handshake message, move content
6195 * and adapt in_msglen.
6196 *
6197 * (2) Alert messages:
6198 * Consume whole record content, in_msglen = 0.
6199 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006200 * (3) Change cipher spec:
6201 * Consume whole record content, in_msglen = 0.
6202 *
6203 * (4) Application data:
6204 * Don't do anything - the record layer provides
6205 * the application data as a stream transport
6206 * and consumes through mbedtls_ssl_read only.
6207 *
6208 */
6209
6210 /* Case (1): Handshake messages */
6211 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006212 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006213 /* Hard assertion to be sure that no application data
6214 * is in flight, as corrupting ssl->in_msglen during
6215 * ssl->in_offt != NULL is fatal. */
6216 if( ssl->in_offt != NULL )
6217 {
6218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6219 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6220 }
6221
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006222 /*
6223 * Get next Handshake message in the current record
6224 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006225
Hanno Becker4a810fb2017-05-24 16:27:30 +01006226 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006227 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006228 * current handshake content: If DTLS handshake
6229 * fragmentation is used, that's the fragment
6230 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006231 * size here is faulty and should be changed at
6232 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006233 * (2) While it doesn't seem to cause problems, one
6234 * has to be very careful not to assume that in_hslen
6235 * is always <= in_msglen in a sensible communication.
6236 * Again, it's wrong for DTLS handshake fragmentation.
6237 * The following check is therefore mandatory, and
6238 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006239 * Additionally, ssl->in_hslen might be arbitrarily out of
6240 * bounds after handling a DTLS message with an unexpected
6241 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006242 */
6243 if( ssl->in_hslen < ssl->in_msglen )
6244 {
6245 ssl->in_msglen -= ssl->in_hslen;
6246 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6247 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006248
Hanno Becker4a810fb2017-05-24 16:27:30 +01006249 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6250 ssl->in_msg, ssl->in_msglen );
6251 }
6252 else
6253 {
6254 ssl->in_msglen = 0;
6255 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006256
Hanno Becker4a810fb2017-05-24 16:27:30 +01006257 ssl->in_hslen = 0;
6258 }
6259 /* Case (4): Application data */
6260 else if( ssl->in_offt != NULL )
6261 {
6262 return( 0 );
6263 }
6264 /* Everything else (CCS & Alerts) */
6265 else
6266 {
6267 ssl->in_msglen = 0;
6268 }
6269
Hanno Becker1097b342018-08-15 14:09:41 +01006270 return( 0 );
6271}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006272
Hanno Beckere74d5562018-08-15 14:26:08 +01006273static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6274{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006275 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006276 return( 1 );
6277
6278 return( 0 );
6279}
6280
Hanno Becker5f066e72018-08-16 14:56:31 +01006281#if defined(MBEDTLS_SSL_PROTO_DTLS)
6282
6283static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6284{
6285 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6286 if( hs == NULL )
6287 return;
6288
Hanno Becker01315ea2018-08-21 17:22:17 +01006289 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006290 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006291 hs->buffering.total_bytes_buffered -=
6292 hs->buffering.future_record.len;
6293
6294 mbedtls_free( hs->buffering.future_record.data );
6295 hs->buffering.future_record.data = NULL;
6296 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006297}
6298
6299static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6300{
6301 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6302 unsigned char * rec;
6303 size_t rec_len;
6304 unsigned rec_epoch;
6305
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006306 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006307 return( 0 );
6308
6309 if( hs == NULL )
6310 return( 0 );
6311
Hanno Becker5f066e72018-08-16 14:56:31 +01006312 rec = hs->buffering.future_record.data;
6313 rec_len = hs->buffering.future_record.len;
6314 rec_epoch = hs->buffering.future_record.epoch;
6315
6316 if( rec == NULL )
6317 return( 0 );
6318
Hanno Becker4cb782d2018-08-20 11:19:05 +01006319 /* Only consider loading future records if the
6320 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006321 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006322 return( 0 );
6323
Hanno Becker5f066e72018-08-16 14:56:31 +01006324 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6325
6326 if( rec_epoch != ssl->in_epoch )
6327 {
6328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6329 goto exit;
6330 }
6331
6332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6333
6334 /* Double-check that the record is not too large */
6335 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6336 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6337 {
6338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6339 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6340 }
6341
Teppo Järvelin91d79382019-10-02 09:09:31 +03006342 mbedtls_platform_memcpy( ssl->in_hdr, rec, rec_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006343 ssl->in_left = rec_len;
6344 ssl->next_record_offset = 0;
6345
6346 ssl_free_buffered_record( ssl );
6347
6348exit:
6349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6350 return( 0 );
6351}
6352
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006353static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6354 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006355{
6356 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006357
6358 /* Don't buffer future records outside handshakes. */
6359 if( hs == NULL )
6360 return( 0 );
6361
6362 /* Only buffer handshake records (we are only interested
6363 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006364 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006365 return( 0 );
6366
6367 /* Don't buffer more than one future epoch record. */
6368 if( hs->buffering.future_record.data != NULL )
6369 return( 0 );
6370
Hanno Becker01315ea2018-08-21 17:22:17 +01006371 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006372 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006373 hs->buffering.total_bytes_buffered ) )
6374 {
6375 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 +01006376 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006377 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006378 return( 0 );
6379 }
6380
Hanno Becker5f066e72018-08-16 14:56:31 +01006381 /* Buffer record */
6382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6383 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006384 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006385
6386 /* ssl_parse_record_header() only considers records
6387 * of the next epoch as candidates for buffering. */
6388 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006389 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006390
6391 hs->buffering.future_record.data =
6392 mbedtls_calloc( 1, hs->buffering.future_record.len );
6393 if( hs->buffering.future_record.data == NULL )
6394 {
6395 /* If we run out of RAM trying to buffer a
6396 * record from the next epoch, just ignore. */
6397 return( 0 );
6398 }
6399
Teppo Järvelin91d79382019-10-02 09:09:31 +03006400 mbedtls_platform_memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006401
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006402 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006403 return( 0 );
6404}
6405
6406#endif /* MBEDTLS_SSL_PROTO_DTLS */
6407
Hanno Beckere74d5562018-08-15 14:26:08 +01006408static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006409{
6410 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006411 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006412
Hanno Becker5f066e72018-08-16 14:56:31 +01006413#if defined(MBEDTLS_SSL_PROTO_DTLS)
6414 /* We might have buffered a future record; if so,
6415 * and if the epoch matches now, load it.
6416 * On success, this call will set ssl->in_left to
6417 * the length of the buffered record, so that
6418 * the calls to ssl_fetch_input() below will
6419 * essentially be no-ops. */
6420 ret = ssl_load_buffered_record( ssl );
6421 if( ret != 0 )
6422 return( ret );
6423#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006424
Hanno Becker8b09b732019-05-08 12:03:28 +01006425 /* Ensure that we have enough space available for the default form
6426 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6427 * with no space for CIDs counted in). */
6428 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6429 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006431 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006432 return( ret );
6433 }
6434
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006435 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6436 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006438#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006439 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006440 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006441 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6442 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006443 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006444 if( ret != 0 )
6445 return( ret );
6446
6447 /* Fall through to handling of unexpected records */
6448 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6449 }
6450
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006451 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6452 {
Hanno Becker87b56262019-07-10 14:37:41 +01006453#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006454 /* Reset in pointers to default state for TLS/DTLS records,
6455 * assuming no CID and no offset between record content and
6456 * record plaintext. */
6457 ssl_update_in_pointers( ssl );
6458
Hanno Becker69412452019-07-12 08:33:49 +01006459 /* Setup internal message pointers from record structure. */
6460 ssl->in_msgtype = rec.type;
6461#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6462 ssl->in_len = ssl->in_cid + rec.cid_len;
6463#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006464 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006465 ssl->in_msglen = rec.data_len;
6466
Hanno Becker87b56262019-07-10 14:37:41 +01006467 ret = ssl_check_client_reconnect( ssl );
6468 if( ret != 0 )
6469 return( ret );
6470#endif
6471
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006472 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006473 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006474
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6476 "(header)" ) );
6477 }
6478 else
6479 {
6480 /* Skip invalid record and the rest of the datagram */
6481 ssl->next_record_offset = 0;
6482 ssl->in_left = 0;
6483
6484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6485 "(header)" ) );
6486 }
6487
6488 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006489 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006490 }
Hanno Becker87b56262019-07-10 14:37:41 +01006491 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006492#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006493 {
6494 return( ret );
6495 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006496 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006498#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006499 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006500 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006501 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006502 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006503 if( ssl->next_record_offset < ssl->in_left )
6504 {
6505 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6506 }
6507 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006508 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006509#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006510#if defined(MBEDTLS_SSL_PROTO_TLS)
6511 {
Hanno Beckere0452772019-07-10 17:12:07 +01006512 /*
6513 * Fetch record contents from underlying transport.
6514 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006515 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006516 if( ret != 0 )
6517 {
6518 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6519 return( ret );
6520 }
6521
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006522 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006523 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006524#endif /* MBEDTLS_SSL_PROTO_TLS */
6525
6526 /*
6527 * Decrypt record contents.
6528 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006529
Hanno Beckera89610a2019-07-11 13:07:45 +01006530 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006532#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006533 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006534 {
6535 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006536 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006537 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006538 /* Except when waiting for Finished as a bad mac here
6539 * probably means something went wrong in the handshake
6540 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6541 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6542 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6543 {
6544#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6545 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6546 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006547 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006548 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6549 }
6550#endif
6551 return( ret );
6552 }
6553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006555 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6556 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6559 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006560 }
6561#endif
6562
Hanno Becker4a810fb2017-05-24 16:27:30 +01006563 /* As above, invalid records cause
6564 * dismissal of the whole datagram. */
6565
6566 ssl->next_record_offset = 0;
6567 ssl->in_left = 0;
6568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006570 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006571 }
6572
6573 return( ret );
6574 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006575 MBEDTLS_SSL_TRANSPORT_ELSE
6576#endif /* MBEDTLS_SSL_PROTO_DTLS */
6577#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006578 {
6579 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006580#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6581 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006582 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006583 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006584 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006585 }
6586#endif
6587 return( ret );
6588 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006589#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006590 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006591
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006592
6593 /* Reset in pointers to default state for TLS/DTLS records,
6594 * assuming no CID and no offset between record content and
6595 * record plaintext. */
6596 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006597#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6598 ssl->in_len = ssl->in_cid + rec.cid_len;
6599#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006600 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006601
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006602 /* The record content type may change during decryption,
6603 * so re-read it. */
6604 ssl->in_msgtype = rec.type;
6605 /* Also update the input buffer, because unfortunately
6606 * the server-side ssl_parse_client_hello() reparses the
6607 * record header when receiving a ClientHello initiating
6608 * a renegotiation. */
6609 ssl->in_hdr[0] = rec.type;
6610 ssl->in_msg = rec.buf + rec.data_offset;
6611 ssl->in_msglen = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006612 (void)mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len );
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006613
Manuel Pégourié-Gonnardae48d862020-01-03 12:18:49 +01006614#if defined(MBEDTLS_ZLIB_SUPPORT)
6615 if( ssl->transform_in != NULL &&
6616 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
6617 {
6618 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
6619 {
6620 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
6621 return( ret );
6622 }
6623
6624 /* Check actual (decompress) record content length against
6625 * configured maximum. */
6626 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
6627 {
6628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
6629 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6630 }
6631 }
6632#endif /* MBEDTLS_ZLIB_SUPPORT */
6633
Simon Butcher99000142016-10-13 17:21:01 +01006634 return( 0 );
6635}
6636
6637int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6638{
6639 int ret;
6640
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006641 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006642 * Handle particular types of records
6643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006645 {
Simon Butcher99000142016-10-13 17:21:01 +01006646 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6647 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006648 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006649 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006650 }
6651
Hanno Beckere678eaa2018-08-21 14:57:46 +01006652 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006653 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006654 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006655 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6657 ssl->in_msglen ) );
6658 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006659 }
6660
Hanno Beckere678eaa2018-08-21 14:57:46 +01006661 if( ssl->in_msg[0] != 1 )
6662 {
6663 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6664 ssl->in_msg[0] ) );
6665 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6666 }
6667
6668#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006669 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006670 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6671 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6672 {
6673 if( ssl->handshake == NULL )
6674 {
6675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6676 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6677 }
6678
6679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6680 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6681 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006682#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006683 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006685 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006686 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006687 if( ssl->in_msglen != 2 )
6688 {
6689 /* Note: Standard allows for more than one 2 byte alert
6690 to be packed in a single message, but Mbed TLS doesn't
6691 currently support this. */
6692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6693 ssl->in_msglen ) );
6694 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6695 }
6696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006698 ssl->in_msg[0], ssl->in_msg[1] ) );
6699
6700 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006701 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006703 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006706 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006708 }
6709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6711 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6714 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006715 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006716
6717#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6718 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6719 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6720 {
Hanno Becker90333da2017-10-10 11:27:13 +01006721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006722 /* Will be handled when trying to parse ServerHello */
6723 return( 0 );
6724 }
6725#endif
6726
6727#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006728 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006729 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6730 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006731 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6732 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6733 {
6734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6735 /* Will be handled in mbedtls_ssl_parse_certificate() */
6736 return( 0 );
6737 }
6738#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6739
6740 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006741 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006742 }
6743
Hanno Beckerc76c6192017-06-06 10:03:17 +01006744#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006745 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006746 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006747 /* Drop unexpected ApplicationData records,
6748 * except at the beginning of renegotiations */
6749 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6750 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6751#if defined(MBEDTLS_SSL_RENEGOTIATION)
6752 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6753 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006754#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006755 )
6756 {
6757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6758 return( MBEDTLS_ERR_SSL_NON_FATAL );
6759 }
6760
6761 if( ssl->handshake != NULL &&
6762 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6763 {
6764 ssl_handshake_wrapup_free_hs_transform( ssl );
6765 }
6766 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006767#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006768
Paul Bakker5121ce52009-01-03 21:22:43 +00006769 return( 0 );
6770}
6771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006773{
6774 int ret;
6775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6777 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6778 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006779 {
6780 return( ret );
6781 }
6782
6783 return( 0 );
6784}
6785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006787 unsigned char level,
6788 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006789{
6790 int ret;
6791
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006792 if( ssl == NULL || ssl->conf == NULL )
6793 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006796 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006798 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006799 ssl->out_msglen = 2;
6800 ssl->out_msg[0] = level;
6801 ssl->out_msg[1] = message;
6802
Hanno Becker67bc7c32018-08-06 11:33:50 +01006803 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006805 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006806 return( ret );
6807 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006809
6810 return( 0 );
6811}
6812
Hanno Becker17572472019-02-08 07:19:04 +00006813#if defined(MBEDTLS_X509_CRT_PARSE_C)
6814static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6815{
6816#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6817 if( session->peer_cert != NULL )
6818 {
6819 mbedtls_x509_crt_free( session->peer_cert );
6820 mbedtls_free( session->peer_cert );
6821 session->peer_cert = NULL;
6822 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006823#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006824 if( session->peer_cert_digest != NULL )
6825 {
6826 /* Zeroization is not necessary. */
6827 mbedtls_free( session->peer_cert_digest );
6828 session->peer_cert_digest = NULL;
6829 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6830 session->peer_cert_digest_len = 0;
6831 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006832#else
6833 ((void) session);
6834#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006835}
6836#endif /* MBEDTLS_X509_CRT_PARSE_C */
6837
Paul Bakker5121ce52009-01-03 21:22:43 +00006838/*
6839 * Handshake functions
6840 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006841#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006842/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006844{
Hanno Beckerdf645962019-06-26 13:02:22 +01006845 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6846 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006849
Hanno Becker5097cba2019-02-05 13:36:46 +00006850 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006853 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6854 {
6855 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6856 }
6857 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6858 {
6859 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6860 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006861 else
6862 {
6863 ssl->state = MBEDTLS_SSL_INVALID;
6864 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006865 return( 0 );
6866 }
6867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6869 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006870}
6871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006872int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006873{
Hanno Beckerdf645962019-06-26 13:02:22 +01006874 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6875 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006878
Hanno Becker5097cba2019-02-05 13:36:46 +00006879 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006882 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6883 {
6884 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6885 }
6886 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6887 {
6888 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6889 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006890 else
6891 {
6892 ssl->state = MBEDTLS_SSL_INVALID;
6893 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006894 return( 0 );
6895 }
6896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6898 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006899}
Gilles Peskinef9828522017-05-03 12:28:43 +02006900
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006901#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006902/* Some certificate support -> implement write and parse */
6903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006904int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006905{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006906 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006907 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006909 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6910 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006913
Hanno Becker5097cba2019-02-05 13:36:46 +00006914 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006917 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6918 {
6919 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6920 }
6921 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6922 {
6923 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6924 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006925 else
6926 {
6927 ssl->state = MBEDTLS_SSL_INVALID;
6928 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006929 return( 0 );
6930 }
6931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006932#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006933 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6934 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006935 {
6936 if( ssl->client_auth == 0 )
6937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006939 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6940 {
6941 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6942 }
6943 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6944 {
6945 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6946 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006947 else
6948 {
6949 ssl->state = MBEDTLS_SSL_INVALID;
6950 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006951 return( 0 );
6952 }
6953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006954#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006955 /*
6956 * If using SSLv3 and got no cert, send an Alert message
6957 * (otherwise an empty Certificate message will be sent).
6958 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006960 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006961 {
6962 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006963 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6964 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6965 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006968 goto write_msg;
6969 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006971 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972#endif /* MBEDTLS_SSL_CLI_C */
6973#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006974 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006976 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6979 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006980 }
6981 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006982#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006985
6986 /*
6987 * 0 . 0 handshake type
6988 * 1 . 3 handshake length
6989 * 4 . 6 length of all certs
6990 * 7 . 9 length of cert. 1
6991 * 10 . n-1 peer certificate
6992 * n . n+2 length of cert. 2
6993 * n+3 . ... upper level cert, etc.
6994 */
6995 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006996 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006997
Paul Bakker29087132010-03-21 21:03:34 +00006998 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006999 {
7000 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10007001 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00007002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10007004 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007005 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007006 }
7007
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03007008 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007009
Teppo Järvelin91d79382019-10-02 09:09:31 +03007010 i += 3; mbedtls_platform_memcpy( ssl->out_msg + i, crt->raw.p, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007011 i += n; crt = crt->next;
7012 }
7013
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03007014 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007015
7016 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007017 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7018 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007019
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007020#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007021write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007022#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007023
Jarno Lamsa2b205162019-11-12 15:36:21 +02007024 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7025 {
7026 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7027 }
7028 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7029 {
7030 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7031 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007032 else
7033 {
7034 ssl->state = MBEDTLS_SSL_INVALID;
7035 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007036
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007037 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007038 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007039 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007040 return( ret );
7041 }
7042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007044
Paul Bakkered27a042013-04-18 22:46:23 +02007045 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007046}
7047
Hanno Becker285ff0c2019-01-31 07:44:03 +00007048#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00007049
7050#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46: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 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7056
7057 if( peer_crt == NULL )
7058 return( -1 );
7059
7060 if( peer_crt->raw.len != crt_buf_len )
7061 return( -1 );
7062
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007063 return( mbedtls_platform_memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007064}
Hanno Becker5882dd02019-06-06 16:25:57 +01007065#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00007066static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7067 unsigned char *crt_buf,
7068 size_t crt_buf_len )
7069{
7070 int ret;
7071 unsigned char const * const peer_cert_digest =
7072 ssl->session->peer_cert_digest;
7073 mbedtls_md_type_t const peer_cert_digest_type =
7074 ssl->session->peer_cert_digest_type;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007075 mbedtls_md_handle_t digest_info =
Hanno Beckerdf759382019-02-05 17:02:46 +00007076 mbedtls_md_info_from_type( peer_cert_digest_type );
7077 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7078 size_t digest_len;
7079
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007080 if( peer_cert_digest == NULL ||
7081 digest_info == MBEDTLS_MD_INVALID_HANDLE )
7082 {
Hanno Beckerdf759382019-02-05 17:02:46 +00007083 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007084 }
Hanno Beckerdf759382019-02-05 17:02:46 +00007085
7086 digest_len = mbedtls_md_get_size( digest_info );
7087 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
7088 return( -1 );
7089
7090 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
7091 if( ret != 0 )
7092 return( -1 );
7093
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007094 return( mbedtls_platform_memcmp( tmp_digest, peer_cert_digest, digest_len ) );
Hanno Beckerdf759382019-02-05 17:02:46 +00007095}
Hanno Becker5882dd02019-06-06 16:25:57 +01007096#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00007097#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00007098
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007099/*
7100 * Once the certificate message is read, parse it into a cert chain and
7101 * perform basic checks, but leave actual verification to the caller
7102 */
Hanno Becker35e41772019-02-05 15:37:23 +00007103static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
7104 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00007105{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007106 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00007107#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7108 int crt_cnt=0;
7109#endif
Paul Bakker23986e52011-04-24 08:57:21 +00007110 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02007111 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00007112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007116 mbedtls_ssl_pend_fatal_alert( ssl,
7117 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007119 }
7120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
7122 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007125 mbedtls_ssl_pend_fatal_alert( ssl,
7126 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007128 }
7129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007131
Paul Bakker5121ce52009-01-03 21:22:43 +00007132 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00007134 */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007135 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007136
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007137 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007141 mbedtls_ssl_pend_fatal_alert( ssl,
7142 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007143 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007144 }
7145
Hanno Becker33c3dc82019-01-30 14:46:46 +00007146 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7147 i += 3;
7148
Hanno Becker33c3dc82019-01-30 14:46:46 +00007149 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007150 while( i < ssl->in_hslen )
7151 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007152 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02007153 if ( i + 3 > ssl->in_hslen ) {
7154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007155 mbedtls_ssl_pend_fatal_alert( ssl,
7156 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02007157 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
7158 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007159 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7160 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007161 if( ssl->in_msg[i] != 0 )
7162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007164 mbedtls_ssl_pend_fatal_alert( ssl,
7165 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007167 }
7168
Hanno Becker33c3dc82019-01-30 14:46:46 +00007169 /* Read length of the next CRT in the chain. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007170 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007171 i += 3;
7172
7173 if( n < 128 || i + n > ssl->in_hslen )
7174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007176 mbedtls_ssl_pend_fatal_alert( ssl,
7177 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007178 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007179 }
7180
Hanno Becker33c3dc82019-01-30 14:46:46 +00007181 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00007182#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7183 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01007184 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7185 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00007186 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007187 {
Hanno Becker68b856d2019-02-08 14:00:04 +00007188 /* During client-side renegotiation, check that the server's
7189 * end-CRTs hasn't changed compared to the initial handshake,
7190 * mitigating the triple handshake attack. On success, reuse
7191 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00007192 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7193 if( ssl_check_peer_crt_unchanged( ssl,
7194 &ssl->in_msg[i],
7195 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007196 {
Hanno Becker35e41772019-02-05 15:37:23 +00007197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007198 mbedtls_ssl_pend_fatal_alert( ssl,
7199 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007200 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007201 }
Hanno Becker35e41772019-02-05 15:37:23 +00007202
7203 /* Now we can safely free the original chain. */
7204 ssl_clear_peer_cert( ssl->session );
7205 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007206#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7207
Hanno Becker33c3dc82019-01-30 14:46:46 +00007208 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007209#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007210 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007211#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007212 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007213 * it in-place from the input buffer instead of making a copy. */
7214 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7215#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007216 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007217 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007218 case 0: /*ok*/
7219 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7220 /* Ignore certificate with an unknown algorithm: maybe a
7221 prior certificate was already trusted. */
7222 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007223
Hanno Becker33c3dc82019-01-30 14:46:46 +00007224 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7225 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7226 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007227
Hanno Becker33c3dc82019-01-30 14:46:46 +00007228 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7229 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7230 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007231
Hanno Becker33c3dc82019-01-30 14:46:46 +00007232 default:
7233 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7234 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007235 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007236 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7237 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007238 }
7239
7240 i += n;
7241 }
7242
Hanno Becker35e41772019-02-05 15:37:23 +00007243 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007244 return( 0 );
7245}
7246
Hanno Beckerb8a08572019-02-05 12:49:06 +00007247#if defined(MBEDTLS_SSL_SRV_C)
7248static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7249{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007250 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007251 return( -1 );
7252
7253#if defined(MBEDTLS_SSL_PROTO_SSL3)
7254 /*
7255 * Check if the client sent an empty certificate
7256 */
Hanno Becker2881d802019-05-22 14:44:53 +01007257 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007258 {
7259 if( ssl->in_msglen == 2 &&
7260 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7261 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7262 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7263 {
7264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7265 return( 0 );
7266 }
7267
7268 return( -1 );
7269 }
7270#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7271
7272#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7273 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7274 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7275 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7276 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Teppo Järvelin0efac532019-10-04 13:21:08 +03007277 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 +00007278 {
7279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7280 return( 0 );
7281 }
7282
Hanno Beckerb8a08572019-02-05 12:49:06 +00007283#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7284 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007285
7286 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007287}
7288#endif /* MBEDTLS_SSL_SRV_C */
7289
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007290/* Check if a certificate message is expected.
7291 * Return either
7292 * - SSL_CERTIFICATE_EXPECTED, or
7293 * - SSL_CERTIFICATE_SKIP
7294 * indicating whether a Certificate message is expected or not.
7295 */
7296#define SSL_CERTIFICATE_EXPECTED 0
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007297#define SSL_CERTIFICATE_SKIP 0xff
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007298static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7299 int authmode )
7300{
Hanno Becker473f98f2019-06-26 10:27:32 +01007301 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007302 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007303
7304 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7305 return( SSL_CERTIFICATE_SKIP );
7306
7307#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007308 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007309 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007310 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7311 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7312 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007313 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007314 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007315
7316 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7317 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007318 ssl->session_negotiate->verify_result =
7319 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7320 return( SSL_CERTIFICATE_SKIP );
7321 }
7322 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007323#else
7324 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007325#endif /* MBEDTLS_SSL_SRV_C */
7326
7327 return( SSL_CERTIFICATE_EXPECTED );
7328}
7329
Hanno Becker3cf50612019-02-05 14:36:34 +00007330static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007331 volatile int authmode,
Hanno Becker3cf50612019-02-05 14:36:34 +00007332 mbedtls_x509_crt *chain,
7333 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007334{
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007335 volatile int verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7336 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7337 volatile int flow_counter = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01007338 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007339 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007340 mbedtls_x509_crt *ca_chain;
7341 mbedtls_x509_crl *ca_crl;
7342
7343 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
Jarno Lamsae1621d42019-12-19 08:58:56 +02007344 {
Hanno Becker3cf50612019-02-05 14:36:34 +00007345 return( 0 );
Jarno Lamsae1621d42019-12-19 08:58:56 +02007346 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007347
Jarno Lamsae1621d42019-12-19 08:58:56 +02007348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Hanno Becker3cf50612019-02-05 14:36:34 +00007349#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7350 if( ssl->handshake->sni_ca_chain != NULL )
7351 {
7352 ca_chain = ssl->handshake->sni_ca_chain;
7353 ca_crl = ssl->handshake->sni_ca_crl;
7354 }
7355 else
7356#endif
7357 {
7358 ca_chain = ssl->conf->ca_chain;
7359 ca_crl = ssl->conf->ca_crl;
7360 }
7361
7362 /*
7363 * Main check: verify certificate
7364 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007365 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007366 chain,
7367 ca_chain, ca_crl,
7368 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007369#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007370 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007371#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007372 &ssl->session_negotiate->verify_result,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01007373#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
7374 ssl->conf->f_vrfy, ssl->conf->p_vrfy,
7375#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
7376 rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007377
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007378 if( verify_ret == 0 )
7379 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007380 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007381 if( verify_ret == 0 )
7382 {
7383 flow_counter++;
7384 }
7385 else
7386 {
7387 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7388 }
7389 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007390 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007391 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007392 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007393 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007394 }
7395
7396#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007397 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007398 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7399#endif
7400
7401 /*
7402 * Secondary checks: always done, but change 'ret' only if it was 0
7403 */
7404
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007405#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007406 {
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007407#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007408 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007409#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007410 mbedtls_pk_context *pk;
7411 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7412 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007413 {
7414 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007415 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007416 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007417
7418 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007419 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007420 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007421 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007422 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007423
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007424 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007425#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007426
7427 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007428 {
7429 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7430
7431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007432 if( verify_ret == 0 )
7433 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007434 flow_counter++;
7435 }
7436 if( ret == 0 )
7437 {
7438 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007439 }
7440 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007441#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007442
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007443 ret = mbedtls_ssl_check_cert_usage( chain,
Hanno Becker3cf50612019-02-05 14:36:34 +00007444 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007445 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7446 MBEDTLS_SSL_IS_CLIENT ),
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007447 &ssl->session_negotiate->verify_result );
7448 if( ret == 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007449 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007450 flow_counter++;
7451 }
7452 else
7453 {
7454 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007456 if( verify_ret == 0 )
7457 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007458 }
7459
7460 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7461 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7462 * with details encoded in the verification flags. All other kinds
7463 * of error codes, including those from the user provided f_vrfy
7464 * functions, are treated as fatal and lead to a failure of
7465 * ssl_parse_certificate even if verification was optional. */
7466 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007467 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7468 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007469 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007470 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007471 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7472 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7473 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7474 {
7475 verify_ret = 0;
7476 flow_counter++;
7477 }
7478 else
7479 {
7480 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7481 }
7482 } else {
7483 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007484 }
7485
7486 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7487 {
7488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007489 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007490 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007491 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007492 else
7493 {
7494 flow_counter++;
7495 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007496
Hanno Becker8c13ee62019-02-26 16:48:17 +00007497 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007498 {
7499 uint8_t alert;
7500
7501 /* The certificate may have been rejected for several reasons.
7502 Pick one and send the corresponding alert. Which alert to send
7503 may be a subject of debate in some cases. */
7504 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7505 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7506 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7507 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7508 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7509 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7510 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7511 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7512 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7513 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7514 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7515 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7516 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7517 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7518 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7519 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7520 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7521 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7522 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7523 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7524 else
7525 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007526 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007527 }
7528
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007529 if( verify_ret == 0 &&
7530#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7531 flow_counter == 5 )
7532#else
7533 flow_counter == 4 )
7534#endif
7535 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007536 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007537 if( verify_ret == 0 &&
7538#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7539 flow_counter == 5 )
7540#else
7541 flow_counter == 4 )
7542#endif
7543 {
Jarno Lamsae1621d42019-12-19 08:58:56 +02007544 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER AUTHENTICATED" ) );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007545 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
7546 }
7547 else
7548 {
7549 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7550 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007551 } else {
7552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER NOT AUTHENTICATED, %d", flow_counter));
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007553 }
7554
Hanno Becker3cf50612019-02-05 14:36:34 +00007555#if defined(MBEDTLS_DEBUG_C)
7556 if( ssl->session_negotiate->verify_result != 0 )
7557 {
7558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7559 ssl->session_negotiate->verify_result ) );
7560 }
7561 else
7562 {
7563 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7564 }
7565#endif /* MBEDTLS_DEBUG_C */
7566
Hanno Becker8c13ee62019-02-26 16:48:17 +00007567 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007568}
7569
Hanno Becker34106f62019-02-08 14:59:05 +00007570#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007571
7572#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007573static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7574 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007575{
7576 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007577 /* Remember digest of the peer's end-CRT. */
7578 ssl->session_negotiate->peer_cert_digest =
7579 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7580 if( ssl->session_negotiate->peer_cert_digest == NULL )
7581 {
7582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7583 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007584 mbedtls_ssl_pend_fatal_alert( ssl,
7585 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007586
7587 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7588 }
7589
7590 ret = mbedtls_md( mbedtls_md_info_from_type(
7591 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7592 start, len,
7593 ssl->session_negotiate->peer_cert_digest );
7594
7595 ssl->session_negotiate->peer_cert_digest_type =
7596 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7597 ssl->session_negotiate->peer_cert_digest_len =
7598 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7599
7600 return( ret );
7601}
Hanno Becker5882dd02019-06-06 16:25:57 +01007602#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007603
7604static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7605 unsigned char *start, size_t len )
7606{
7607 unsigned char *end = start + len;
7608 int ret;
7609
7610 /* Make a copy of the peer's raw public key. */
7611 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7612 ret = mbedtls_pk_parse_subpubkey( &start, end,
7613 &ssl->handshake->peer_pubkey );
7614 if( ret != 0 )
7615 {
7616 /* We should have parsed the public key before. */
7617 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7618 }
7619
Manuel Pégourié-Gonnard2829bbf2019-09-19 10:45:14 +02007620 ssl->handshake->got_peer_pubkey = 1;
Hanno Becker34106f62019-02-08 14:59:05 +00007621 return( 0 );
7622}
7623#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7624
Hanno Becker3cf50612019-02-05 14:36:34 +00007625int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7626{
7627 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007628 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007629#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7630 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7631 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007632 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007633#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007634 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007635#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007636 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007637 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007638
7639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7640
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007641 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7642 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007643 {
7644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007645 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007646 }
7647
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007648#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7649 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007650 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007651 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007652 chain = ssl->handshake->ecrs_peer_cert;
7653 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007654 goto crt_verify;
7655 }
7656#endif
7657
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007658 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007659 {
7660 /* mbedtls_ssl_read_record may have sent an alert already. We
7661 let it decide whether to alert. */
7662 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007663 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007664 }
7665
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007666#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007667 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7668 {
7669 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007670
Hanno Beckerb8a08572019-02-05 12:49:06 +00007671 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007672 ret = 0;
7673 else
7674 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007675
Hanno Becker613d4902019-02-05 13:11:17 +00007676 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007677 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007678#endif /* MBEDTLS_SSL_SRV_C */
7679
Hanno Becker35e41772019-02-05 15:37:23 +00007680 /* Clear existing peer CRT structure in case we tried to
7681 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007682 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007683
7684 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7685 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007686 {
7687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7688 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007689 mbedtls_ssl_pend_fatal_alert( ssl,
7690 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007691
Hanno Beckere4aeb762019-02-05 17:19:52 +00007692 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7693 goto exit;
7694 }
7695 mbedtls_x509_crt_init( chain );
7696
7697 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007698 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007699 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007700
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007701#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7702 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007703 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007704
7705crt_verify:
7706 if( ssl->handshake->ecrs_enabled)
7707 rs_ctx = &ssl->handshake->ecrs_ctx;
7708#endif
7709
Hanno Becker3cf50612019-02-05 14:36:34 +00007710 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007711 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007712 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007713 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007714
Hanno Becker3008d282019-02-05 17:02:28 +00007715#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007716 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007717 size_t pk_len;
7718 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007719
Hanno Becker34106f62019-02-08 14:59:05 +00007720 /* We parse the CRT chain without copying, so
7721 * these pointers point into the input buffer,
7722 * and are hence still valid after freeing the
7723 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007724
Hanno Becker5882dd02019-06-06 16:25:57 +01007725#if defined(MBEDTLS_SSL_RENEGOTIATION)
7726 unsigned char *crt_start;
7727 size_t crt_len;
7728
Hanno Becker34106f62019-02-08 14:59:05 +00007729 crt_start = chain->raw.p;
7730 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007731#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007732
Hanno Becker8c13ee62019-02-26 16:48:17 +00007733 pk_start = chain->cache->pk_raw.p;
7734 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007735
7736 /* Free the CRT structures before computing
7737 * digest and copying the peer's public key. */
7738 mbedtls_x509_crt_free( chain );
7739 mbedtls_free( chain );
7740 chain = NULL;
7741
Hanno Becker5882dd02019-06-06 16:25:57 +01007742#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007743 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007744 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007745 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007746#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007747
Hanno Becker34106f62019-02-08 14:59:05 +00007748 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007749 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007750 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007751 }
Hanno Becker34106f62019-02-08 14:59:05 +00007752#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7753 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007754 ssl->session_negotiate->peer_cert = chain;
7755 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007756#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007759
Hanno Becker613d4902019-02-05 13:11:17 +00007760exit:
7761
Hanno Beckere4aeb762019-02-05 17:19:52 +00007762 if( ret == 0 )
Jarno Lamsa2b205162019-11-12 15:36:21 +02007763 {
7764 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7765 {
7766 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7767 }
7768 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7769 {
7770 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7771 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007772 else
7773 {
7774 ssl->state = MBEDTLS_SSL_INVALID;
7775 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02007776 }
Hanno Beckere4aeb762019-02-05 17:19:52 +00007777
7778#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7779 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7780 {
7781 ssl->handshake->ecrs_peer_cert = chain;
7782 chain = NULL;
7783 }
7784#endif
7785
7786 if( chain != NULL )
7787 {
7788 mbedtls_x509_crt_free( chain );
7789 mbedtls_free( chain );
7790 }
7791
Paul Bakker5121ce52009-01-03 21:22:43 +00007792 return( ret );
7793}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007794#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007797{
7798 int ret;
7799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007803 ssl->out_msglen = 1;
7804 ssl->out_msg[0] = 1;
7805
Jarno Lamsa2b205162019-11-12 15:36:21 +02007806 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7807 {
7808 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7809 }
7810 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7811 {
7812 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7813 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007814 else
7815 {
7816 ssl->state = MBEDTLS_SSL_INVALID;
7817 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007818
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007819 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007820 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007822 return( ret );
7823 }
7824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007826
7827 return( 0 );
7828}
7829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007831{
7832 int ret;
7833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007835
Hanno Becker327c93b2018-08-15 13:56:18 +01007836 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007839 return( ret );
7840 }
7841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007845 mbedtls_ssl_pend_fatal_alert( ssl,
7846 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007848 }
7849
Hanno Beckere678eaa2018-08-21 14:57:46 +01007850 /* CCS records are only accepted if they have length 1 and content '1',
7851 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007852
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007853 /*
7854 * Switch to our negotiated transform and session parameters for inbound
7855 * data.
7856 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007857 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007858 ssl->transform_in = ssl->transform_negotiate;
7859 ssl->session_in = ssl->session_negotiate;
7860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007862 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007865 ssl_dtls_replay_reset( ssl );
7866#endif
7867
7868 /* Increment epoch */
7869 if( ++ssl->in_epoch == 0 )
7870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007872 /* This is highly unlikely to happen for legitimate reasons, so
7873 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007875 }
7876 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007877 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007879#if defined(MBEDTLS_SSL_PROTO_TLS)
7880 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02007881 mbedtls_platform_memset( ssl->in_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007882 }
7883#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007884
Hanno Beckerf5970a02019-05-08 09:38:41 +01007885 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7888 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007893 mbedtls_ssl_pend_fatal_alert( ssl,
7894 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007895 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007896 }
7897 }
7898#endif
7899
Jarno Lamsa2b205162019-11-12 15:36:21 +02007900 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7901 {
7902 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7903 }
7904 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7905 {
7906 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7907 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007908 else
7909 {
7910 ssl->state = MBEDTLS_SSL_INVALID;
7911 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007914
7915 return( 0 );
7916}
7917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007919{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7921 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007922 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007923 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007924#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7926#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007927 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007928#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007930 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007931#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007933}
7934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007936{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007938
7939 /*
7940 * Free our handshake params
7941 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007942 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007944 ssl->handshake = NULL;
7945
7946 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007947 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007948 */
7949 if( ssl->transform )
7950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007951 mbedtls_ssl_transform_free( ssl->transform );
7952 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007953 }
7954 ssl->transform = ssl->transform_negotiate;
7955 ssl->transform_negotiate = NULL;
7956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007957 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007958}
7959
Jarno Lamsae1621d42019-12-19 08:58:56 +02007960int mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007961{
Jarno Lamsae1621d42019-12-19 08:58:56 +02007962 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007963
7964#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Jarno Lamsa015aa442019-12-20 12:09:37 +02007965 volatile const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007966 ? ssl->handshake->sni_authmode
7967 : mbedtls_ssl_conf_get_authmode( ssl->conf );
7968#else
Jarno Lamsa015aa442019-12-20 12:09:37 +02007969 volatile const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007970#endif
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007971#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7972 volatile int crt_expected = SSL_CERTIFICATE_EXPECTED;
7973 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7974#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007977#if defined(MBEDTLS_SSL_RENEGOTIATION)
7978 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007980 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007981 ssl->renego_records_seen = 0;
7982 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007983#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007984
7985 /*
7986 * Free the previous session and switch in the current one
7987 */
Paul Bakker0a597072012-09-25 21:55:46 +00007988 if( ssl->session )
7989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007990#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007991 /* RFC 7366 3.1: keep the EtM state */
7992 ssl->session_negotiate->encrypt_then_mac =
7993 ssl->session->encrypt_then_mac;
7994#endif
7995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996 mbedtls_ssl_session_free( ssl->session );
7997 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007998 }
7999 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00008000 ssl->session_negotiate = NULL;
8001
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02008002#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00008003 /*
8004 * Add cache entry
8005 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008006 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02008007 ssl->session->id_len != 0 &&
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008008 ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_UNSET )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02008009 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008010 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02008012 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02008013#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00008014
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008015 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8016 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8017#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8018 crt_expected == SSL_CERTIFICATE_SKIP )
8019#else
8020 1 )
8021#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008022 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008023 mbedtls_platform_random_delay();
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008024 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8025 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8026#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8027 crt_expected == SSL_CERTIFICATE_SKIP )
8028#else
8029 1 )
8030#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008031 {
8032 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
8033 }
8034 else
8035 {
8036 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8037 goto cleanup;
8038 }
8039 }
8040
Jarno Lamsae1621d42019-12-19 08:58:56 +02008041#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008042 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008043 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008044 mbedtls_platform_random_delay();
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008045 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008046 {
Jarno Lamsa06164052019-12-19 14:40:36 +02008047 /* When doing session resume, no premaster or peer authentication */
Jarno Lamsae1621d42019-12-19 08:58:56 +02008048 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsa06164052019-12-19 14:40:36 +02008049 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008050 }
8051 else
8052 {
8053 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008054 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008055 }
8056 }
8057#endif
8058
8059 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8060 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008061 mbedtls_platform_random_delay();
Jarno Lamsae1621d42019-12-19 08:58:56 +02008062 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8063 {
8064 ret = 0;
8065 }
8066 else
8067 {
8068 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008069 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008070 }
8071 }
8072 else
8073 {
8074 ret = MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008075 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008076 }
8077
Jarno Lamsa06164052019-12-19 14:40:36 +02008078 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8079 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8080 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8081 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008082 mbedtls_platform_random_delay();
Jarno Lamsa06164052019-12-19 14:40:36 +02008083 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8084 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8085 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8086 {
8087 ret = 0;
8088 }
8089 else
8090 {
8091 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8092 goto cleanup;
8093 }
8094 }
8095 else
8096 {
8097 MBEDTLS_SSL_DEBUG_MSG( 3, ( "hello random %d", ssl->handshake->hello_random_set ) );
8098 MBEDTLS_SSL_DEBUG_MSG( 3, ( "key_derivation_done %d", ssl->handshake->key_derivation_done ) );
8099 MBEDTLS_SSL_DEBUG_MSG( 3, ( "premaster_generated %d", ssl->handshake->premaster_generated ) );
8100 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8101 }
8102
8103cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008104#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008105 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008106 ssl->handshake->flight != NULL )
8107 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008108 /* Cancel handshake timer */
8109 ssl_set_timer( ssl, 0 );
8110
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008111 /* Keep last flight around in case we need to resend it:
8112 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008114 }
8115 else
8116#endif
8117 ssl_handshake_wrapup_free_hs_transform( ssl );
8118
Jarno Lamsa2b205162019-11-12 15:36:21 +02008119 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Paul Bakker48916f92012-09-16 19:57:18 +00008120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Jarno Lamsae1621d42019-12-19 08:58:56 +02008122 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00008123}
8124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008125int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00008126{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008127 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00008128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008130
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008131 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01008132
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008133 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8134 mbedtls_ssl_suite_get_mac(
8135 mbedtls_ssl_ciphersuite_from_id(
8136 mbedtls_ssl_session_get_ciphersuite(
8137 ssl->session_negotiate ) ) ),
8138 ssl, ssl->out_msg + 4,
8139 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008140
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01008141 /*
8142 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8143 * may define some other value. Currently (early 2016), no defined
8144 * ciphersuite does this (and this is unlikely to change as activity has
8145 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8146 */
Hanno Becker2881d802019-05-22 14:44:53 +01008147 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008150 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008151 mbedtls_platform_memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008152#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008153
Paul Bakker5121ce52009-01-03 21:22:43 +00008154 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008155 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8156 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008157
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008158#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00008159 /*
8160 * In case of session resuming, invert the client and server
8161 * ChangeCipherSpec messages order.
8162 */
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008163 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008166 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8167 MBEDTLS_SSL_IS_CLIENT )
8168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008169 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008170 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008171#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008172#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008173 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8174 MBEDTLS_SSL_IS_SERVER )
8175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008176 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008177 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008178#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008179 }
8180 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008181#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008182 {
8183 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8184 {
8185 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8186 }
8187 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8188 {
8189 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8190 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008191 else
8192 {
8193 ssl->state = MBEDTLS_SSL_INVALID;
8194 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008195 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008196
Paul Bakker48916f92012-09-16 19:57:18 +00008197 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008198 * Switch to our negotiated transform and session parameters for outbound
8199 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008203#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008204 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008205 {
8206 unsigned char i;
8207
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008208 /* Remember current epoch settings for resending */
8209 ssl->handshake->alt_transform_out = ssl->transform_out;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008210 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008211
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008212 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008213 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008214
8215 /* Increment epoch */
8216 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008217 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008218 break;
8219
8220 /* The loop goes to its end iff the counter is wrapping */
8221 if( i == 0 )
8222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8224 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008225 }
8226 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008227 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008228#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008229#if defined(MBEDTLS_SSL_PROTO_TLS)
8230 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008231 mbedtls_platform_memset( ssl->cur_out_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008232 }
8233#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008234
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008235 ssl->transform_out = ssl->transform_negotiate;
8236 ssl->session_out = ssl->session_negotiate;
8237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008238#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8239 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008241 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008243 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8244 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008245 }
8246 }
8247#endif
8248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008249#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008250 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008251 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008252#endif
8253
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008254 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008255 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008256 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008257 return( ret );
8258 }
8259
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008260#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008261 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008262 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8263 {
8264 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8265 return( ret );
8266 }
8267#endif
8268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008270
8271 return( 0 );
8272}
8273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008274#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008275#define SSL_MAX_HASH_LEN 36
8276#else
8277#define SSL_MAX_HASH_LEN 12
8278#endif
8279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008280int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008281{
Paul Bakker23986e52011-04-24 08:57:21 +00008282 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008283 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008284 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008287
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008288 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8289 mbedtls_ssl_suite_get_mac(
8290 mbedtls_ssl_ciphersuite_from_id(
8291 mbedtls_ssl_session_get_ciphersuite(
8292 ssl->session_negotiate ) ) ),
8293 ssl, buf,
8294 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008295
Hanno Becker327c93b2018-08-15 13:56:18 +01008296 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008298 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008299 return( ret );
8300 }
8301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008302 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008305 mbedtls_ssl_pend_fatal_alert( ssl,
8306 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008308 }
8309
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008310 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008311#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01008312 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008313 hash_len = 36;
8314 else
8315#endif
8316 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008318 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8319 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008322 mbedtls_ssl_pend_fatal_alert( ssl,
8323 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008324 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008325 }
8326
Teppo Järvelin707ceb82019-10-04 07:49:39 +03008327 if( mbedtls_platform_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008328 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008331 mbedtls_ssl_pend_fatal_alert( ssl,
8332 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008333 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008334 }
8335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008336#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008337 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008338 mbedtls_platform_memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008339#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008340
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008341#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008342 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008344#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008345 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008346 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008347#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008348#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008349 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008350 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008351#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008352 }
8353 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008354#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008355 {
8356 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8357 {
8358 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8359 }
8360 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8361 {
8362 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8363 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008364 else
8365 {
8366 ssl->state = MBEDTLS_SSL_INVALID;
8367 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008368 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008370#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008371 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008373#endif
8374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008376
8377 return( 0 );
8378}
8379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008380static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008381{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008382 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008384#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8385 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8386 mbedtls_md5_init( &handshake->fin_md5 );
8387 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008388 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8389 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008390#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008391#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8392#if defined(MBEDTLS_SHA256_C)
8393 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008394 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008395#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008396#if defined(MBEDTLS_SHA512_C)
8397 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008398 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008399#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008400#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008401
Hanno Becker7e5437a2017-04-28 17:15:26 +01008402#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8403 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8404 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8405#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008407#if defined(MBEDTLS_DHM_C)
8408 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008409#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008410#if defined(MBEDTLS_ECDH_C)
8411 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008412#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008413#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008414 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008415#if defined(MBEDTLS_SSL_CLI_C)
8416 handshake->ecjpake_cache = NULL;
8417 handshake->ecjpake_cache_len = 0;
8418#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008419#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008420
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008421#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008422 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008423#endif
8424
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008425#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8426 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8427#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008428
8429#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8430 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8431 mbedtls_pk_init( &handshake->peer_pubkey );
8432#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008433}
8434
Hanno Becker611a83b2018-01-03 14:27:32 +00008435void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008436{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008437 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008439 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8440 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008441
Hanno Becker92231322018-01-03 15:32:51 +00008442#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443 mbedtls_md_init( &transform->md_ctx_enc );
8444 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008445#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008446}
8447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008448void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008449{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008450 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008451}
8452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008453static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008454{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008455 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008456 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008457 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008458 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008459 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008460 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008461 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008462
8463 /*
8464 * Either the pointers are now NULL or cleared properly and can be freed.
8465 * Now allocate missing structures.
8466 */
8467 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008468 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008469 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008470 }
Paul Bakker48916f92012-09-16 19:57:18 +00008471
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008472 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008473 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008474 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008475 }
Paul Bakker48916f92012-09-16 19:57:18 +00008476
Paul Bakker82788fb2014-10-20 13:59:19 +02008477 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008478 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008479 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008480 }
Paul Bakker48916f92012-09-16 19:57:18 +00008481
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008482 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008483 if( ssl->handshake == NULL ||
8484 ssl->transform_negotiate == NULL ||
8485 ssl->session_negotiate == NULL )
8486 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008489 mbedtls_free( ssl->handshake );
8490 mbedtls_free( ssl->transform_negotiate );
8491 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008492
8493 ssl->handshake = NULL;
8494 ssl->transform_negotiate = NULL;
8495 ssl->session_negotiate = NULL;
8496
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008497 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008498 }
8499
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008500 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008501 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008502 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008503 ssl_handshake_params_init( ssl->handshake );
8504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008505#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008506 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008507 {
8508 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008509
Hanno Becker2d9623f2019-06-13 12:07:05 +01008510 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008511 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8512 else
8513 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8514 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008515#endif
8516
Paul Bakker48916f92012-09-16 19:57:18 +00008517 return( 0 );
8518}
8519
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008520#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008521/* Dummy cookie callbacks for defaults */
8522static int ssl_cookie_write_dummy( void *ctx,
8523 unsigned char **p, unsigned char *end,
8524 const unsigned char *cli_id, size_t cli_id_len )
8525{
8526 ((void) ctx);
8527 ((void) p);
8528 ((void) end);
8529 ((void) cli_id);
8530 ((void) cli_id_len);
8531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008532 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008533}
8534
8535static int ssl_cookie_check_dummy( void *ctx,
8536 const unsigned char *cookie, size_t cookie_len,
8537 const unsigned char *cli_id, size_t cli_id_len )
8538{
8539 ((void) ctx);
8540 ((void) cookie);
8541 ((void) cookie_len);
8542 ((void) cli_id);
8543 ((void) cli_id_len);
8544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008545 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008546}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008547#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008548
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008549/* Once ssl->out_hdr as the address of the beginning of the
8550 * next outgoing record is set, deduce the other pointers.
8551 *
8552 * Note: For TLS, we save the implicit record sequence number
8553 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8554 * and the caller has to make sure there's space for this.
8555 */
8556
8557static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8558 mbedtls_ssl_transform *transform )
8559{
8560#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008561 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008562 {
8563 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008564#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008565 ssl->out_cid = ssl->out_ctr + 8;
8566 ssl->out_len = ssl->out_cid;
8567 if( transform != NULL )
8568 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008569#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008570 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008571#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008572 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008573 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008574 MBEDTLS_SSL_TRANSPORT_ELSE
8575#endif /* MBEDTLS_SSL_PROTO_DTLS */
8576#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008577 {
8578 ssl->out_ctr = ssl->out_hdr - 8;
8579 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008580#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008581 ssl->out_cid = ssl->out_len;
8582#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008583 ssl->out_iv = ssl->out_hdr + 5;
8584 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008585#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008586
8587 /* Adjust out_msg to make space for explicit IV, if used. */
8588 if( transform != NULL &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008589 mbedtls_ssl_ver_geq(
8590 mbedtls_ssl_get_minor_ver( ssl ),
8591 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008592 {
8593 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8594 }
8595 else
8596 ssl->out_msg = ssl->out_iv;
8597}
8598
8599/* Once ssl->in_hdr as the address of the beginning of the
8600 * next incoming record is set, deduce the other pointers.
8601 *
8602 * Note: For TLS, we save the implicit record sequence number
8603 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8604 * and the caller has to make sure there's space for this.
8605 */
8606
Hanno Beckerf5970a02019-05-08 09:38:41 +01008607static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008608{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008609 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008610 * of unprotected TLS/DTLS records, with ssl->in_msg
8611 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008612 *
8613 * When decrypting a protected record, ssl->in_msg
8614 * will be shifted to point to the beginning of the
8615 * record plaintext.
8616 */
8617
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008618#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008619 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008620 {
Hanno Becker70e79282019-05-03 14:34:53 +01008621 /* This sets the header pointers to match records
8622 * without CID. When we receive a record containing
8623 * a CID, the fields are shifted accordingly in
8624 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008625 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008626#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008627 ssl->in_cid = ssl->in_ctr + 8;
8628 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008629#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008630 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008631#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008632 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008633 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008634 MBEDTLS_SSL_TRANSPORT_ELSE
8635#endif /* MBEDTLS_SSL_PROTO_DTLS */
8636#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008637 {
8638 ssl->in_ctr = ssl->in_hdr - 8;
8639 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008640#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008641 ssl->in_cid = ssl->in_len;
8642#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008643 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008644 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008645#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008646}
8647
Paul Bakker5121ce52009-01-03 21:22:43 +00008648/*
8649 * Initialize an SSL context
8650 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008651void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8652{
8653 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8654}
8655
8656/*
8657 * Setup an SSL context
8658 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008659
8660static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8661{
8662 /* Set the incoming and outgoing record pointers. */
8663#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008664 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008665 {
8666 ssl->out_hdr = ssl->out_buf;
8667 ssl->in_hdr = ssl->in_buf;
8668 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008669 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008670#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008671#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008672 {
8673 ssl->out_hdr = ssl->out_buf + 8;
8674 ssl->in_hdr = ssl->in_buf + 8;
8675 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008676#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008677
8678 /* Derive other internal pointers. */
8679 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008680 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008681}
8682
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008683int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008684 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008685{
Paul Bakker48916f92012-09-16 19:57:18 +00008686 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008687
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008688 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008689
Hanno Beckeref982d52019-07-23 15:56:18 +01008690#if defined(MBEDTLS_USE_TINYCRYPT)
8691 uECC_set_rng( &uecc_rng_wrapper );
8692#endif
8693
Paul Bakker62f2dee2012-09-28 07:31:51 +00008694 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008695 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008696 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008697
8698 /* Set to NULL in case of an error condition */
8699 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008700
Angus Grattond8213d02016-05-25 20:56:48 +10008701 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8702 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008703 {
Angus Grattond8213d02016-05-25 20:56:48 +10008704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008705 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008706 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008707 }
8708
8709 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8710 if( ssl->out_buf == NULL )
8711 {
8712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008713 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008714 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008715 }
8716
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008717 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008718
Paul Bakker48916f92012-09-16 19:57:18 +00008719 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008720 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008721
Hanno Beckerc8f52992019-07-25 11:15:08 +01008722 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008723
Paul Bakker5121ce52009-01-03 21:22:43 +00008724 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008725
8726error:
8727 mbedtls_free( ssl->in_buf );
8728 mbedtls_free( ssl->out_buf );
8729
8730 ssl->conf = NULL;
8731
8732 ssl->in_buf = NULL;
8733 ssl->out_buf = NULL;
8734
8735 ssl->in_hdr = NULL;
8736 ssl->in_ctr = NULL;
8737 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008738 ssl->in_msg = NULL;
8739
8740 ssl->out_hdr = NULL;
8741 ssl->out_ctr = NULL;
8742 ssl->out_len = NULL;
8743 ssl->out_iv = NULL;
8744 ssl->out_msg = NULL;
8745
k-stachowiak9f7798e2018-07-31 16:52:32 +02008746 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008747}
8748
8749/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008750 * Reset an initialized and used SSL context for re-use while retaining
8751 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008752 *
8753 * If partial is non-zero, keep data in the input buffer and client ID.
8754 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008755 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008756static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008757{
Paul Bakker48916f92012-09-16 19:57:18 +00008758 int ret;
8759
Hanno Becker7e772132018-08-10 12:38:21 +01008760#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8761 !defined(MBEDTLS_SSL_SRV_C)
8762 ((void) partial);
8763#endif
8764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008765 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008766
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008767 /* Cancel any possibly running timer */
8768 ssl_set_timer( ssl, 0 );
8769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770#if defined(MBEDTLS_SSL_RENEGOTIATION)
8771 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008772 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008773
8774 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008775 mbedtls_platform_memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8776 mbedtls_platform_memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008777#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008778 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008779
Paul Bakker7eb013f2011-10-06 12:37:39 +00008780 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008781 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008782
8783 ssl->in_msgtype = 0;
8784 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008785#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008786 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008787 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008788#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008789#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008790 ssl_dtls_replay_reset( ssl );
8791#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008792
8793 ssl->in_hslen = 0;
8794 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008795
8796 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008797
8798 ssl->out_msgtype = 0;
8799 ssl->out_msglen = 0;
8800 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008801#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8802 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008803 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008804#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008805
Hanno Becker19859472018-08-06 09:40:20 +01008806 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8807
Paul Bakker48916f92012-09-16 19:57:18 +00008808 ssl->transform_in = NULL;
8809 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008810
Hanno Becker78640902018-08-13 16:35:15 +01008811 ssl->session_in = NULL;
8812 ssl->session_out = NULL;
8813
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008814 mbedtls_platform_memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008815
8816#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008817 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008818#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8819 {
8820 ssl->in_left = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008821 mbedtls_platform_memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008822 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008824#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8825 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8828 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008830 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8831 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008832 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008833 }
8834#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008835
Paul Bakker48916f92012-09-16 19:57:18 +00008836 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008838 mbedtls_ssl_transform_free( ssl->transform );
8839 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008840 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008841 }
Paul Bakker48916f92012-09-16 19:57:18 +00008842
Paul Bakkerc0463502013-02-14 11:19:38 +01008843 if( ssl->session )
8844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845 mbedtls_ssl_session_free( ssl->session );
8846 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008847 ssl->session = NULL;
8848 }
8849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008850#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008851 ssl->alpn_chosen = NULL;
8852#endif
8853
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008854#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008855#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008856 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008857#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008858 {
8859 mbedtls_free( ssl->cli_id );
8860 ssl->cli_id = NULL;
8861 ssl->cli_id_len = 0;
8862 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008863#endif
8864
Paul Bakker48916f92012-09-16 19:57:18 +00008865 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8866 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008867
8868 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008869}
8870
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008871/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008872 * Reset an initialized and used SSL context for re-use while retaining
8873 * all application-set variables, function pointers and data.
8874 */
8875int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8876{
8877 return( ssl_session_reset_int( ssl, 0 ) );
8878}
8879
8880/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008881 * SSL set accessors
8882 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008883#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008884void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008885{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008886 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008887}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008888#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008889
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008890void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008891{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008892 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008893}
8894
Hanno Becker7f376f42019-06-12 16:20:48 +01008895#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8896 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008897void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008898{
Hanno Becker7f376f42019-06-12 16:20:48 +01008899 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008900}
Hanno Becker7f376f42019-06-12 16:20:48 +01008901#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008902
Hanno Beckerde671542019-06-12 16:30:46 +01008903#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8904 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8905void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8906 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008907{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008908 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008909}
Hanno Beckerde671542019-06-12 16:30:46 +01008910#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008912#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008913
Hanno Becker1841b0a2018-08-24 11:13:57 +01008914void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8915 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008916{
8917 ssl->disable_datagram_packing = !allow_packing;
8918}
8919
Hanno Becker1f835fa2019-06-13 10:14:59 +01008920#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8921 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008922void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8923 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008924{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008925 conf->hs_timeout_min = min;
8926 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008927}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008928#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8929 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8930void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8931 uint32_t min, uint32_t max )
8932{
8933 ((void) conf);
8934 ((void) min);
8935 ((void) max);
8936}
8937#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8938 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8939
8940#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008941
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008942void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008943{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008944#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8945 conf->authmode = authmode;
8946#else
8947 ((void) conf);
8948 ((void) authmode);
8949#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008950}
8951
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008952#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8953 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008954void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008955 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008956 void *p_vrfy )
8957{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008958 conf->f_vrfy = f_vrfy;
8959 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008960}
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008961#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008962
Hanno Beckerece325c2019-06-13 15:39:27 +01008963#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008964void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008965 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008966 void *p_rng )
8967{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008968 conf->f_rng = f_rng;
8969 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008970}
Hanno Beckerece325c2019-06-13 15:39:27 +01008971#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008972
Hanno Becker14a4a442019-07-02 17:00:34 +01008973#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008974void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008975 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008976 void *p_dbg )
8977{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008978 conf->f_dbg = f_dbg;
8979 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008980}
Hanno Becker14a4a442019-07-02 17:00:34 +01008981#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008982
Hanno Beckera58a8962019-06-13 16:11:15 +01008983#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8984 !defined(MBEDTLS_SSL_CONF_SEND) && \
8985 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008986void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008987 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008988 mbedtls_ssl_send_t *f_send,
8989 mbedtls_ssl_recv_t *f_recv,
8990 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008991{
Hanno Beckera58a8962019-06-13 16:11:15 +01008992 ssl->p_bio = p_bio;
8993 ssl->f_send = f_send;
8994 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008995 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008996}
Hanno Beckera58a8962019-06-13 16:11:15 +01008997#else
8998void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8999 void *p_bio )
9000{
9001 ssl->p_bio = p_bio;
9002}
9003#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009004
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02009005#if defined(MBEDTLS_SSL_PROTO_DTLS)
9006void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
9007{
9008 ssl->mtu = mtu;
9009}
9010#endif
9011
Hanno Becker1f835fa2019-06-13 10:14:59 +01009012#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009013void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009014{
9015 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009016}
Hanno Becker1f835fa2019-06-13 10:14:59 +01009017#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009018
Hanno Becker0ae6b242019-06-13 16:45:36 +01009019#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
9020 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009021void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
9022 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00009023 mbedtls_ssl_set_timer_t *f_set_timer,
9024 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009025{
9026 ssl->p_timer = p_timer;
9027 ssl->f_set_timer = f_set_timer;
9028 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009029 /* Make sure we start with no timer running */
9030 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009031}
Hanno Becker0ae6b242019-06-13 16:45:36 +01009032#else
9033void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
9034 void *p_timer )
9035{
9036 ssl->p_timer = p_timer;
9037 /* Make sure we start with no timer running */
9038 ssl_set_timer( ssl, 0 );
9039}
9040#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009041
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009042#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009043void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009044 void *p_cache,
9045 int (*f_get_cache)(void *, mbedtls_ssl_session *),
9046 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00009047{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009048 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009049 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009050 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00009051}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009052#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009053
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009054#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009055int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00009056{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009057 int ret;
9058
9059 if( ssl == NULL ||
9060 session == NULL ||
9061 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009062 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009065 }
9066
Hanno Becker58fccf22019-02-06 14:30:46 +00009067 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
9068 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009069 return( ret );
9070
Jarno Lamsa8d09e572019-12-19 15:20:19 +02009071 ssl->handshake->resume = MBEDTLS_SSL_FI_FLAG_SET;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009072
9073 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009074}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009075#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009076
Hanno Becker73f4cb12019-06-27 13:51:07 +01009077#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009078void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009079 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00009080{
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009081 conf->ciphersuite_list[0] = ciphersuites;
9082 conf->ciphersuite_list[1] = ciphersuites;
9083 conf->ciphersuite_list[2] = ciphersuites;
9084 conf->ciphersuite_list[3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009085}
9086
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009087void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02009088 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009089 int major, int minor )
9090{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009091 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009092 return;
9093
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009094 if( mbedtls_ssl_ver_lt( minor, MBEDTLS_SSL_MINOR_VERSION_0 ) ||
9095 mbedtls_ssl_ver_gt( minor, MBEDTLS_SSL_MINOR_VERSION_3 ) )
9096 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009097 return;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009098 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009099
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009100 conf->ciphersuite_list[mbedtls_ssl_minor_ver_index( minor )] =
9101 ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00009102}
Hanno Becker73f4cb12019-06-27 13:51:07 +01009103#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009105#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009106void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01009107 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009108{
9109 conf->cert_profile = profile;
9110}
9111
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009112/* Append a new keycert entry to a (possibly empty) list */
9113static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
9114 mbedtls_x509_crt *cert,
9115 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009116{
niisato8ee24222018-06-25 19:05:48 +09009117 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009118
niisato8ee24222018-06-25 19:05:48 +09009119 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
9120 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009121 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009122
niisato8ee24222018-06-25 19:05:48 +09009123 new_cert->cert = cert;
9124 new_cert->key = key;
9125 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009126
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009127 /* Update head is the list was null, else add to the end */
9128 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01009129 {
niisato8ee24222018-06-25 19:05:48 +09009130 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01009131 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009132 else
9133 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009134 mbedtls_ssl_key_cert *cur = *head;
9135 while( cur->next != NULL )
9136 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09009137 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009138 }
9139
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009140 return( 0 );
9141}
9142
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009143int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009144 mbedtls_x509_crt *own_cert,
9145 mbedtls_pk_context *pk_key )
9146{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02009147 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009148}
9149
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009150void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009151 mbedtls_x509_crt *ca_chain,
9152 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009153{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009154 conf->ca_chain = ca_chain;
9155 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00009156}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009157#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00009158
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009159#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9160int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
9161 mbedtls_x509_crt *own_cert,
9162 mbedtls_pk_context *pk_key )
9163{
9164 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
9165 own_cert, pk_key ) );
9166}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02009167
9168void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
9169 mbedtls_x509_crt *ca_chain,
9170 mbedtls_x509_crl *ca_crl )
9171{
9172 ssl->handshake->sni_ca_chain = ca_chain;
9173 ssl->handshake->sni_ca_crl = ca_crl;
9174}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02009175
9176void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
9177 int authmode )
9178{
9179 ssl->handshake->sni_authmode = authmode;
9180}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009181#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9182
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009183#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009184/*
9185 * Set EC J-PAKE password for current handshake
9186 */
9187int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
9188 const unsigned char *pw,
9189 size_t pw_len )
9190{
9191 mbedtls_ecjpake_role role;
9192
Janos Follath8eb64132016-06-03 15:40:57 +01009193 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9195
Hanno Becker2d9623f2019-06-13 12:07:05 +01009196 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009197 role = MBEDTLS_ECJPAKE_SERVER;
9198 else
9199 role = MBEDTLS_ECJPAKE_CLIENT;
9200
9201 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
9202 role,
9203 MBEDTLS_MD_SHA256,
9204 MBEDTLS_ECP_DP_SECP256R1,
9205 pw, pw_len ) );
9206}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009207#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009209#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009210int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009211 const unsigned char *psk, size_t psk_len,
9212 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009213{
Paul Bakker6db455e2013-09-18 17:29:31 +02009214 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009215 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02009216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009217 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9218 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01009219
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009220 /* Identity len will be encoded on two bytes */
9221 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009222 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009223 {
9224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9225 }
9226
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009227 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02009228 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009229 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009230
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009231 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009232 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009233 conf->psk_len = 0;
9234 }
9235 if( conf->psk_identity != NULL )
9236 {
9237 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009238 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009239 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02009240 }
9241
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009242 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
9243 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05009244 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009245 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009246 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009247 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009248 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009249 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05009250 }
Paul Bakker6db455e2013-09-18 17:29:31 +02009251
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009252 conf->psk_len = psk_len;
9253 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02009254
Teppo Järvelin91d79382019-10-02 09:09:31 +03009255 mbedtls_platform_memcpy( conf->psk, psk, conf->psk_len );
9256 mbedtls_platform_memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02009257
9258 return( 0 );
9259}
9260
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009261int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9262 const unsigned char *psk, size_t psk_len )
9263{
9264 if( psk == NULL || ssl->handshake == NULL )
9265 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9266
9267 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9268 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9269
9270 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009271 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009272 mbedtls_platform_zeroize( ssl->handshake->psk,
9273 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01009274 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009275 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009276 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009277
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009278 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009279 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009280
9281 ssl->handshake->psk_len = psk_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03009282 mbedtls_platform_memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009283
9284 return( 0 );
9285}
9286
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009287void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009288 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009289 size_t),
9290 void *p_psk )
9291{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009292 conf->f_psk = f_psk;
9293 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009294}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009296
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009297#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009298
9299#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009300int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009301{
9302 int ret;
9303
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009304 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9305 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9306 {
9307 mbedtls_mpi_free( &conf->dhm_P );
9308 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009309 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009310 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009311
9312 return( 0 );
9313}
Hanno Becker470a8c42017-10-04 15:28:46 +01009314#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009315
Hanno Beckera90658f2017-10-04 15:29:08 +01009316int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9317 const unsigned char *dhm_P, size_t P_len,
9318 const unsigned char *dhm_G, size_t G_len )
9319{
9320 int ret;
9321
9322 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9323 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9324 {
9325 mbedtls_mpi_free( &conf->dhm_P );
9326 mbedtls_mpi_free( &conf->dhm_G );
9327 return( ret );
9328 }
9329
9330 return( 0 );
9331}
Paul Bakker5121ce52009-01-03 21:22:43 +00009332
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009333int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009334{
9335 int ret;
9336
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009337 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9338 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9339 {
9340 mbedtls_mpi_free( &conf->dhm_P );
9341 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009342 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009343 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009344
9345 return( 0 );
9346}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009347#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009348
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009349#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9350/*
9351 * Set the minimum length for Diffie-Hellman parameters
9352 */
9353void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9354 unsigned int bitlen )
9355{
9356 conf->dhm_min_bitlen = bitlen;
9357}
9358#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9359
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009360#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009361/*
9362 * Set allowed/preferred hashes for handshake signatures
9363 */
9364void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9365 const int *hashes )
9366{
Hanno Becker56595f42019-06-19 16:31:38 +01009367#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009368 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01009369#else
9370 ((void) conf);
9371 ((void) hashes);
9372#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009373}
Hanno Becker947194e2017-04-07 13:25:49 +01009374#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009375
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009376#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01009377#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009378/*
9379 * Set the allowed elliptic curves
9380 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009381void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009382 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009383{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009384 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009385}
Hanno Beckerc1096e72019-06-19 12:30:41 +01009386#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01009387#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009388
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009389#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009391{
Hanno Becker947194e2017-04-07 13:25:49 +01009392 /* Initialize to suppress unnecessary compiler warning */
9393 size_t hostname_len = 0;
9394
9395 /* Check if new hostname is valid before
9396 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009397 if( hostname != NULL )
9398 {
9399 hostname_len = strlen( hostname );
9400
9401 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9402 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9403 }
9404
9405 /* Now it's clear that we will overwrite the old hostname,
9406 * so we can free it safely */
9407
9408 if( ssl->hostname != NULL )
9409 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009410 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009411 mbedtls_free( ssl->hostname );
9412 }
9413
9414 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009415
Paul Bakker5121ce52009-01-03 21:22:43 +00009416 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009417 {
9418 ssl->hostname = NULL;
9419 }
9420 else
9421 {
9422 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009423 if( ssl->hostname == NULL )
9424 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009425
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03009426 /* Not using more secure mbedtls_platform_memcpy as hostname is public in initial handshake */
9427 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009428
Hanno Becker947194e2017-04-07 13:25:49 +01009429 ssl->hostname[hostname_len] = '\0';
9430 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009431
9432 return( 0 );
9433}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009434#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009435
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009436#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009437void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009438 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009439 const unsigned char *, size_t),
9440 void *p_sni )
9441{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009442 conf->f_sni = f_sni;
9443 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009444}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009445#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009447#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009448int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009449{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009450 size_t cur_len, tot_len;
9451 const char **p;
9452
9453 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009454 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9455 * MUST NOT be truncated."
9456 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009457 */
9458 tot_len = 0;
9459 for( p = protos; *p != NULL; p++ )
9460 {
9461 cur_len = strlen( *p );
9462 tot_len += cur_len;
9463
9464 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009465 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009466 }
9467
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009468 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009469
9470 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009471}
9472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009473const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009474{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009475 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009476}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009478
Hanno Becker33b9b252019-07-05 11:23:25 +01009479#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9480 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9481void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9482 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009483{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009484 conf->max_major_ver = major;
9485 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009486}
Hanno Becker33b9b252019-07-05 11:23:25 +01009487#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9488 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009489
Hanno Becker33b9b252019-07-05 11:23:25 +01009490#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9491 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9492void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9493 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009494{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009495 conf->min_major_ver = major;
9496 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009497}
Hanno Becker33b9b252019-07-05 11:23:25 +01009498#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9499 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009502void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009503{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009504 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009505}
9506#endif
9507
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009508#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009509void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9510 char cert_req_ca_list )
9511{
9512 conf->cert_req_ca_list = cert_req_ca_list;
9513}
9514#endif
9515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009516#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009517void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009518{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009519 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009520}
9521#endif
9522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009523#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009524#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009525void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009526{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009527 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009528}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009529#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9530#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009531void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009532 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009533{
9534 conf->enforce_extended_master_secret = ems_enf;
9535}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009536#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009537#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009538
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009539#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009540void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009541{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009542 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009543}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009544#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009547int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009548{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009550 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009553 }
9554
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009555 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009556
9557 return( 0 );
9558}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009559#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009562void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009563{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009564 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009565}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009568#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009569void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009570{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009571 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009572}
9573#endif
9574
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009575#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009576void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009577{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009578 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009579}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009580#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009583void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009584{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009585 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009586}
9587
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009588void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009589{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009590 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009591}
9592
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009593void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009594 const unsigned char period[8] )
9595{
Teppo Järvelin91d79382019-10-02 09:09:31 +03009596 mbedtls_platform_memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009597}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009598#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009600#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009601#if defined(MBEDTLS_SSL_CLI_C)
9602void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009603{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009604 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009605}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009606#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009607
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009608#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009609void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9610 mbedtls_ssl_ticket_write_t *f_ticket_write,
9611 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9612 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009613{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009614 conf->f_ticket_write = f_ticket_write;
9615 conf->f_ticket_parse = f_ticket_parse;
9616 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009617}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009618#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009619#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009620
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009621#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9622void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9623 mbedtls_ssl_export_keys_t *f_export_keys,
9624 void *p_export_keys )
9625{
9626 conf->f_export_keys = f_export_keys;
9627 conf->p_export_keys = p_export_keys;
9628}
9629#endif
9630
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009631#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009632void mbedtls_ssl_conf_async_private_cb(
9633 mbedtls_ssl_config *conf,
9634 mbedtls_ssl_async_sign_t *f_async_sign,
9635 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9636 mbedtls_ssl_async_resume_t *f_async_resume,
9637 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009638 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009639{
9640 conf->f_async_sign_start = f_async_sign;
9641 conf->f_async_decrypt_start = f_async_decrypt;
9642 conf->f_async_resume = f_async_resume;
9643 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009644 conf->p_async_config_data = async_config_data;
9645}
9646
Gilles Peskine8f97af72018-04-26 11:46:10 +02009647void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9648{
9649 return( conf->p_async_config_data );
9650}
9651
Gilles Peskine1febfef2018-04-30 11:54:39 +02009652void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009653{
9654 if( ssl->handshake == NULL )
9655 return( NULL );
9656 else
9657 return( ssl->handshake->user_async_ctx );
9658}
9659
Gilles Peskine1febfef2018-04-30 11:54:39 +02009660void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009661 void *ctx )
9662{
9663 if( ssl->handshake != NULL )
9664 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009665}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009666#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009667
Paul Bakker5121ce52009-01-03 21:22:43 +00009668/*
9669 * SSL get accessors
9670 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009671size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009672{
9673 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9674}
9675
Hanno Becker8b170a02017-10-10 11:51:19 +01009676int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9677{
9678 /*
9679 * Case A: We're currently holding back
9680 * a message for further processing.
9681 */
9682
9683 if( ssl->keep_current_message == 1 )
9684 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009685 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009686 return( 1 );
9687 }
9688
9689 /*
9690 * Case B: Further records are pending in the current datagram.
9691 */
9692
9693#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009694 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009695 ssl->in_left > ssl->next_record_offset )
9696 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009697 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009698 return( 1 );
9699 }
9700#endif /* MBEDTLS_SSL_PROTO_DTLS */
9701
9702 /*
9703 * Case C: A handshake message is being processed.
9704 */
9705
Hanno Becker8b170a02017-10-10 11:51:19 +01009706 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9707 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009708 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009709 return( 1 );
9710 }
9711
9712 /*
9713 * Case D: An application data message is being processed
9714 */
9715 if( ssl->in_offt != NULL )
9716 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009717 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009718 return( 1 );
9719 }
9720
9721 /*
9722 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009723 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009724 * we implement support for multiple alerts in single records.
9725 */
9726
9727 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9728 return( 0 );
9729}
9730
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009731uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009732{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009733 if( ssl->session != NULL )
9734 return( ssl->session->verify_result );
9735
9736 if( ssl->session_negotiate != NULL )
9737 return( ssl->session_negotiate->verify_result );
9738
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009739 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009740}
9741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009742const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009743{
Hanno Beckere02758c2019-06-26 15:31:31 +01009744 int suite;
9745
Paul Bakker926c8e42013-03-06 10:23:34 +01009746 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009747 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009748
Hanno Beckere02758c2019-06-26 15:31:31 +01009749 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9750 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009751}
9752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009753const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009754{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009755#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009756 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009757 {
Hanno Becker2881d802019-05-22 14:44:53 +01009758 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009760 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009761 return( "DTLSv1.0" );
9762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009763 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009764 return( "DTLSv1.2" );
9765
9766 default:
9767 return( "unknown (DTLS)" );
9768 }
9769 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009770 MBEDTLS_SSL_TRANSPORT_ELSE
9771#endif /* MBEDTLS_SSL_PROTO_DTLS */
9772#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009773 {
Hanno Becker2881d802019-05-22 14:44:53 +01009774 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009775 {
9776 case MBEDTLS_SSL_MINOR_VERSION_0:
9777 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009778
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009779 case MBEDTLS_SSL_MINOR_VERSION_1:
9780 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009781
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009782 case MBEDTLS_SSL_MINOR_VERSION_2:
9783 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009784
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009785 case MBEDTLS_SSL_MINOR_VERSION_3:
9786 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009787
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009788 default:
9789 return( "unknown" );
9790 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009791 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009792#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009793}
9794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009795int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009796{
Hanno Becker3136ede2018-08-17 15:28:19 +01009797 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009798 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009799
Hanno Becker43395762019-05-03 14:46:38 +01009800 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9801
Hanno Becker78640902018-08-13 16:35:15 +01009802 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009803 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009805#if defined(MBEDTLS_ZLIB_SUPPORT)
9806 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9807 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009808#endif
9809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009810 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009811 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009812#if defined(MBEDTLS_GCM_C) || \
9813 defined(MBEDTLS_CCM_C) || \
9814 defined(MBEDTLS_CHACHAPOLY_C)
9815#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009816 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009817#endif
9818#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009820#endif
9821#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009822 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009823#endif
9824 transform_expansion =
9825 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009826 break;
9827
Hanno Beckera9d5c452019-07-25 16:47:12 +01009828#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9829 MBEDTLS_CHACHAPOLY_C */
9830
9831#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9832 case MBEDTLS_MODE_STREAM:
9833 transform_expansion = transform->maclen;
9834 break;
9835#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9836
9837#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009838 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009839 {
9840 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009841
9842 block_size = mbedtls_cipher_get_block_size(
9843 &transform->cipher_ctx_enc );
9844
Hanno Becker3136ede2018-08-17 15:28:19 +01009845 /* Expansion due to the addition of the MAC. */
9846 transform_expansion += transform->maclen;
9847
9848 /* Expansion due to the addition of CBC padding;
9849 * Theoretically up to 256 bytes, but we never use
9850 * more than the block size of the underlying cipher. */
9851 transform_expansion += block_size;
9852
9853 /* For TLS 1.1 or higher, an explicit IV is added
9854 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009855#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009856 if( mbedtls_ssl_ver_geq(
9857 mbedtls_ssl_get_minor_ver( ssl ),
9858 MBEDTLS_SSL_MINOR_VERSION_2 ) )
9859 {
Hanno Becker3136ede2018-08-17 15:28:19 +01009860 transform_expansion += block_size;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009861 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01009862#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009863
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009864 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009865 }
9866#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009867
9868 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009870 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009871 }
9872
Hanno Beckera5a2b082019-05-15 14:03:01 +01009873#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009874 if( transform->out_cid_len != 0 )
9875 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009876#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009877
Hanno Becker43395762019-05-03 14:46:38 +01009878 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009879}
9880
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009881#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9882size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9883{
9884 size_t max_len;
9885
9886 /*
9887 * Assume mfl_code is correct since it was checked when set
9888 */
Angus Grattond8213d02016-05-25 20:56:48 +10009889 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009890
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009891 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009892 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009893 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009894 {
Angus Grattond8213d02016-05-25 20:56:48 +10009895 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009896 }
9897
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009898 /* During a handshake, use the value being negotiated */
9899 if( ssl->session_negotiate != NULL &&
9900 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9901 {
9902 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9903 }
9904
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009905 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009906}
9907#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9908
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009909#if defined(MBEDTLS_SSL_PROTO_DTLS)
9910static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9911{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009912 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009913 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009914 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9915 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009916 return( 0 );
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009917
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009918 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9919 return( ssl->mtu );
9920
9921 if( ssl->mtu == 0 )
9922 return( ssl->handshake->mtu );
9923
9924 return( ssl->mtu < ssl->handshake->mtu ?
9925 ssl->mtu : ssl->handshake->mtu );
9926}
9927#endif /* MBEDTLS_SSL_PROTO_DTLS */
9928
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009929int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9930{
9931 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9932
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009933#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9934 !defined(MBEDTLS_SSL_PROTO_DTLS)
9935 (void) ssl;
9936#endif
9937
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009938#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9939 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9940
9941 if( max_len > mfl )
9942 max_len = mfl;
9943#endif
9944
9945#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009946 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009947 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009948 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009949 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9950 const size_t overhead = (size_t) ret;
9951
9952 if( ret < 0 )
9953 return( ret );
9954
9955 if( mtu <= overhead )
9956 {
9957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9958 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9959 }
9960
9961 if( max_len > mtu - overhead )
9962 max_len = mtu - overhead;
9963 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009964#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009965
Hanno Becker0defedb2018-08-10 12:35:02 +01009966#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9967 !defined(MBEDTLS_SSL_PROTO_DTLS)
9968 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009969#endif
9970
9971 return( (int) max_len );
9972}
9973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009974#if defined(MBEDTLS_X509_CRT_PARSE_C)
9975const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009976{
9977 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009978 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009979
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009980#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009981 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009982#else
9983 return( NULL );
9984#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009985}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009986#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009988#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009989int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9990 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009991{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009992 if( ssl == NULL ||
9993 dst == NULL ||
9994 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009995 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009997 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009998 }
9999
Hanno Becker58fccf22019-02-06 14:30:46 +000010000 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010001}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010002#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010003
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +020010004const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
10005{
10006 if( ssl == NULL )
10007 return( NULL );
10008
10009 return( ssl->session );
10010}
10011
Paul Bakker5121ce52009-01-03 21:22:43 +000010012/*
Hanno Beckerb5352f02019-05-16 12:39:07 +010010013 * Define ticket header determining Mbed TLS version
10014 * and structure of the ticket.
10015 */
10016
Hanno Becker41527622019-05-16 12:50:45 +010010017/*
Hanno Becker26829e92019-05-28 14:30:45 +010010018 * Define bitflag determining compile-time settings influencing
10019 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +010010020 */
10021
Hanno Becker26829e92019-05-28 14:30:45 +010010022#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010023#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +010010024#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010025#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +010010026#endif /* MBEDTLS_HAVE_TIME */
10027
10028#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010029#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +010010030#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010031#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +010010032#endif /* MBEDTLS_X509_CRT_PARSE_C */
10033
10034#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010035#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +010010036#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010037#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +010010038#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
10039
10040#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010041#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +010010042#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010043#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +010010044#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
10045
10046#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010047#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +010010048#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010049#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +010010050#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
10051
10052#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010053#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +010010054#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010055#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +010010056#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
10057
Hanno Becker41527622019-05-16 12:50:45 +010010058#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10059#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
10060#else
10061#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
10062#endif /* MBEDTLS_SSL_SESSION_TICKETS */
10063
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010064#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10065#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
10066#else
10067#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
10068#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10069
Hanno Becker88440552019-07-03 14:16:13 +010010070#if defined(MBEDTLS_ZLIB_SUPPORT)
10071#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
10072#else
10073#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
10074#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10075
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010076#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
10077#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
10078#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
10079#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
10080#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
10081#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
10082#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010083#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +010010084#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010085
Hanno Becker26829e92019-05-28 14:30:45 +010010086#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010087 ( (uint16_t) ( \
10088 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
10089 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
10090 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
10091 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
10092 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
10093 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010094 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +010010095 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010096 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +010010097
Hanno Becker557fe9f2019-05-16 12:41:07 +010010098static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +010010099 MBEDTLS_VERSION_MAJOR,
10100 MBEDTLS_VERSION_MINOR,
10101 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +010010102 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
10103 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +010010104};
Hanno Beckerb5352f02019-05-16 12:39:07 +010010105
10106/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010107 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010108 * (in the presentation language of TLS, RFC 8446 section 3)
10109 *
Hanno Becker26829e92019-05-28 14:30:45 +010010110 * opaque mbedtls_version[3]; // major, minor, patch
10111 * opaque session_format[2]; // version-specific 16-bit field determining
10112 * // the format of the remaining
10113 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +010010114 *
10115 * Note: When updating the format, remember to keep
10116 * these version+format bytes.
10117 *
Hanno Becker7bf77102019-06-04 09:43:16 +010010118 * // In this version, `session_format` determines
10119 * // the setting of those compile-time
10120 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +010010121 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010122 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +010010123 * uint8 ciphersuite[2]; // defined by the standard
10124 * uint8 compression; // 0 or 1
10125 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010126 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +010010127 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010128 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +010010129 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
10130 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
10131 * case disabled: uint8_t peer_cert_digest_type;
10132 * opaque peer_cert_digest<0..2^8-1>;
10133 * }
Hanno Becker26829e92019-05-28 14:30:45 +010010134 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010135 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +010010136 * uint8 mfl_code; // up to 255 according to standard
10137 * uint8 trunc_hmac; // 0 or 1
10138 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010139 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010140 * The order is the same as in the definition of the structure, except
10141 * verify_result is put before peer_cert so that all mandatory fields come
10142 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010143 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010144static int ssl_session_save( const mbedtls_ssl_session *session,
10145 unsigned char omit_header,
10146 unsigned char *buf,
10147 size_t buf_len,
10148 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010149{
10150 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010151 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010152#if defined(MBEDTLS_HAVE_TIME)
10153 uint64_t start;
10154#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010155#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010156#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010157 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010158#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +000010159#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010160
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010161 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010162 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010163 /*
10164 * Add version identifier
10165 */
10166
10167 used += sizeof( ssl_serialized_session_header );
10168
10169 if( used <= buf_len )
10170 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010171 mbedtls_platform_memcpy( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010172 sizeof( ssl_serialized_session_header ) );
10173 p += sizeof( ssl_serialized_session_header );
10174 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010175 }
10176
10177 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010178 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010179 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010180#if defined(MBEDTLS_HAVE_TIME)
10181 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010182
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010183 if( used <= buf_len )
10184 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010185 start = (uint64_t) session->start;
10186
10187 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
10188 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
10189 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
10190 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
10191 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
10192 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
10193 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
10194 *p++ = (unsigned char)( ( start ) & 0xFF );
10195 }
10196#endif /* MBEDTLS_HAVE_TIME */
10197
10198 /*
10199 * Basic mandatory fields
10200 */
Hanno Becker88440552019-07-03 14:16:13 +010010201 {
10202 size_t const ciphersuite_len = 2;
10203#if defined(MBEDTLS_ZLIB_SUPPORT)
10204 size_t const compression_len = 1;
10205#else
10206 size_t const compression_len = 0;
10207#endif
10208 size_t const id_len_len = 1;
10209 size_t const id_len = 32;
10210 size_t const master_len = 48;
10211 size_t const verif_result_len = 4;
10212
10213 size_t const basic_len =
10214 ciphersuite_len +
10215 compression_len +
10216 id_len_len +
10217 id_len +
10218 master_len +
10219 verif_result_len;
10220
10221 used += basic_len;
10222 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010223
10224 if( used <= buf_len )
10225 {
Hanno Beckere02758c2019-06-26 15:31:31 +010010226 const int ciphersuite =
10227 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010228 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010229
Hanno Becker88440552019-07-03 14:16:13 +010010230#if defined(MBEDTLS_ZLIB_SUPPORT)
10231 *p++ = (unsigned char)(
10232 mbedtls_ssl_session_get_compression( session ) );
10233#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010234
10235 *p++ = (unsigned char)( session->id_len & 0xFF );
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010236 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10237 memcpy( p, session->id, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010238 p += 32;
10239
Teppo Järvelin91d79382019-10-02 09:09:31 +030010240 mbedtls_platform_memcpy( p, session->master, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010241 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010242 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010243 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010244
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010245 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010246 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010247 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010248#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010249#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010250 if( session->peer_cert == NULL )
10251 cert_len = 0;
10252 else
10253 cert_len = session->peer_cert->raw.len;
10254
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010255 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010256
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010257 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010258 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010259 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010260
10261 if( session->peer_cert != NULL )
10262 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010263 mbedtls_platform_memcpy( p, session->peer_cert->raw.p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010264 p += cert_len;
10265 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010266 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010267
Hanno Becker5882dd02019-06-06 16:25:57 +010010268#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010269 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010270 if( session->peer_cert_digest != NULL )
10271 {
10272 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
10273 if( used <= buf_len )
10274 {
10275 *p++ = (unsigned char) session->peer_cert_digest_type;
10276 *p++ = (unsigned char) session->peer_cert_digest_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +030010277 mbedtls_platform_memcpy( p, session->peer_cert_digest,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010278 session->peer_cert_digest_len );
10279 p += session->peer_cert_digest_len;
10280 }
10281 }
10282 else
10283 {
10284 used += 2;
10285 if( used <= buf_len )
10286 {
10287 *p++ = (unsigned char) MBEDTLS_MD_NONE;
10288 *p++ = 0;
10289 }
10290 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010291#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010292#endif /* MBEDTLS_X509_CRT_PARSE_C */
10293
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010294 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010295 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010296 */
10297#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010298 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010299
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010300 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010301 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010302 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010303
10304 if( session->ticket != NULL )
10305 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010306 mbedtls_platform_memcpy( p, session->ticket, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010307 p += session->ticket_len;
10308 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010309
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010310 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010311 }
10312#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10313
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010314 /*
10315 * Misc extension-related info
10316 */
10317#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10318 used += 1;
10319
10320 if( used <= buf_len )
10321 *p++ = session->mfl_code;
10322#endif
10323
10324#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10325 used += 1;
10326
10327 if( used <= buf_len )
10328 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
10329#endif
10330
10331#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10332 used += 1;
10333
10334 if( used <= buf_len )
10335 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
10336#endif
10337
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010338 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010339 *olen = used;
10340
10341 if( used > buf_len )
10342 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010343
10344 return( 0 );
10345}
10346
10347/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010348 * Public wrapper for ssl_session_save()
10349 */
10350int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
10351 unsigned char *buf,
10352 size_t buf_len,
10353 size_t *olen )
10354{
10355 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
10356}
10357
10358/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010359 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010360 *
10361 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010362 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010363 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010364static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010365 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010366 const unsigned char *buf,
10367 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010368{
10369 const unsigned char *p = buf;
10370 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +010010371 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010372#if defined(MBEDTLS_HAVE_TIME)
10373 uint64_t start;
10374#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010375#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010376#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010377 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +000010378#endif
10379#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010380
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010381 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010382 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010383 /*
10384 * Check version identifier
10385 */
10386
10387 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
10388 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10389
Teppo Järvelin0efac532019-10-04 13:21:08 +030010390 // use regular memcmp as session header is public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010391 if( memcmp( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010392 sizeof( ssl_serialized_session_header ) ) != 0 )
10393 {
10394 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10395 }
10396 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +010010397 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010398
10399 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010400 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010401 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010402#if defined(MBEDTLS_HAVE_TIME)
10403 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010404 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10405
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010406 start = ( (uint64_t) p[0] << 56 ) |
10407 ( (uint64_t) p[1] << 48 ) |
10408 ( (uint64_t) p[2] << 40 ) |
10409 ( (uint64_t) p[3] << 32 ) |
10410 ( (uint64_t) p[4] << 24 ) |
10411 ( (uint64_t) p[5] << 16 ) |
10412 ( (uint64_t) p[6] << 8 ) |
10413 ( (uint64_t) p[7] );
10414 p += 8;
10415
10416 session->start = (time_t) start;
10417#endif /* MBEDTLS_HAVE_TIME */
10418
10419 /*
10420 * Basic mandatory fields
10421 */
Hanno Becker88440552019-07-03 14:16:13 +010010422 {
10423 size_t const ciphersuite_len = 2;
10424#if defined(MBEDTLS_ZLIB_SUPPORT)
10425 size_t const compression_len = 1;
10426#else
10427 size_t const compression_len = 0;
10428#endif
10429 size_t const id_len_len = 1;
10430 size_t const id_len = 32;
10431 size_t const master_len = 48;
10432 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010433
Hanno Becker88440552019-07-03 14:16:13 +010010434 size_t const basic_len =
10435 ciphersuite_len +
10436 compression_len +
10437 id_len_len +
10438 id_len +
10439 master_len +
10440 verif_result_len;
10441
10442 if( basic_len > (size_t)( end - p ) )
10443 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10444 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010445
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010446 ciphersuite = (int)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010447 p += 2;
10448
Hanno Becker73f4cb12019-06-27 13:51:07 +010010449#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010450 session->ciphersuite = ciphersuite;
10451#else
10452 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010453 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010454 {
10455 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10456 }
10457#endif
10458
Hanno Becker88440552019-07-03 14:16:13 +010010459#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010460 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010461#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010462
10463 session->id_len = *p++;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010464 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10465 memcpy( session->id, p, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010466 p += 32;
10467
Teppo Järvelin91d79382019-10-02 09:09:31 +030010468 mbedtls_platform_memcpy( session->master, p, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010469 p += 48;
10470
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010471 session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010472 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010473
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010474 /* Immediately clear invalid pointer values that have been read, in case
10475 * we exit early before we replaced them with valid ones. */
10476#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010477#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010478 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010479#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010480 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010481#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010482#endif
10483#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10484 session->ticket = NULL;
10485#endif
10486
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010487 /*
10488 * Peer certificate
10489 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010490#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010491#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010492 if( 3 > (size_t)( end - p ) )
10493 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10494
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010495 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10496
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010497 p += 3;
10498
10499 if( cert_len == 0 )
10500 {
10501 session->peer_cert = NULL;
10502 }
10503 else
10504 {
10505 int ret;
10506
10507 if( cert_len > (size_t)( end - p ) )
10508 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10509
10510 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10511
10512 if( session->peer_cert == NULL )
10513 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10514
10515 mbedtls_x509_crt_init( session->peer_cert );
10516
10517 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10518 p, cert_len ) ) != 0 )
10519 {
10520 mbedtls_x509_crt_free( session->peer_cert );
10521 mbedtls_free( session->peer_cert );
10522 session->peer_cert = NULL;
10523 return( ret );
10524 }
10525
10526 p += cert_len;
10527 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010528#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010529 /* Deserialize CRT digest from the end of the ticket. */
10530 if( 2 > (size_t)( end - p ) )
10531 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10532
10533 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10534 session->peer_cert_digest_len = (size_t) *p++;
10535
10536 if( session->peer_cert_digest_len != 0 )
10537 {
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010538 mbedtls_md_handle_t md_info =
Hanno Becker2326d202019-06-06 14:54:55 +010010539 mbedtls_md_info_from_type( session->peer_cert_digest_type );
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010540 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Hanno Becker2326d202019-06-06 14:54:55 +010010541 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10542 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10543 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10544
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010545 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10546 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10547
10548 session->peer_cert_digest =
10549 mbedtls_calloc( 1, session->peer_cert_digest_len );
10550 if( session->peer_cert_digest == NULL )
10551 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10552
Teppo Järvelin91d79382019-10-02 09:09:31 +030010553 mbedtls_platform_memcpy( session->peer_cert_digest, p,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010554 session->peer_cert_digest_len );
10555 p += session->peer_cert_digest_len;
10556 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010557#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010558#endif /* MBEDTLS_X509_CRT_PARSE_C */
10559
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010560 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010561 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010562 */
10563#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10564 if( 3 > (size_t)( end - p ) )
10565 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10566
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010567 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010568 p += 3;
10569
10570 if( session->ticket_len != 0 )
10571 {
10572 if( session->ticket_len > (size_t)( end - p ) )
10573 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10574
10575 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10576 if( session->ticket == NULL )
10577 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10578
Teppo Järvelin91d79382019-10-02 09:09:31 +030010579 mbedtls_platform_memcpy( session->ticket, p, session->ticket_len );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010580 p += session->ticket_len;
10581 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010582
10583 if( 4 > (size_t)( end - p ) )
10584 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10585
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010586 session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010587 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010588#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10589
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010590 /*
10591 * Misc extension-related info
10592 */
10593#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10594 if( 1 > (size_t)( end - p ) )
10595 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10596
10597 session->mfl_code = *p++;
10598#endif
10599
10600#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10601 if( 1 > (size_t)( end - p ) )
10602 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10603
10604 session->trunc_hmac = *p++;
10605#endif
10606
10607#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10608 if( 1 > (size_t)( end - p ) )
10609 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10610
10611 session->encrypt_then_mac = *p++;
10612#endif
10613
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010614 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010615 if( p != end )
10616 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10617
10618 return( 0 );
10619}
10620
10621/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010622 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010623 */
10624int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10625 const unsigned char *buf,
10626 size_t len )
10627{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010628 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010629
10630 if( ret != 0 )
10631 mbedtls_ssl_session_free( session );
10632
10633 return( ret );
10634}
10635
10636/*
Paul Bakker1961b702013-01-25 14:49:24 +010010637 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010639int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010641 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010642
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010643 if( ssl == NULL || ssl->conf == NULL )
10644 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010646#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010647 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010648 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010649#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010650#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010651 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010652 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010653#endif
10654
Hanno Beckerb82350b2019-07-26 07:24:05 +010010655 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010656 return( ret );
10657}
10658
10659/*
10660 * Perform the SSL handshake
10661 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010662int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010663{
10664 int ret = 0;
10665
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010666 if( ssl == NULL || ssl->conf == NULL )
10667 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010671 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010673 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010674
10675 if( ret != 0 )
10676 break;
10677 }
10678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010680
10681 return( ret );
10682}
10683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010684#if defined(MBEDTLS_SSL_RENEGOTIATION)
10685#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010686/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010687 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010689static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010690{
10691 int ret;
10692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010694
10695 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010696 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10697 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010698
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010699 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010700 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010701 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010702 return( ret );
10703 }
10704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010706
10707 return( 0 );
10708}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010709#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010710
10711/*
10712 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010713 * - any side: calling mbedtls_ssl_renegotiate(),
10714 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10715 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010716 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010717 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010718 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010720static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010721{
10722 int ret;
10723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010725
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010726 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10727 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010728
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010729 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10730 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010731#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010732 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010733 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010734 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010735 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10736 MBEDTLS_SSL_IS_SERVER )
10737 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010738 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010739 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010740 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010741 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010742 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010743 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010744 }
10745#endif
10746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010747 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10748 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010750 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010752 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010753 return( ret );
10754 }
10755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010757
10758 return( 0 );
10759}
10760
10761/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010762 * Renegotiate current connection on client,
10763 * or request renegotiation on server
10764 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010765int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010766{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010767 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010768
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010769 if( ssl == NULL || ssl->conf == NULL )
10770 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010772#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010773 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010774 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010776 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10777 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010779 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010780
10781 /* Did we already try/start sending HelloRequest? */
10782 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010783 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010784
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010785 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010786 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010787#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010789#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010790 /*
10791 * On client, either start the renegotiation process or,
10792 * if already in progress, continue the handshake
10793 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010794 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010796 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10797 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010798
10799 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010801 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010802 return( ret );
10803 }
10804 }
10805 else
10806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010807 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010809 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010810 return( ret );
10811 }
10812 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010813#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010814
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010815 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010816}
10817
10818/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010819 * Check record counters and renegotiate if they're above the limit.
10820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010821static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010822{
Andres AG2196c7f2016-12-15 17:01:16 +000010823 size_t ep_len = ssl_ep_len( ssl );
10824 int in_ctr_cmp;
10825 int out_ctr_cmp;
10826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010827 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10828 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010829 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010830 {
10831 return( 0 );
10832 }
10833
Teppo Järvelin0efac532019-10-04 13:21:08 +030010834 // use regular memcmp as counters are public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010835 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010836 ssl->conf->renego_period + ep_len, 8 - ep_len );
Teppo Järvelin650343c2019-10-03 15:36:59 +030010837 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010838 ssl->conf->renego_period + ep_len, 8 - ep_len );
10839
10840 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010841 {
10842 return( 0 );
10843 }
10844
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010846 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010847}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010848#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010849
10850/*
10851 * Receive application data decrypted from the SSL layer
10852 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010853int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010854{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010855 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010856 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010857
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010858 if( ssl == NULL || ssl->conf == NULL )
10859 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010863#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010864 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010866 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010867 return( ret );
10868
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010869 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010870 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010871 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010872 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010873 return( ret );
10874 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010875 }
10876#endif
10877
Hanno Becker4a810fb2017-05-24 16:27:30 +010010878 /*
10879 * Check if renegotiation is necessary and/or handshake is
10880 * in process. If yes, perform/continue, and fall through
10881 * if an unexpected packet is received while the client
10882 * is waiting for the ServerHello.
10883 *
10884 * (There is no equivalent to the last condition on
10885 * the server-side as it is not treated as within
10886 * a handshake while waiting for the ClientHello
10887 * after a renegotiation request.)
10888 */
10889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010890#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010891 ret = ssl_check_ctr_renegotiate( ssl );
10892 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10893 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010895 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010896 return( ret );
10897 }
10898#endif
10899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010900 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010902 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010903 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10904 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010907 return( ret );
10908 }
10909 }
10910
Hanno Beckere41158b2017-10-23 13:30:32 +010010911 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010912 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010913 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010914 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010915 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10916 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010917 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010918 ssl_set_timer( ssl,
10919 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010920 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010921
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 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010924 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10925 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010926
Hanno Becker4a810fb2017-05-24 16:27:30 +010010927 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10928 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010929 }
10930
10931 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010932 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010933 {
10934 /*
10935 * OpenSSL sends empty messages to randomize the IV
10936 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010937 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010939 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010940 return( 0 );
10941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010942 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010943 return( ret );
10944 }
10945 }
10946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010947 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010950
Hanno Becker4a810fb2017-05-24 16:27:30 +010010951 /*
10952 * - For client-side, expect SERVER_HELLO_REQUEST.
10953 * - For server-side, expect CLIENT_HELLO.
10954 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10955 */
10956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010957#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010958 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10959 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010960 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010961 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010964
10965 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010966#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010967 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010968 {
10969 continue;
10970 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010971 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010972#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010973#if defined(MBEDTLS_SSL_PROTO_TLS)
10974 {
10975 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10976 }
10977#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010978 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010979#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010980
Hanno Becker4a810fb2017-05-24 16:27:30 +010010981#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010982 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10983 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010984 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010987
10988 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010989#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010990 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010991 {
10992 continue;
10993 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010994 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010995#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010996#if defined(MBEDTLS_SSL_PROTO_TLS)
10997 {
10998 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10999 }
11000#endif
Paul Bakker48916f92012-09-16 19:57:18 +000011001 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010011002#endif /* MBEDTLS_SSL_SRV_C */
11003
Hanno Becker21df7f92017-10-17 11:03:26 +010011004#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010011005 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011006 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
11007 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010011008 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011009 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
11010 {
11011 /*
11012 * Accept renegotiation request
11013 */
Paul Bakker48916f92012-09-16 19:57:18 +000011014
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011015 /* DTLS clients need to know renego is server-initiated */
11016#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011017 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010011018 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11019 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011020 {
11021 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
11022 }
11023#endif
11024 ret = ssl_start_renegotiation( ssl );
11025 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
11026 ret != 0 )
11027 {
11028 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
11029 return( ret );
11030 }
11031 }
11032 else
Hanno Becker21df7f92017-10-17 11:03:26 +010011033#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000011034 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010011035 /*
11036 * Refuse renegotiation
11037 */
11038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011039 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011041#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010011042 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000011043 {
Gilles Peskine92e44262017-05-10 17:27:49 +020011044 /* SSLv3 does not have a "no_renegotiation" warning, so
11045 we send a fatal alert and abort the connection. */
11046 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11047 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
11048 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011049 }
11050 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011051#endif /* MBEDTLS_SSL_PROTO_SSL3 */
11052#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11053 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011054 if( mbedtls_ssl_ver_geq(
11055 mbedtls_ssl_get_minor_ver( ssl ),
11056 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011057 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010011058 ret = mbedtls_ssl_send_alert_message( ssl,
11059 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11060 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
11061 if( ret != 0 )
11062 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000011063 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020011064 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011065#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
11066 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020011067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
11069 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020011070 }
Paul Bakker48916f92012-09-16 19:57:18 +000011071 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011072
Hanno Becker90333da2017-10-10 11:27:13 +010011073 /* At this point, we don't know whether the renegotiation has been
11074 * completed or not. The cases to consider are the following:
11075 * 1) The renegotiation is complete. In this case, no new record
11076 * has been read yet.
11077 * 2) The renegotiation is incomplete because the client received
11078 * an application data record while awaiting the ServerHello.
11079 * 3) The renegotiation is incomplete because the client received
11080 * a non-handshake, non-application data message while awaiting
11081 * the ServerHello.
11082 * In each of these case, looping will be the proper action:
11083 * - For 1), the next iteration will read a new record and check
11084 * if it's application data.
11085 * - For 2), the loop condition isn't satisfied as application data
11086 * is present, hence continue is the same as break
11087 * - For 3), the loop condition is satisfied and read_record
11088 * will re-deliver the message that was held back by the client
11089 * when expecting the ServerHello.
11090 */
11091 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000011092 }
Hanno Becker21df7f92017-10-17 11:03:26 +010011093#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011094 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011095 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011096 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011097 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011098 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011101 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011102 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011103 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011104 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011105 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011106#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011108 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
11109 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010011112 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011113 }
11114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011115 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000011116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
11118 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000011119 }
11120
11121 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020011122
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011123 /* We're going to return something now, cancel timer,
11124 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011125 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011126 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011127
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020011128#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011129 /* If we requested renego but received AppData, resend HelloRequest.
11130 * Do it now, after setting in_offt, to avoid taking this branch
11131 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011132#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010011133 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11134 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011135 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011136 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011137 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011139 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011140 return( ret );
11141 }
11142 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011143#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010011144#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000011145 }
11146
11147 n = ( len < ssl->in_msglen )
11148 ? len : ssl->in_msglen;
11149
Teppo Järvelin91d79382019-10-02 09:09:31 +030011150 mbedtls_platform_memcpy( buf, ssl->in_offt, n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011151 ssl->in_msglen -= n;
11152
Teppo Järvelincafb6c92020-01-08 09:19:07 +020011153 // clear incoming data after it's copied to buffer
11154 mbedtls_platform_memset(ssl->in_offt, 0, n);
11155
Paul Bakker5121ce52009-01-03 21:22:43 +000011156 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010011157 {
11158 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000011159 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010011160 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011161 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011162 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010011163 {
Paul Bakker5121ce52009-01-03 21:22:43 +000011164 /* more data available */
11165 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011166 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011169
Paul Bakker23986e52011-04-24 08:57:21 +000011170 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011171}
11172
11173/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011174 * Send application data to be encrypted by the SSL layer, taking care of max
11175 * fragment length and buffer size.
11176 *
11177 * According to RFC 5246 Section 6.2.1:
11178 *
11179 * Zero-length fragments of Application data MAY be sent as they are
11180 * potentially useful as a traffic analysis countermeasure.
11181 *
11182 * Therefore, it is possible that the input message length is 0 and the
11183 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000011184 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011185static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011186 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000011187{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020011188 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
11189 const size_t max_len = (size_t) ret;
11190
11191 if( ret < 0 )
11192 {
11193 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
11194 return( ret );
11195 }
11196
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011197 if( len > max_len )
11198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011199#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011200 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011203 "maximum fragment length: %d > %d",
11204 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011205 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011206 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011207 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011208#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011209#if defined(MBEDTLS_SSL_PROTO_TLS)
11210 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011211 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011212 }
11213#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011214 }
Paul Bakker887bd502011-06-08 13:10:54 +000011215
Paul Bakker5121ce52009-01-03 21:22:43 +000011216 if( ssl->out_left != 0 )
11217 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011218 /*
11219 * The user has previously tried to send the data and
11220 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
11221 * written. In this case, we expect the high-level write function
11222 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
11223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011224 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011226 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011227 return( ret );
11228 }
11229 }
Paul Bakker887bd502011-06-08 13:10:54 +000011230 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000011231 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011232 /*
11233 * The user is trying to send a message the first time, so we need to
11234 * copy the data into the internal buffers and setup the data structure
11235 * to keep track of partial writes
11236 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011237 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011238 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Teppo Järvelin91d79382019-10-02 09:09:31 +030011239 mbedtls_platform_memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000011240
Hanno Becker67bc7c32018-08-06 11:33:50 +010011241 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000011242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011243 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000011244 return( ret );
11245 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011246 }
11247
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011248 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000011249}
11250
11251/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011252 * Write application data, doing 1/n-1 splitting if necessary.
11253 *
11254 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011255 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010011256 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011258#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011259static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011260 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011261{
11262 int ret;
11263
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011264 if( ssl->conf->cbc_record_splitting ==
11265 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011266 len <= 1 ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011267 mbedtls_ssl_ver_gt(
11268 mbedtls_ssl_get_minor_ver( ssl ),
11269 MBEDTLS_SSL_MINOR_VERSION_1 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011270 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
11271 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011272 {
11273 return( ssl_write_real( ssl, buf, len ) );
11274 }
11275
11276 if( ssl->split_done == 0 )
11277 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011278 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011279 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011280 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011281 }
11282
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011283 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
11284 return( ret );
11285 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011286
11287 return( ret + 1 );
11288}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011289#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011290
11291/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011292 * Write application data (public-facing wrapper)
11293 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011294int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011295{
11296 int ret;
11297
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011299
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011300 if( ssl == NULL || ssl->conf == NULL )
11301 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11302
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011303#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011304 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
11305 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011306 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011307 return( ret );
11308 }
11309#endif
11310
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011311 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011312 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011313 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011314 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020011315 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011316 return( ret );
11317 }
11318 }
11319
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011320#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011321 ret = ssl_write_split( ssl, buf, len );
11322#else
11323 ret = ssl_write_real( ssl, buf, len );
11324#endif
11325
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011327
11328 return( ret );
11329}
11330
11331/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011332 * Notify the peer that the connection is being closed
11333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011334int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011335{
11336 int ret;
11337
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011338 if( ssl == NULL || ssl->conf == NULL )
11339 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011342
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011343 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011344 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011346 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000011347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011348 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
11349 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11350 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011352 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011353 return( ret );
11354 }
11355 }
11356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011358
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011359 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000011360}
11361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011362void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000011363{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011364 if( transform == NULL )
11365 return;
11366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011367#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000011368 deflateEnd( &transform->ctx_deflate );
11369 inflateEnd( &transform->ctx_inflate );
11370#endif
11371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011372 mbedtls_cipher_free( &transform->cipher_ctx_enc );
11373 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020011374
Hanno Becker92231322018-01-03 15:32:51 +000011375#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011376 mbedtls_md_free( &transform->md_ctx_enc );
11377 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000011378#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011379
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011380 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011381}
11382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011383#if defined(MBEDTLS_X509_CRT_PARSE_C)
11384static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011385{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011386 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011387
11388 while( cur != NULL )
11389 {
11390 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011391 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011392 cur = next;
11393 }
11394}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011395#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011396
Hanno Becker0271f962018-08-16 13:23:47 +010011397#if defined(MBEDTLS_SSL_PROTO_DTLS)
11398
11399static void ssl_buffering_free( mbedtls_ssl_context *ssl )
11400{
11401 unsigned offset;
11402 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11403
11404 if( hs == NULL )
11405 return;
11406
Hanno Becker283f5ef2018-08-24 09:34:47 +010011407 ssl_free_buffered_record( ssl );
11408
Hanno Becker0271f962018-08-16 13:23:47 +010011409 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010011410 ssl_buffering_free_slot( ssl, offset );
11411}
11412
11413static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
11414 uint8_t slot )
11415{
11416 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11417 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010011418
11419 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
11420 return;
11421
Hanno Beckere605b192018-08-21 15:59:07 +010011422 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010011423 {
Hanno Beckere605b192018-08-21 15:59:07 +010011424 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010011425 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010011426 mbedtls_free( hs_buf->data );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020011427 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010011428 }
11429}
11430
11431#endif /* MBEDTLS_SSL_PROTO_DTLS */
11432
Gilles Peskine9b562d52018-04-25 20:32:43 +020011433void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011434{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011435 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11436
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011437 if( handshake == NULL )
11438 return;
11439
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011440#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11441 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11442 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011443 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011444 handshake->async_in_progress = 0;
11445 }
11446#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11447
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011448#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11449 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11450 mbedtls_md5_free( &handshake->fin_md5 );
11451 mbedtls_sha1_free( &handshake->fin_sha1 );
11452#endif
11453#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11454#if defined(MBEDTLS_SHA256_C)
11455 mbedtls_sha256_free( &handshake->fin_sha256 );
11456#endif
11457#if defined(MBEDTLS_SHA512_C)
11458 mbedtls_sha512_free( &handshake->fin_sha512 );
11459#endif
11460#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011462#if defined(MBEDTLS_DHM_C)
11463 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011464#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011465#if defined(MBEDTLS_ECDH_C)
11466 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011467#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011468#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011469 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011470#if defined(MBEDTLS_SSL_CLI_C)
11471 mbedtls_free( handshake->ecjpake_cache );
11472 handshake->ecjpake_cache = NULL;
11473 handshake->ecjpake_cache_len = 0;
11474#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011475#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011476
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011477#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11478 if( handshake->psk != NULL )
11479 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011480 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011481 mbedtls_free( handshake->psk );
11482 }
11483#endif
11484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011485#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11486 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011487 /*
11488 * Free only the linked list wrapper, not the keys themselves
11489 * since the belong to the SNI callback
11490 */
11491 if( handshake->sni_key_cert != NULL )
11492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011493 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011494
11495 while( cur != NULL )
11496 {
11497 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011498 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011499 cur = next;
11500 }
11501 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011502#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011503
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011504#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011505 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011506 if( handshake->ecrs_peer_cert != NULL )
11507 {
11508 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11509 mbedtls_free( handshake->ecrs_peer_cert );
11510 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011511#endif
11512
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011513#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11514 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11515 mbedtls_pk_free( &handshake->peer_pubkey );
11516#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011518#if defined(MBEDTLS_SSL_PROTO_DTLS)
11519 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011520 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011521 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011522#endif
11523
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011524 mbedtls_platform_zeroize( handshake,
11525 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011526}
11527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011528void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011529{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011530 if( session == NULL )
11531 return;
11532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011533#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011534 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011535#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011536
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011537#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011538 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011539#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011540
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011541 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011542}
11543
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011544#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011545
11546#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11547#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11548#else
11549#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11550#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11551
11552#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11553#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11554#else
11555#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11556#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11557
11558#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11559#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11560#else
11561#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11562#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11563
11564#if defined(MBEDTLS_SSL_ALPN)
11565#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11566#else
11567#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11568#endif /* MBEDTLS_SSL_ALPN */
11569
11570#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11571#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11572#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11573#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11574
11575#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11576 ( (uint32_t) ( \
11577 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11578 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11579 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11580 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11581 0u ) )
11582
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011583static unsigned char ssl_serialized_context_header[] = {
11584 MBEDTLS_VERSION_MAJOR,
11585 MBEDTLS_VERSION_MINOR,
11586 MBEDTLS_VERSION_PATCH,
11587 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11588 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011589 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11590 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11591 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011592};
11593
Paul Bakker5121ce52009-01-03 21:22:43 +000011594/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011595 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011596 *
11597 * The format of the serialized data is:
11598 * (in the presentation language of TLS, RFC 8446 section 3)
11599 *
11600 * // header
11601 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011602 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011603 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011604 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011605 * Note: When updating the format, remember to keep these
11606 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011607 *
11608 * // session sub-structure
11609 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11610 * // transform sub-structure
11611 * uint8 random[64]; // ServerHello.random+ClientHello.random
11612 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11613 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11614 * // fields from ssl_context
11615 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11616 * uint64 in_window_top; // DTLS: last validated record seq_num
11617 * uint64 in_window; // DTLS: bitmask for replay protection
11618 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11619 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11620 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11621 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11622 *
11623 * Note that many fields of the ssl_context or sub-structures are not
11624 * serialized, as they fall in one of the following categories:
11625 *
11626 * 1. forced value (eg in_left must be 0)
11627 * 2. pointer to dynamically-allocated memory (eg session, transform)
11628 * 3. value can be re-derived from other data (eg session keys from MS)
11629 * 4. value was temporary (eg content of input buffer)
11630 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011631 */
11632int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11633 unsigned char *buf,
11634 size_t buf_len,
11635 size_t *olen )
11636{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011637 unsigned char *p = buf;
11638 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011639 size_t session_len;
11640 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011641
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011642 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011643 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11644 * this function's documentation.
11645 *
11646 * These are due to assumptions/limitations in the implementation. Some of
11647 * them are likely to stay (no handshake in progress) some might go away
11648 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011649 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011650 /* The initial handshake must be over */
11651 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011653 if( ssl->handshake != NULL )
11654 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11655 /* Double-check that sub-structures are indeed ready */
11656 if( ssl->transform == NULL || ssl->session == NULL )
11657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11658 /* There must be no pending incoming or outgoing data */
11659 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11660 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11661 if( ssl->out_left != 0 )
11662 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11663 /* Protocol must be DLTS, not TLS */
11664 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11665 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11666 /* Version must be 1.2 */
11667 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11668 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11669 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11670 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11671 /* We must be using an AEAD ciphersuite */
11672 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11674 /* Renegotiation must not be enabled */
11675 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11676 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011677
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011678 /*
11679 * Version and format identifier
11680 */
11681 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011682
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011683 if( used <= buf_len )
11684 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011685 mbedtls_platform_memcpy( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011686 sizeof( ssl_serialized_context_header ) );
11687 p += sizeof( ssl_serialized_context_header );
11688 }
11689
11690 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011691 * Session (length + data)
11692 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011693 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011694 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11695 return( ret );
11696
11697 used += 4 + session_len;
11698 if( used <= buf_len )
11699 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011700 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011701
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011702 ret = ssl_session_save( ssl->session, 1,
11703 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011704 if( ret != 0 )
11705 return( ret );
11706
11707 p += session_len;
11708 }
11709
11710 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011711 * Transform
11712 */
11713 used += sizeof( ssl->transform->randbytes );
11714 if( used <= buf_len )
11715 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011716 mbedtls_platform_memcpy( p, ssl->transform->randbytes,
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011717 sizeof( ssl->transform->randbytes ) );
11718 p += sizeof( ssl->transform->randbytes );
11719 }
11720
11721#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11722 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11723 if( used <= buf_len )
11724 {
11725 *p++ = ssl->transform->in_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011726 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11727 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011728 p += ssl->transform->in_cid_len;
11729
11730 *p++ = ssl->transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011731 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11732 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011733 p += ssl->transform->out_cid_len;
11734 }
11735#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11736
11737 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011738 * Saved fields from top-level ssl_context structure
11739 */
11740#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11741 used += 4;
11742 if( used <= buf_len )
11743 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011744 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011745 }
11746#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11747
11748#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11749 used += 16;
11750 if( used <= buf_len )
11751 {
11752 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11753 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11754 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11755 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11756 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11757 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11758 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11759 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11760
11761 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11762 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11763 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11764 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11765 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11766 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11767 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11768 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11769 }
11770#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11771
11772#if defined(MBEDTLS_SSL_PROTO_DTLS)
11773 used += 1;
11774 if( used <= buf_len )
11775 {
11776 *p++ = ssl->disable_datagram_packing;
11777 }
11778#endif /* MBEDTLS_SSL_PROTO_DTLS */
11779
11780 used += 8;
11781 if( used <= buf_len )
11782 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011783 mbedtls_platform_memcpy( p, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011784 p += 8;
11785 }
11786
11787#if defined(MBEDTLS_SSL_PROTO_DTLS)
11788 used += 2;
11789 if( used <= buf_len )
11790 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011791 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011792 }
11793#endif /* MBEDTLS_SSL_PROTO_DTLS */
11794
11795#if defined(MBEDTLS_SSL_ALPN)
11796 {
11797 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011798 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011799 : 0;
11800
11801 used += 1 + alpn_len;
11802 if( used <= buf_len )
11803 {
11804 *p++ = alpn_len;
11805
11806 if( ssl->alpn_chosen != NULL )
11807 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011808 mbedtls_platform_memcpy( p, ssl->alpn_chosen, alpn_len );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011809 p += alpn_len;
11810 }
11811 }
11812 }
11813#endif /* MBEDTLS_SSL_ALPN */
11814
11815 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011816 * Done
11817 */
11818 *olen = used;
11819
11820 if( used > buf_len )
11821 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011822
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011823 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11824
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011825 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011826}
11827
11828/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011829 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011830 *
11831 * This internal version is wrapped by a public function that cleans up in
11832 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011833 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011834static int ssl_context_load( mbedtls_ssl_context *ssl,
11835 const unsigned char *buf,
11836 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011837{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011838 const unsigned char *p = buf;
11839 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011840 size_t session_len;
11841 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011842
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011843 /*
11844 * The context should have been freshly setup or reset.
11845 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011846 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011847 * renegotiating, or if the user mistakenly loaded a session first.)
11848 */
11849 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11850 ssl->session != NULL )
11851 {
11852 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11853 }
11854
11855 /*
11856 * We can't check that the config matches the initial one, but we can at
11857 * least check it matches the requirements for serializing.
11858 */
11859 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011860 mbedtls_ssl_ver_lt(
11861 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ),
11862 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11863 mbedtls_ssl_ver_gt(
11864 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ),
11865 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11866 mbedtls_ssl_ver_lt(
11867 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ),
11868 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
11869 mbedtls_ssl_ver_gt(
11870 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ),
11871 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011872 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011873 {
11874 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11875 }
11876
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011877 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11878
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011879 /*
11880 * Check version identifier
11881 */
11882 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11883 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11884
Teppo Järvelin650343c2019-10-03 15:36:59 +030011885 // use regular memcmp as header is not that critical
11886 if( memcmp( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011887 sizeof( ssl_serialized_context_header ) ) != 0 )
11888 {
11889 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11890 }
11891 p += sizeof( ssl_serialized_context_header );
11892
11893 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011894 * Session
11895 */
11896 if( (size_t)( end - p ) < 4 )
11897 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11898
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011899 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011900 p += 4;
11901
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011902 /* This has been allocated by ssl_handshake_init(), called by
11903 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11904 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011905 ssl->session_in = ssl->session;
11906 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011907 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011908
11909 if( (size_t)( end - p ) < session_len )
11910 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11911
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011912 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011913 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011914 {
11915 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011916 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011917 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011918
11919 p += session_len;
11920
11921 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011922 * Transform
11923 */
11924
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011925 /* This has been allocated by ssl_handshake_init(), called by
11926 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11927 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011928 ssl->transform_in = ssl->transform;
11929 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011930 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011931
11932 /* Read random bytes and populate structure */
11933 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11934 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11935
11936 ret = ssl_populate_transform( ssl->transform,
11937 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11938 ssl->session->master,
11939#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11940#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11941 ssl->session->encrypt_then_mac,
11942#endif
11943#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11944 ssl->session->trunc_hmac,
11945#endif
11946#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11947#if defined(MBEDTLS_ZLIB_SUPPORT)
11948 ssl->session->compression,
11949#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011950 p, /* currently pointing to randbytes */
11951 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11952 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11953 ssl );
11954 if( ret != 0 )
11955 return( ret );
11956
11957 p += sizeof( ssl->transform->randbytes );
11958
11959#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11960 /* Read connection IDs and store them */
11961 if( (size_t)( end - p ) < 1 )
11962 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11963
11964 ssl->transform->in_cid_len = *p++;
11965
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011966 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11968
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011969 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11970 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011971 p += ssl->transform->in_cid_len;
11972
11973 ssl->transform->out_cid_len = *p++;
11974
11975 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11976 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11977
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011978 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11979 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011980 p += ssl->transform->out_cid_len;
11981#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11982
11983 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011984 * Saved fields from top-level ssl_context structure
11985 */
11986#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11987 if( (size_t)( end - p ) < 4 )
11988 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11989
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011990 ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011991 p += 4;
11992#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11993
11994#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11995 if( (size_t)( end - p ) < 16 )
11996 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11997
11998 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11999 ( (uint64_t) p[1] << 48 ) |
12000 ( (uint64_t) p[2] << 40 ) |
12001 ( (uint64_t) p[3] << 32 ) |
12002 ( (uint64_t) p[4] << 24 ) |
12003 ( (uint64_t) p[5] << 16 ) |
12004 ( (uint64_t) p[6] << 8 ) |
12005 ( (uint64_t) p[7] );
12006 p += 8;
12007
12008 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
12009 ( (uint64_t) p[1] << 48 ) |
12010 ( (uint64_t) p[2] << 40 ) |
12011 ( (uint64_t) p[3] << 32 ) |
12012 ( (uint64_t) p[4] << 24 ) |
12013 ( (uint64_t) p[5] << 16 ) |
12014 ( (uint64_t) p[6] << 8 ) |
12015 ( (uint64_t) p[7] );
12016 p += 8;
12017#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
12018
12019#if defined(MBEDTLS_SSL_PROTO_DTLS)
12020 if( (size_t)( end - p ) < 1 )
12021 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12022
12023 ssl->disable_datagram_packing = *p++;
12024#endif /* MBEDTLS_SSL_PROTO_DTLS */
12025
12026 if( (size_t)( end - p ) < 8 )
12027 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12028
Teppo Järvelin91d79382019-10-02 09:09:31 +030012029 mbedtls_platform_memcpy( ssl->cur_out_ctr, p, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012030 p += 8;
12031
12032#if defined(MBEDTLS_SSL_PROTO_DTLS)
12033 if( (size_t)( end - p ) < 2 )
12034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030012035 ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012036 p += 2;
12037#endif /* MBEDTLS_SSL_PROTO_DTLS */
12038
12039#if defined(MBEDTLS_SSL_ALPN)
12040 {
12041 uint8_t alpn_len;
12042 const char **cur;
12043
12044 if( (size_t)( end - p ) < 1 )
12045 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12046
12047 alpn_len = *p++;
12048
12049 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
12050 {
12051 /* alpn_chosen should point to an item in the configured list */
12052 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
12053 {
12054 if( strlen( *cur ) == alpn_len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +030012055 mbedtls_platform_memcmp( p, cur, alpn_len ) == 0 )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012056 {
12057 ssl->alpn_chosen = *cur;
12058 break;
12059 }
12060 }
12061 }
12062
12063 /* can only happen on conf mismatch */
12064 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
12065 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12066
12067 p += alpn_len;
12068 }
12069#endif /* MBEDTLS_SSL_ALPN */
12070
12071 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012072 * Forced fields from top-level ssl_context structure
12073 *
12074 * Most of them already set to the correct value by mbedtls_ssl_init() and
12075 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
12076 */
12077 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
12078
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012079#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012080 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012081#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
12082#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012083 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012084#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012085
Hanno Becker83985822019-08-30 10:42:49 +010012086 /* Adjust pointers for header fields of outgoing records to
12087 * the given transform, accounting for explicit IV and CID. */
12088 ssl_update_out_pointers( ssl, ssl->transform );
12089
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012090#if defined(MBEDTLS_SSL_PROTO_DTLS)
12091 ssl->in_epoch = 1;
12092#endif
12093
12094 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
12095 * which we don't want - otherwise we'd end up freeing the wrong transform
12096 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
12097 if( ssl->handshake != NULL )
12098 {
12099 mbedtls_ssl_handshake_free( ssl );
12100 mbedtls_free( ssl->handshake );
12101 ssl->handshake = NULL;
12102 }
12103
12104 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020012105 * Done - should have consumed entire buffer
12106 */
12107 if( p != end )
12108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012109
12110 return( 0 );
12111}
12112
12113/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020012114 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012115 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012116int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012117 const unsigned char *buf,
12118 size_t len )
12119{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012120 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012121
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012122 if( ret != 0 )
12123 mbedtls_ssl_free( context );
12124
12125 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012126}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020012127#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012128
12129/*
Paul Bakker5121ce52009-01-03 21:22:43 +000012130 * Free an SSL context
12131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012132void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000012133{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020012134 if( ssl == NULL )
12135 return;
12136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012138
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012139 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012140 {
Angus Grattond8213d02016-05-25 20:56:48 +100012141 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012142 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012143 }
12144
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012145 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012146 {
Angus Grattond8213d02016-05-25 20:56:48 +100012147 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012148 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012149 }
12150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012151#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020012152 if( ssl->compress_buf != NULL )
12153 {
Angus Grattond8213d02016-05-25 20:56:48 +100012154 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012155 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020012156 }
12157#endif
12158
Paul Bakker48916f92012-09-16 19:57:18 +000012159 if( ssl->transform )
12160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012161 mbedtls_ssl_transform_free( ssl->transform );
12162 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000012163 }
12164
12165 if( ssl->handshake )
12166 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020012167 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012168 mbedtls_ssl_transform_free( ssl->transform_negotiate );
12169 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012171 mbedtls_free( ssl->handshake );
12172 mbedtls_free( ssl->transform_negotiate );
12173 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012174 }
12175
Paul Bakkerc0463502013-02-14 11:19:38 +010012176 if( ssl->session )
12177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012178 mbedtls_ssl_session_free( ssl->session );
12179 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010012180 }
12181
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030012182#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020012183 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012184 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012185 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012186 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000012187 }
Paul Bakker0be444a2013-08-27 21:55:01 +020012188#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000012189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012190#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
12191 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000012192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
12194 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000012195 }
12196#endif
12197
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012198#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012199 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020012200#endif
12201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000012203
Paul Bakker86f04f42013-02-14 11:20:09 +010012204 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012205 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012206}
12207
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012208/*
12209 * Initialze mbedtls_ssl_config
12210 */
12211void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
12212{
12213 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010012214
12215#if !defined(MBEDTLS_SSL_PROTO_TLS)
12216 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
12217#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012218}
12219
Simon Butcherc97b6972015-12-27 23:48:17 +000012220#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012221#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012222static int ssl_preset_default_hashes[] = {
12223#if defined(MBEDTLS_SHA512_C)
12224 MBEDTLS_MD_SHA512,
12225 MBEDTLS_MD_SHA384,
12226#endif
12227#if defined(MBEDTLS_SHA256_C)
12228 MBEDTLS_MD_SHA256,
12229 MBEDTLS_MD_SHA224,
12230#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020012231#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012232 MBEDTLS_MD_SHA1,
12233#endif
12234 MBEDTLS_MD_NONE
12235};
Simon Butcherc97b6972015-12-27 23:48:17 +000012236#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012237#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012238
Hanno Becker73f4cb12019-06-27 13:51:07 +010012239#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012240static int ssl_preset_suiteb_ciphersuites[] = {
12241 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
12242 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
12243 0
12244};
Hanno Becker73f4cb12019-06-27 13:51:07 +010012245#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012246
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012247#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012248#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012249static int ssl_preset_suiteb_hashes[] = {
12250 MBEDTLS_MD_SHA256,
12251 MBEDTLS_MD_SHA384,
12252 MBEDTLS_MD_NONE
12253};
12254#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012255#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012256
Hanno Beckerc1096e72019-06-19 12:30:41 +010012257#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012258static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010012259#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012260 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012261#endif
12262#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012263 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012264#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012265 MBEDTLS_ECP_DP_NONE
12266};
12267#endif
12268
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012269/*
Tillmann Karras588ad502015-09-25 04:27:22 +020012270 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012271 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012272int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012273 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012274{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012275#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012276 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012277#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012278
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020012279 /* Use the functions here so that they are covered in tests,
12280 * but otherwise access member directly for efficiency */
12281 mbedtls_ssl_conf_endpoint( conf, endpoint );
12282 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012283
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012284 /*
12285 * Things that are common to all presets
12286 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012287#if defined(MBEDTLS_SSL_CLI_C)
12288 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
12289 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012290#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012291 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012292#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012293#if defined(MBEDTLS_SSL_SESSION_TICKETS)
12294 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
12295#endif
12296 }
12297#endif
12298
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012299#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012300 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012301#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012302
12303#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
12304 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
12305#endif
12306
12307#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010012308#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012309 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012310#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
12311#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030012312 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030012313 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012314#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012315#endif
12316
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010012317#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
12318 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
12319#endif
12320
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012321#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012322 conf->f_cookie_write = ssl_cookie_write_dummy;
12323 conf->f_cookie_check = ssl_cookie_check_dummy;
12324#endif
12325
Hanno Becker7f376f42019-06-12 16:20:48 +010012326#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
12327 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012328 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
12329#endif
12330
Janos Follath088ce432017-04-10 12:42:31 +010012331#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012332#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010012333 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012334#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
12335#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010012336
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012337#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010012338#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012339 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012340#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
12341#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012342 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012343#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
12344#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012345
12346#if defined(MBEDTLS_SSL_RENEGOTIATION)
12347 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020012348 mbedtls_platform_memset( conf->renego_period, 0x00, 2 );
12349 mbedtls_platform_memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012350#endif
12351
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012352#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
12353 if( endpoint == MBEDTLS_SSL_IS_SERVER )
12354 {
Hanno Becker00d0a682017-10-04 13:14:29 +010012355 const unsigned char dhm_p[] =
12356 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
12357 const unsigned char dhm_g[] =
12358 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
12359
Hanno Beckera90658f2017-10-04 15:29:08 +010012360 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
12361 dhm_p, sizeof( dhm_p ),
12362 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012363 {
12364 return( ret );
12365 }
12366 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020012367#endif
12368
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012369 /*
12370 * Preset-specific defaults
12371 */
12372 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012373 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012374 /*
12375 * NSA Suite B
12376 */
12377 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010012378#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012379 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010012380#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12381#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012382 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010012383#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12384#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012385 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012386#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12387#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012388 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012389#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012390
Hanno Becker73f4cb12019-06-27 13:51:07 +010012391#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012392 conf->ciphersuite_list[0] =
12393 conf->ciphersuite_list[1] =
12394 conf->ciphersuite_list[2] =
12395 conf->ciphersuite_list[3] =
12396 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010012397#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012398
12399#if defined(MBEDTLS_X509_CRT_PARSE_C)
12400 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012401#endif
12402
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012403#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012404#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012405 conf->sig_hashes = ssl_preset_suiteb_hashes;
12406#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012407#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012408
12409#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012410#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012411 conf->curve_list = ssl_preset_suiteb_curves;
12412#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012413#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020012414 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012415
12416 /*
12417 * Default
12418 */
12419 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010012420#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012421 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
12422 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
12423 MBEDTLS_SSL_MIN_MAJOR_VERSION :
12424 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012425#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12426#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012427 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
12428 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
12429 MBEDTLS_SSL_MIN_MINOR_VERSION :
12430 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012431#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020012432 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012433 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
12434#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010012435#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12436#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
12437 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12438#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12439#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12440 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12441#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012442
Hanno Becker73f4cb12019-06-27 13:51:07 +010012443#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012444 conf->ciphersuite_list[0] =
12445 conf->ciphersuite_list[1] =
12446 conf->ciphersuite_list[2] =
12447 conf->ciphersuite_list[3] =
12448 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012449#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012450
12451#if defined(MBEDTLS_X509_CRT_PARSE_C)
12452 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12453#endif
12454
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012455#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012456#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012457 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012458#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012459#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012460
12461#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012462#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012463 conf->curve_list = mbedtls_ecp_grp_id_list();
12464#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012465#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012466
12467#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12468 conf->dhm_min_bitlen = 1024;
12469#endif
12470 }
12471
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012472 return( 0 );
12473}
12474
12475/*
12476 * Free mbedtls_ssl_config
12477 */
12478void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12479{
12480#if defined(MBEDTLS_DHM_C)
12481 mbedtls_mpi_free( &conf->dhm_P );
12482 mbedtls_mpi_free( &conf->dhm_G );
12483#endif
12484
12485#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12486 if( conf->psk != NULL )
12487 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012488 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012489 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012490 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012491 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012492 }
12493
12494 if( conf->psk_identity != NULL )
12495 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012496 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012497 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012498 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012499 conf->psk_identity_len = 0;
12500 }
12501#endif
12502
12503#if defined(MBEDTLS_X509_CRT_PARSE_C)
12504 ssl_key_cert_free( conf->key_cert );
12505#endif
12506
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012507 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012508}
12509
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012510#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012511 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12512 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012513/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012514 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012516unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012518#if defined(MBEDTLS_RSA_C)
12519 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12520 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012521#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012522#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012523 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12524 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012525#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012526 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012527}
12528
Hanno Becker7e5437a2017-04-28 17:15:26 +010012529unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12530{
12531 switch( type ) {
12532 case MBEDTLS_PK_RSA:
12533 return( MBEDTLS_SSL_SIG_RSA );
12534 case MBEDTLS_PK_ECDSA:
12535 case MBEDTLS_PK_ECKEY:
12536 return( MBEDTLS_SSL_SIG_ECDSA );
12537 default:
12538 return( MBEDTLS_SSL_SIG_ANON );
12539 }
12540}
12541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012542mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012543{
12544 switch( sig )
12545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012546#if defined(MBEDTLS_RSA_C)
12547 case MBEDTLS_SSL_SIG_RSA:
12548 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012549#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012550#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012551 case MBEDTLS_SSL_SIG_ECDSA:
12552 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012553#endif
12554 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012555 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012556 }
12557}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012558#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012559
Hanno Becker7e5437a2017-04-28 17:15:26 +010012560#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12561 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12562
12563/* Find an entry in a signature-hash set matching a given hash algorithm. */
12564mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12565 mbedtls_pk_type_t sig_alg )
12566{
12567 switch( sig_alg )
12568 {
12569 case MBEDTLS_PK_RSA:
12570 return( set->rsa );
12571 case MBEDTLS_PK_ECDSA:
12572 return( set->ecdsa );
12573 default:
12574 return( MBEDTLS_MD_NONE );
12575 }
12576}
12577
12578/* Add a signature-hash-pair to a signature-hash set */
12579void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12580 mbedtls_pk_type_t sig_alg,
12581 mbedtls_md_type_t md_alg )
12582{
12583 switch( sig_alg )
12584 {
12585 case MBEDTLS_PK_RSA:
12586 if( set->rsa == MBEDTLS_MD_NONE )
12587 set->rsa = md_alg;
12588 break;
12589
12590 case MBEDTLS_PK_ECDSA:
12591 if( set->ecdsa == MBEDTLS_MD_NONE )
12592 set->ecdsa = md_alg;
12593 break;
12594
12595 default:
12596 break;
12597 }
12598}
12599
12600/* Allow exactly one hash algorithm for each signature. */
12601void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12602 mbedtls_md_type_t md_alg )
12603{
12604 set->rsa = md_alg;
12605 set->ecdsa = md_alg;
12606}
12607
12608#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12609 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12610
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012611/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012612 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012613 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012614mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012615{
12616 switch( hash )
12617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012618#if defined(MBEDTLS_MD5_C)
12619 case MBEDTLS_SSL_HASH_MD5:
12620 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012621#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012622#if defined(MBEDTLS_SHA1_C)
12623 case MBEDTLS_SSL_HASH_SHA1:
12624 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012625#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012626#if defined(MBEDTLS_SHA256_C)
12627 case MBEDTLS_SSL_HASH_SHA224:
12628 return( MBEDTLS_MD_SHA224 );
12629 case MBEDTLS_SSL_HASH_SHA256:
12630 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012631#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012632#if defined(MBEDTLS_SHA512_C)
12633 case MBEDTLS_SSL_HASH_SHA384:
12634 return( MBEDTLS_MD_SHA384 );
12635 case MBEDTLS_SSL_HASH_SHA512:
12636 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012637#endif
12638 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012639 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012640 }
12641}
12642
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012643/*
12644 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12645 */
12646unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12647{
12648 switch( md )
12649 {
12650#if defined(MBEDTLS_MD5_C)
12651 case MBEDTLS_MD_MD5:
12652 return( MBEDTLS_SSL_HASH_MD5 );
12653#endif
12654#if defined(MBEDTLS_SHA1_C)
12655 case MBEDTLS_MD_SHA1:
12656 return( MBEDTLS_SSL_HASH_SHA1 );
12657#endif
12658#if defined(MBEDTLS_SHA256_C)
12659 case MBEDTLS_MD_SHA224:
12660 return( MBEDTLS_SSL_HASH_SHA224 );
12661 case MBEDTLS_MD_SHA256:
12662 return( MBEDTLS_SSL_HASH_SHA256 );
12663#endif
12664#if defined(MBEDTLS_SHA512_C)
12665 case MBEDTLS_MD_SHA384:
12666 return( MBEDTLS_SSL_HASH_SHA384 );
12667 case MBEDTLS_MD_SHA512:
12668 return( MBEDTLS_SSL_HASH_SHA512 );
12669#endif
12670 default:
12671 return( MBEDTLS_SSL_HASH_NONE );
12672 }
12673}
12674
Hanno Beckeree902df2019-08-23 13:47:47 +010012675#if defined(MBEDTLS_USE_TINYCRYPT)
12676/*
12677 * Check if a curve proposed by the peer is in our list.
12678 * Return 0 if we're willing to use it, -1 otherwise.
12679 */
12680int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12681 mbedtls_uecc_group_id grp_id )
12682{
12683 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12684 if( own_ec_id == grp_id )
12685 return( 0 );
12686 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12687
12688 return( -1 );
12689}
12690#endif /* MBEDTLS_USE_TINYCRYPT */
12691
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012692#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012693/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012694 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012695 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012696 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012697int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12698 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012699{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012700 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12701 if( own_ec_id == grp_id )
12702 return( 0 );
12703 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012704
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012705 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012706}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012707#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012708
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012709#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012710/*
12711 * Check if a hash proposed by the peer is in our list.
12712 * Return 0 if we're willing to use it, -1 otherwise.
12713 */
12714int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12715 mbedtls_md_type_t md )
12716{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012717 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12718 if( md_alg == md )
12719 return( 0 );
12720 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012721
12722 return( -1 );
12723}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012724#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012726#if defined(MBEDTLS_X509_CRT_PARSE_C)
12727int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012728 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012729 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012730 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012731{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012732 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012733#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012734 int usage = 0;
12735#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012736#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012737 const char *ext_oid;
12738 size_t ext_len;
12739#endif
12740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012741#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12742 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012743 ((void) cert);
12744 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012745 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012746#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012748#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12749 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012750 {
12751 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012752 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012754 case MBEDTLS_KEY_EXCHANGE_RSA:
12755 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012756 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012757 break;
12758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012759 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12760 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12761 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12762 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012763 break;
12764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012765 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12766 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012767 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012768 break;
12769
12770 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012771 case MBEDTLS_KEY_EXCHANGE_NONE:
12772 case MBEDTLS_KEY_EXCHANGE_PSK:
12773 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12774 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012775 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012776 usage = 0;
12777 }
12778 }
12779 else
12780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012781 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12782 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012783 }
12784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012785 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012786 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012787 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012788 ret = -1;
12789 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012790#else
12791 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012792#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012794#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12795 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012797 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12798 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012799 }
12800 else
12801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012802 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12803 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012804 }
12805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012806 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012807 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012808 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012809 ret = -1;
12810 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012811#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012812
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012813 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012814}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012815#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012816
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012817#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12818 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12819int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12820 unsigned char *output,
12821 unsigned char *data, size_t data_len )
12822{
12823 int ret = 0;
12824 mbedtls_md5_context mbedtls_md5;
12825 mbedtls_sha1_context mbedtls_sha1;
12826
12827 mbedtls_md5_init( &mbedtls_md5 );
12828 mbedtls_sha1_init( &mbedtls_sha1 );
12829
12830 /*
12831 * digitally-signed struct {
12832 * opaque md5_hash[16];
12833 * opaque sha_hash[20];
12834 * };
12835 *
12836 * md5_hash
12837 * MD5(ClientHello.random + ServerHello.random
12838 * + ServerParams);
12839 * sha_hash
12840 * SHA(ClientHello.random + ServerHello.random
12841 * + ServerParams);
12842 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012843 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012844 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012845 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012846 goto exit;
12847 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012848 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012849 ssl->handshake->randbytes, 64 ) ) != 0 )
12850 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012851 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012852 goto exit;
12853 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012854 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012855 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012856 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012857 goto exit;
12858 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012859 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012860 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012861 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012862 goto exit;
12863 }
12864
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012865 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012866 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012867 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012868 goto exit;
12869 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012870 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012871 ssl->handshake->randbytes, 64 ) ) != 0 )
12872 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012873 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012874 goto exit;
12875 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012876 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012877 data_len ) ) != 0 )
12878 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012880 goto exit;
12881 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012882 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012883 output + 16 ) ) != 0 )
12884 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012886 goto exit;
12887 }
12888
12889exit:
12890 mbedtls_md5_free( &mbedtls_md5 );
12891 mbedtls_sha1_free( &mbedtls_sha1 );
12892
12893 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012894 mbedtls_ssl_pend_fatal_alert( ssl,
12895 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012896
12897 return( ret );
12898
12899}
12900#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12901 MBEDTLS_SSL_PROTO_TLS1_1 */
12902
12903#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12904 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12905int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012906 unsigned char *hash, size_t *hashlen,
12907 unsigned char *data, size_t data_len,
12908 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012909{
12910 int ret = 0;
12911 mbedtls_md_context_t ctx;
Hanno Beckera5cedbc2019-07-17 11:21:02 +010012912 mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012913 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012914
12915 mbedtls_md_init( &ctx );
12916
12917 /*
12918 * digitally-signed struct {
12919 * opaque client_random[32];
12920 * opaque server_random[32];
12921 * ServerDHParams params;
12922 * };
12923 */
12924 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12925 {
12926 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12927 goto exit;
12928 }
12929 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12930 {
12931 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12932 goto exit;
12933 }
12934 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12935 {
12936 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12937 goto exit;
12938 }
12939 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12940 {
12941 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12942 goto exit;
12943 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012944 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012945 {
12946 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12947 goto exit;
12948 }
12949
12950exit:
12951 mbedtls_md_free( &ctx );
12952
12953 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012954 mbedtls_ssl_pend_fatal_alert( ssl,
12955 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012956
12957 return( ret );
12958}
12959#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12960 MBEDTLS_SSL_PROTO_TLS1_2 */
12961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012962#endif /* MBEDTLS_SSL_TLS_C */